(TextView)this.findViewById(....) liefert null

E

Ernesto S

Neues Mitglied
0
Hallo,

wie in zahlreichen Tutorials beschrieben, ist es sehr einfach, den Text eines TextViews in onCreate nach setContentView zu ändern.
Leider führt das bei mir so nicht zum Erfolg.
LogCat zeigt keinen Fehler, aber textView ist null.

Vielen Dank für die Hilfe Ernesto

Hier Coding, Layout und Manifest:

Code:
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
        
        TextView textView = (TextView)this.findViewById(R.id.textView1);
        if (textView != null){
                Log.d("TEST", "Alles io");
               String str = "na geht doch !";
                 textView.setText(str);
          } else {
               Log.d("TEST", " textView ist null");
          }

    }
Code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="horizontal"
    tools:context="com.example.mytest.MainActivity$PlaceholderFragment" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
 
</LinearLayout>
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mytest"
    android:versionCode="1"
    android:versionName="1.0" >
    

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.mytest.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
 
hast das richtige R importiert ? android.R wäre falsch
 
@Ernesto S: Stichwort Fragment ;)
 
Ich habe mit Eclipse ein neues Androidprojekt erzeugt um evtl. selbstgemachte Fehlerquellen in meinem App auszuschliessen und wollte das mit dem Textview ausprobieren, weil es in meinem App auch nicht läuft. Also nehme ich mal an, dass das richtige R. importiert wird.
Das Stichwort Fragmentierung bringt mich nicht weiter. Einen Verdacht hatte ich schon in dieser Richtung, weil nirgendwo in den Beispielen das Coding mit getSupportFragmentManager().... enthalten war. Ein paar Stichworte mehr wären bestimmt hilfreich. Vielen Dank
 
Hast du schon mal geprüft, ob der TextView vor der Transaction des PlaceholderFragment null ist?
 
Ändere die id von textView1 in Layout und Activity mal um in testText. Ich würde wetten, dass die IDE dann meckert, weil das falsche R importiert wird. Problem ist, dass es in android.R auch ein textView1 gibt.
 
Der TextView existiert nicht in der Activity View Hierarchie, weshalb der Wert null ist.

In der Methode setContentView() wird die Datei activity_main.xml als Resource geladen. Das xml layout, das in deiner Frage steht, gehört aber zum PlaceholderFragment. Dies wird vom PlaceholderFragment geladen.

Wenn du den TextView aufrufen möchtest, muss du das also im PlaceholderFragment tun, nicht in der Activity.
 
Im PlaceholderFragment wird der TextView erkannt. Das war die Lösung.
Code:
   public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            
            
            TextView textView = (TextView)rootView.findViewById(R.id.textView1);
            if (textView != null){
                Log.d("TEST", "Alles io");
                String str = "na geht doch !";
                textView.setText(str);
               } else {
                Log.d("TEST", " Textview ist null");
            }
           
            
            return rootView;
        }
    }

Vielen Dank für die Hilfe, besonders an markus.tullius für die Lösung
Ernesto
 
Ernesto S schrieb:
Das Stichwort Fragmentierung
Fragment != Fragmentierung ;)

Lies dich ein was Fragments sind, schau dir deinen Code an und erkenne, dass da bereits ein Fragment genutzt wird ;)
 

Ähnliche Themen

T
Antworten
1
Aufrufe
857
jogimuc
J
J
Antworten
4
Aufrufe
807
deek
D
K
Antworten
10
Aufrufe
1.509
swa00
swa00
Zurück
Oben Unten