我是android開發中的新手,已經花了幾個小時。 (eclipse + adt) 這裏有一些額外的單詞,所以我可以發表這個。調用run_time.setText(「t」)時獲取空指針異常setText導致nullPointerException,非常簡單,但沒有想法(android)
@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'這個佈局文件? – nKn
是的,文件名應該是正確的,非常令人沮喪 – user3570219
如果您100%肯定佈局名稱是這樣的,那麼在R和二進制XML文件中的資源ID不同步的情況下清理並重新生成。 – laalto