NullPointerException:设置 TextView 值时

发布于 2025-01-05 01:52:33 字数 1572 浏览 0 评论 0原文

我有一个布局文件(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黯淡〆 2025-01-12 01:52:33

创建 Textview id 如下 myTitle = (TextView) myTestView .findViewById(R.id.my_title);

create Textview id as follows myTitle = (TextView) myTestView .findViewById(R.id.my_title);

不一样的天空 2025-01-12 01:52:33

您应该正确编写 onCreate ,因为您没有设置任何要在屏幕上显示的内容:

@Override
    protected void onCreate(Bundle arg0) {
       super.onCreate(arg0);
       setContentView(R.layout.my_test);

       myTitle = (TextView) findViewById(R.id.my_title);

       myTitle.setText(getString(R.string.hello));
    }

you should write your onCreate correctly because you are not setting any thing to be displayed on screen:

@Override
    protected void onCreate(Bundle arg0) {
       super.onCreate(arg0);
       setContentView(R.layout.my_test);

       myTitle = (TextView) findViewById(R.id.my_title);

       myTitle.setText(getString(R.string.hello));
    }
悲欢浪云 2025-01-12 01:52:33

您只能直接获取 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....();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文