Search code examples
androidtextviewruntimesettext

Runtime error on textView.setText(message)... Why? :-(


I'm new to Android programming. I honestly can say, I have been searching for answers just about everywhere on the web. Please help.

Here is a stackoverflow link of a typical, and the nearest to what I want to achieve Change TextView text

My main problem is. Whenever I want to change the text of TextView object, I get a runtime error and the app is "force close". I am experience in OOP in C++ and PHP. I simply don't understand why the app terminate on textView.setText(message). Everything is defined. I even tried textView.setText("Hello"). whenever I remove this statement, the app run.

activity_main.xml (entire file)

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="MyApp.MainActivity"
    tools:ignore="MergeRootFrame" />

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="MyApp.MainActivity$PlaceholderFragment" >

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

</RelativeLayout>

MainActivity.java

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

    String message = "hi there";
    TextView textView = (TextView) findViewById(R.id.hello_world);
    textView.setText("this is a test");

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

// SKIPPED A FEW CODE

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
        return rootView;
    } // onCreateView
} // PlaceholderFragment

LogCat

06-23 20:11:42.106: W/dalvikvm(332): threadid=1: thread exiting with uncaught exception (group=0x40015560)
06-23 20:11:42.116: E/AndroidRuntime(332): FATAL EXCEPTION: main
06-23 20:11:42.116: E/AndroidRuntime(332): java.lang.RuntimeException: Unable to start activity ComponentInfo{MyApp/MyApp.MainActivity}: java.lang.NullPointerException
06-23 20:11:42.116: E/AndroidRuntime(332):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
06-23 20:11:42.116: E/AndroidRuntime(332):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-23 20:11:42.116: E/AndroidRuntime(332):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-23 20:11:42.116: E/AndroidRuntime(332):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-23 20:11:42.116: E/AndroidRuntime(332):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-23 20:11:42.116: E/AndroidRuntime(332):  at android.os.Looper.loop(Looper.java:123)
06-23 20:11:42.116: E/AndroidRuntime(332):  at android.app.ActivityThread.main(ActivityThread.java:3683)
06-23 20:11:42.116: E/AndroidRuntime(332):  at java.lang.reflect.Method.invokeNative(Native Method)
06-23 20:11:42.116: E/AndroidRuntime(332):  at java.lang.reflect.Method.invoke(Method.java:507)
06-23 20:11:42.116: E/AndroidRuntime(332):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-23 20:11:42.116: E/AndroidRuntime(332):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-23 20:11:42.116: E/AndroidRuntime(332):  at dalvik.system.NativeStart.main(Native Method)
06-23 20:11:42.116: E/AndroidRuntime(332): Caused by: java.lang.NullPointerException
06-23 20:11:42.116: E/AndroidRuntime(332):  at MyApp.MainActivity.onCreate(MainActivity.java:30)
06-23 20:11:42.116: E/AndroidRuntime(332):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-23 20:11:42.116: E/AndroidRuntime(332):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
06-23 20:11:42.116: E/AndroidRuntime(332):  ... 11 more
06-23 20:11:44.826: I/Process(332): Sending signal. PID: 332 SIG: 9

What is NullPointerException and uncaught exception?

Again, the app runs when I remove the setText() line, byt terminates when included. What am I doing wrong?


Solution

  • You are calling setContentView(R.layout.activity_main) in onCreate(). You have not provided the contents of res/layout/activity_main.xml in your question.

    However, your error indicates that res/layout/activity_main.xml does not contain a widget with android:id="@+id/hello_world".

    Your res/layout/fragment_main.xml file has such a widget, but you are not using that file in the code pasted into your question so far.

    Either:

    • Add a widget with the right ID to res/layout/activity_main.xml, or

    • Move your TextView logic to some place that is appropriate, based upon where you are trying to use res/layout/fragment_main.xml