NullPointerException:设置 TextView 值时
我有一个布局文件(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
I have a layout file(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"
/>
My Activity:
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));
}
}
(I am sure the problem is not about the res/strings.xml
, because the "hello" string resource has been used in other place which is fine.)
Why I got NullPointerException when I set the TextView
content ??
The full error trace:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
创建 Textview id 如下 myTitle = (TextView) myTestView .findViewById(R.id.my_title);
create Textview id as follows myTitle = (TextView) myTestView .findViewById(R.id.my_title);
您应该正确编写 onCreate ,因为您没有设置任何要在屏幕上显示的内容:
you should write your onCreate correctly because you are not setting any thing to be displayed on screen:
您只能直接获取 setcontentView(R.layout.your_layout_id) 中声明的布局中视图的 id;但是当您膨胀某些布局时,您必须获取膨胀视图中视图的 id,如下所示 view_id.find....();
you can only get the id's directly for views in layout that is declared in setcontentView(R.layout.your_layout_id); but when you inflate some layout then you have to get ids of the views in the inflated view as follows view_id.find....();