时,我有一个布局文件(layout/my_test.xml
):NullPointerException异常:设置TextView的价值
<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
,因为“他LLO”字符串资源已经在这是很好的其他地方使用。)
为什么我NullPointerException异常当我设置TextView
内容?
完整的错误跟踪:
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
你没有设置任何东西在屏幕上显示? – waqaslam 2012-02-13 15:15:32