我无法通过调用TextView
找到findViewById()
,即使该ID确实存在。
OtherActivity
:
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 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>
AndroidManifest.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
为空?
答案 0 :(得分:15)
在尝试查找任何UI组件之前,必须先调用setContentView(...)
。你在打电话之前试图找到TextView
。
将您的代码更改为此...
super.onCreate(savedInstanceState);
setContentView(R.layout.other_activity);
TextView textView = (TextView)findViewById(R.id.txt02);
答案 1 :(得分:4)
更改您的代码,如::
TextView textView = (TextView)findViewById(R.id.txt02);
没有像“ID”这样的大写字母
主要:
<TextView android:id="@+id/txt02" android:layout_width="fill_parent"
android:layout_height="wrap_content" />
答案 2 :(得分:1)
我实际上只是遇到了这个问题,这是因为现在自动检查了构建。只需手动构建项目或自动单击构建,这应该可以解决您的问题!
答案 3 :(得分:1)
您必须确保在创建活动时已生成TextView元素。
最有可能的是,如果它是一个Menu元素,则必须在onCreateOptionsMenu()回调上调用findViewById方法,而不是在Oncreate()回调上调用。