【发布时间】:2014-04-24 18:54:43
【问题描述】:
我是 android 开发的新手,在这方面花了几个小时。 (日食+广告) 这里有一些额外的词,所以我可以发布这个。调用run_time.setText("t")时出现空指针异常;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_run);
TextView run_time = (TextView) findViewById(R.id.run_time);
try {
run_time.setText("t");
}catch(Exception e) {
Log.i("Log", e.getMessage()+"Error!"); // LogCat message
}
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
layout activity_start_run.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/run_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ghjghjghjghjghjghj" />
</LinearLayout>
【问题讨论】:
-
这个布局文件真的叫
activity_start_run.xml吗? -
是的,文件名应该是正确的,很郁闷
-
如果您 100% 肯定布局名称是这样的,请清理并重建以防 R 和二进制 XML 文件中的资源 ID 不同步。
-
这个例外的解释是
run_time实际上是在片段布局中而不是在活动布局中。 -
它们在片段中,但我将它们复制到活动中,没有帮助。
标签: android eclipse nullpointerexception settext