2011-07-20 35 views
5

Tôi không thể tìm thấy một TextView bằng cách gọi phương thức findViewById ngay cả thông qua ID không tồn tại. xin vui lòng xem mã của tôi:Không thể tìm thấy một TextView bằng cách gọi phương thức findViewById

Hoạt động:

public class OtherActivity extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     TextView textView = (TextView)findViewById(R.ID.txt02); 
     super.onCreate(savedInstanceState);//line 5 
     setContentView(R.layout.other_activity); 
     //textView.setText(ACCESSIBILITY_SERVICE); 
     Button button = (Button)findViewById(R.ID.otherActivity_Btn); 
     button.setText(R.string.otherActivityBtn); 
     button.setOnClickListener(new GoBackBtnListener(this)); 
    } 

    class GoBackBtnListener implements OnClickListener { 
     OtherActivity other = null; 
     public GoBackBtnListener(OtherActivity p_other) { 
      other = p_other; 
     } 
     public void onClick(View v) { 
      Intent intent = new Intent(); 
      intent.setClass(other, Activity01Activity.class); 
      other.startActivity(intent); 
     } 
    } 
} 

tôi thêm một breakpoint tại dòng 5 và bắt đầu dự án trong chế độ gỡ lỗi, khi thực hiện dừng lại ở các điểm break, tôi chuyển chuột để TextView biến, nó là null. các other_activity.xml là như thế này:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

<TextView 
    android:id="@+ID/txt02" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
/> 

<Button 
    android:id="@+ID/otherActivity_Btn" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
/> 
</LinearLayout> 

và Androidmenifest.xml là như thế này:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.hp" android:versionCode="1" android:versionName="1.0"> 
    <uses-sdk android:minSdkVersion="4" /> 

    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
     <activity android:name=".Activity01Activity" android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity android:name=".OtherActivity" android:label="@string/other"></activity> 
    </application> 
</manifest> 

các R.java là như sau:

public final class R { 
    public static final class ID { 
     public static final int Activity01_btn=0x7f050001; 
     public static final int otherActivity_Btn=0x7f050003; 
     public static final int txt01=0x7f050000; 
     public static final int txt02=0x7f050002; 
    } 
    public static final class attr { 
    } 
    public static final class drawable { 
     public static final int icon=0x7f020000; 
    } 
    public static final class layout { 
     public static final int main=0x7f030000; 
     public static final int other_activity=0x7f030001; 
    } 
    public static final class string { 
     public static final int app_name=0x7f040001; 
     public static final int hello=0x7f040000; 
     public static final int other=0x7f040002; 
     public static final int otherActivityBtn=0x7f040003; 
    } 
} 

Vì vậy, tôi không nghĩ rằng TextView không thể được tìm thấy, nhưng tại sao textView biến là null. Tôi đã nhầm lẫn. bạn có thể giúp tôi tìm ra nguyên nhân gốc rễ không?

+0

Hãy thử gọi setContentView (R.layout.other_activity); ngay sau cuộc gọi super.onCreate (savedInstance) của onCreate in onCreate ... –

+0

Tôi không chắc chắn whetehr "ID" có thể là mũ ở đây. android: id = "@ + ID/otherActivity_Btn" –

+0

Tôi cảm thấy Andro_selva là đúng "id" trong TextView textView = (TextView) findViewById (R.ID.txt02); không thể được bỏ mũ – Pravy

Trả lời

15

Bạn phải gọi setContentView(...) TRƯỚC KHI bạn tìm cách tìm bất kỳ thành phần giao diện người dùng nào. Bạn đang cố gắng tìm số TextView trước khi bạn gọi nó.

Thay đổi mã của bạn để này ...

super.onCreate(savedInstanceState); 
setContentView(R.layout.other_activity); 
TextView textView = (TextView)findViewById(R.id.txt02); 
+1

lãng phí năm giờ của tôi về vấn đề này: (Bạn đã lưu tôi – Sikander

4

thay đổi mã của bạn như ::

TextView textView = (TextView)findViewById(R.id.txt02); 

Không có chữ cái viết hoa như "ID"

chính:

<TextView android:id="@+id/txt02" android:layout_width="fill_parent" 
    android:layout_height="wrap_content" /> 
+0

và cũng làm theo mã được đề xuất bởi MisterSquonk –

+0

Vâng, bắt tốt, tôi đã không phát hiện thấy chữ hoa chữ thường. +1 – Squonk

1

Tôi thực sự chỉ có vấn đề này nó đã được gây ra bởi bây giờ có xây dựng tự động kiểm tra. Chỉ cần xây dựng dự án của bạn theo cách thủ công hoặc nhấp vào xây dựng tự động và điều đó sẽ giải quyết được vấn đề của bạn!

+0

Cách vấn đề này gây ra bởi tự động xây dựng? – AaA

1

Bạn phải đảm bảo rằng phần tử TextView của bạn đã được tạo khi tạo hoạt động của bạn.

Rất có thể, nếu đó là phần tử Menu, bạn sẽ phải gọi phương thức findViewById trên gọi lại onCreateOptionsMenu() và không phải trên cuộc gọi lại Oncreate().

Các vấn đề liên quan