I'm very new to Android. This is a couple of lines added to the MyFirstApp guide referenced in the Eclipse ADT. I'm just trying to change the text in a TextView. I'm embarrassed that I've been at this for hours now and still can't find a solution. Any help is appreciated :-)

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
有帮助吗?

解决方案

The TextView is in layout fragment_main but you've only inflated activity_main with setContentView(). The view you're trying to find is not in the activity view hierarchy.

Possible solutions:

  • Move the TextView to your activity layout.

  • Move the code accessing the TextView to the fragment's onCreateView().

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top