TextView setText上的NullPointerException

时间:2014-03-22 18:10:43

标签: android eclipse nullpointerexception textview settext

我对Android非常陌生。这是添加到Eclipse ADT中引用的MyFirstApp指南的几行。我只想尝试更改TextView中的文本。我很尴尬,我已经在这里工作了好几个小时,仍然无法找到解决方案。任何帮助表示赞赏: - )

MainActivity.Java

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView objTV = (TextView)this.findViewById(R.id.tv_id);
        objTV.setText("test");

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

fragment_main.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="com.example.myfirstapp.MainActivity$PlaceholderFragment" >

<TextView
    android:id="@+id/tv_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

</RelativeLayout>

logcat的

03-22 13:44:36.793: E/AndroidRuntime(1710): FATAL EXCEPTION: main
03-22 13:44:36.793: E/AndroidRuntime(1710): Process: com.example.myfirstapp, PID: 1710
03-22 13:44:36.793: E/AndroidRuntime(1710): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.MainActivity}: java.lang.NullPointerException
03-22 13:44:36.793: E/AndroidRuntime(1710):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-22 13:44:36.793: E/AndroidRuntime(1710):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-22 13:44:36.793: E/AndroidRuntime(1710):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-22 13:44:36.793: E/AndroidRuntime(1710):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-22 13:44:36.793: E/AndroidRuntime(1710):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-22 13:44:36.793: E/AndroidRuntime(1710):     at android.os.Looper.loop(Looper.java:136)
03-22 13:44:36.793: E/AndroidRuntime(1710):     at android.app.ActivityThread.main(ActivityThread.java:5017)
03-22 13:44:36.793: E/AndroidRuntime(1710):     at java.lang.reflect.Method.invokeNative(Native Method)
03-22 13:44:36.793: E/AndroidRuntime(1710):     at java.lang.reflect.Method.invoke(Method.java:515)
03-22 13:44:36.793: E/AndroidRuntime(1710):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-22 13:44:36.793: E/AndroidRuntime(1710):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-22 13:44:36.793: E/AndroidRuntime(1710):     at dalvik.system.NativeStart.main(Native Method)
03-22 13:44:36.793: E/AndroidRuntime(1710): Caused by: java.lang.NullPointerException
03-22 13:44:36.793: E/AndroidRuntime(1710):     at com.example.myfirstapp.MainActivity.onCreate(MainActivity.java:21)
03-22 13:44:36.793: E/AndroidRuntime(1710):     at android.app.Activity.performCreate(Activity.java:5231)
03-22 13:44:36.793: E/AndroidRuntime(1710):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-22 13:44:36.793: E/AndroidRuntime(1710):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-22 13:44:36.793: E/AndroidRuntime(1710):     ... 11 more

1 个答案:

答案 0 :(得分:8)

TextView位于布局fragment_main,但您只使用activity_main虚张setContentView()。您尝试查找的视图不在活动视图层次结构中。

可能的解决方案:

  • TextView移至您的活动布局。

  • 将访问TextView的代码移至片段的onCreateView()