开发者

Accessing a TextView from a resource file

开发者 https://www.devze.com 2023-02-18 01:27 出处:网络
Let me first explain what my Androïd application is made of : - a class extended from an Activity. In the OnCreate member of my class, I try to access a TextView described in my main.xml file by usin

Let me first explain what my Androïd application is made of : - a class extended from an Activity. In the OnCreate member of my class, I try to access a TextView described in my main.xml file by using

"MyTextView=(TextView)findViewById(R.id.myTextView);".

- an xml file where the TextView is described as follows :

<TextView android:name="@+id/myTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/another TextView"/> In the R.java file, I can see that my TextView is registered.

My problem is that, when I try to get a handle on the TextView with the findViewById function, I get a null pointer.

It seems a mystery to me because I wrote another application where I was able to access TextViews. And I can't see any difference beetween both applications!!!

Hello,

Here is my complete layout file :

<TextView
    android:name="@+id/Titre"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Application GPS :"/>

<TextView
    android:name="@+id/NombreMaxSatellites"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

<TextView
    android:name="@+id/NombreSatellites"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Hello Android from Ne开发者_如何转开发tBeans"/>

<TextView
    android:name="@+id/TempsAcquisition"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Hello Android from NetBeans"/>

<EditText
    android:name="@+id/Texte"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Le texte"/>

<Button android:id="@+id/BoutonTexte"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="130dip"
android:layout_marginTop="10dip"
android:text="Terminer"/>

I can access the Button but neither the TextView nor the EditText.

Any idea?


I am sure there are some errors in the layout, due to which your changes for layout were not saved. And then it is not reflecting in R.Java. Look for any red marks in your layout. Resolve the errors before your proceed, then it should work fine. Post your complete layout file.


Make sure that any calls to findViewById() occur after setting the layout with setContentView().

A good practice is to call setContentView() as the first line in your onCreate() method.


Make sure about 2 things

  1. the ID of your textview is not repeated in the same layout
  2. make sure that you are setting the content view (setContentView()) to the correct layout.


The problem is id is assigned to name android:name="@+id/Titre", it should be android:id="@+id/Titre".

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号