TextView上的SetText在NullPointerException上崩溃

时间:2014-04-01 18:42:01

标签: android

请我有这个代码:

MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    data = (TextView)findViewById(R.id.vystup);
    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }
    String pom = "AAA";
    data.setText(pom);
}

activity_main.xml中

<TextView
    android:id="@+id/vystup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="44dp"
    android:text="@string/vystup1"
    android:textAppearance="?android:attr/textAppearanceMedium" />

但是它与

一起崩溃了
data.setText(pom)

在NullPointerException上,因为数据没有连接到任何小部件,但为什么,我已经将它连接到了:

data = (TextView)findViewById(R.id.vystup);

所以,拜托,你能帮帮我......我错了什么? 感谢

更新

这是我的logcat:

04-01 21:09:00.198: W/dalvikvm(12575): threadid=1: thread exiting with uncaught exception (group=0x4112d9a8)
04-01 21:09:00.201: E/AndroidRuntime(12575): FATAL EXCEPTION: main
04-01 21:09:00.201: E/AndroidRuntime(12575): java.lang.RuntimeException: Unable to start activity ComponentInfo{cz.pavel.testovacisettext/cz.pavel.testovacisettext.MainActivity}: java.lang.NullPointerException
04-01 21:09:00.201: E/AndroidRuntime(12575):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
04-01 21:09:00.201: E/AndroidRuntime(12575):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
04-01 21:09:00.201: E/AndroidRuntime(12575):    at android.app.ActivityThread.access$600(ActivityThread.java:156)
04-01 21:09:00.201: E/AndroidRuntime(12575):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340) 
04-01 21:09:00.201: E/AndroidRuntime(12575):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-01 21:09:00.201: E/AndroidRuntime(12575):    at android.os.Looper.loop(Looper.java:153)
04-01 21:09:00.201: E/AndroidRuntime(12575):    at android.app.ActivityThread.main(ActivityThread.java:5299)
04-01 21:09:00.201: E/AndroidRuntime(12575):    at java.lang.reflect.Method.invokeNative(Native Method)
04-01 21:09:00.201: E/AndroidRuntime(12575):    at java.lang.reflect.Method.invoke(Method.java:511)
04-01 21:09:00.201: E/AndroidRuntime(12575):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
04-01 21:09:00.201: E/AndroidRuntime(12575):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
04-01 21:09:00.201: E/AndroidRuntime(12575):    at dalvik.system.NativeStart.main(Native Method)
04-01 21:09:00.201: E/AndroidRuntime(12575): Caused by: java.lang.NullPointerException
04-01 21:09:00.201: E/AndroidRuntime(12575):    at cz.pavel.testovacisettext.MainActivity.onCreate(MainActivity.java:25)
04-01 21:09:00.201: E/AndroidRuntime(12575):    at android.app.Activity.performCreate(Activity.java:5122)
04-01 21:09:00.201: E/AndroidRuntime(12575):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
04-01 21:09:00.201: E/AndroidRuntime(12575):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)

我尝试删除此部分:

   if (savedInstanceState == null) {
    getFragmentManager().beginTransaction()
            .add(R.id.container, new PlaceholderFragment())
            .commit();
}

但问题仍然是一样的

更新2

这是完整的xml文件:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="cz.pavel.testovacisettext.MainActivity$PlaceholderFragment" >

<TextView
    android:id="@+id/vystup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="44dp"
    android:text="@string/vystup1"
    android:textAppearance="?android:attr/textAppearanceMedium" />

2 个答案:

答案 0 :(得分:0)

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    data = (TextView)findViewById(R.id.vystup);
    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }
    String pom = "AAA";
    data.setText(pom);
}

您的textView位于activity_main.xml中,但是代码的这一部分:

if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
如果我没有误会,

会将当前的活动内容视图替换为R.id.container 尝试删除那段代码,然后再试一次......

希望有所帮助

答案 1 :(得分:0)

更改行

String pom = "AAA";
data.setText(pom);
碎片交易前的

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    data = (TextView)findViewById(R.id.vystup);
    String pom = "AAA";
    data.setText(pom);
    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }
}

该交易将当前“contentView”从activity_main替换为R.id.container,但不包含Textview“数据”。