2011-03-19 117 views
0

讓我先解釋一下我的Android應用程序是由: - 一類從活動延長。在我班上的OnCreate成員,我嘗試使用從資源文件訪問一個TextView

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

訪問我main.xml文件中描述一個TextView - 其中的TextView被描述爲一個XML文件如下:
<TextView android:name="@+id/myTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/another TextView"/>R.java文件,我可以看到我的TextView已註冊。

我的問題是,當我試圖得到一個手柄上的TextView與findViewById功能,我得到一個空指針。

這似乎是一個謎我,因爲我寫了另一個應用程序,我能訪問TextViews。我看不出兩個應用程序之間有什麼區別!

你好,

這裏是我的完整佈局文件:

<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 NetBeans"/> 

<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"/> 

我可以訪問Button但無論是TextView也不EditText

有什麼想法?

+0

接受一些答案 – 2011-05-04 18:28:39

回答

0

我肯定有一些錯誤的佈局,由於它的佈局的變更並未儲存。然後它不會在R.Java中反映出來。在佈局中查找任何紅色標記。在繼續之前解決錯誤,那麼它應該可以正常工作。發佈完整的佈局文件。

+0

我的佈局文件中沒有紅線或編譯錯誤。 – Zelig 2011-03-20 07:39:34

+0

發佈您的完整佈局xml和代碼文件。 – 2011-03-21 06:09:14

0

確保約2個東西

  1. 在相同的佈局
  2. 確保您設置的內容視圖(setContentView()),以正確的佈局,不重複你的TextView的ID。
0

請確保在setContentView()設置佈局後發生findViewById()的任何調用。

一個好的做法是調用setContentView()在你onCreate()方法的第一行。

+0

在finViewVyId調用之前,我有setContentView() – Zelig 2011-03-20 07:37:57

0

問題是id被分配到名稱android:name="@+id/Titre",它應該是android:id="@+id/Titre"