即使通過ID確實存在,我也無法通過調用findViewById方法找到TextView。 請參閱我的代碼:通過調用findViewById方法找不到TextView
活動:
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);
}
}
}
我在5號線加一個斷點,在調試模式下啓動的項目,當停在斷點執行時,我把鼠標移動到變量的TextView,它是空的。 的other_activity.xml是這樣的:
<?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>
和Androidmenifest.xml是這樣的:
<?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>
的R.java是如下:
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;
}
}
所以,我不要認爲TextView找不到,但爲什麼變量textView爲null。我很困惑。 你能幫我找到根本原因嗎?
嘗試調用setContentView(R.layout.other_activity);在onCreate的super.onCreate(savedInstance)調用onCreate後... –
我不知道什麼時候「ID」可以在這裏上限。 android:id =「@ + ID/otherActivity_Btn」 –
我覺得Andro_selva是正確的在TextView中的「ID」textView =(TextView)findViewById(R.ID.txt02); can not be caps – Pravy