【发布时间】:2012-02-13 15:11:08
【问题描述】:
我有一个布局文件(layout/my_test.xml):
<TextView
android:id="@+id/my_title"
android:gravity="left|center_vertical"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
/>
我的活动:
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.TextView;
public class MyActivity extends FragmentActivity{
private View myTestView;
private TextView actionBarTitle;
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
myTestView = getLayoutInflater().inflate(R.layout.my_test, null);
myTitle = (TextView) findViewById(R.id.my_title);
//NullPointerException here !!
myTitle.setText(getString(R.string.hello));
}
}
(我确定问题不在于 res/strings.xml ,因为“hello”字符串资源已在其他地方使用,这很好。)
为什么我在设置TextView 内容时得到NullPointerException??
完整的错误跟踪:
Caused by: java.lang.NullPointerException
E/AndroidRuntime(17283): at com.test.MyActivity.onCreate(MyActivity.java:21)
E/AndroidRuntime(17283): at com.eficode.card.CardActivity.onCreate(CarActivity.java:29)
E/AndroidRuntime(17283): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime(17283): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2669)
E/AndroidRuntime(17283): ... 11 more
【问题讨论】:
-
你没有设置任何东西显示在屏幕上?
标签: android android-layout android-intent android-emulator