0

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?

Community
  • 1
  • 1
sacre
  • 15
  • 8
  • 1
    First, read: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this Then, edit your question to explain what your stack trace is showing you. Also, please explain where you think that you are using the XML that you list here. – CommonsWare Jun 22 '14 at 23:57
  • Hi. Thanks for the link :-) I've added the names of the xml & java files, and the LogCat... I'm also new to stackoverflow, I'm unfamiliar with the layout. I thought your reply was an auto response. Sorry :-) – sacre Jun 23 '14 at 19:00
  • Relevant: http://stackoverflow.com/questions/23653778/nullpointerexception-accessing-views-in-oncreate – laalto Jun 23 '14 at 19:18

1 Answers1

0

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

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • thanks for responding! :-) I apologize for lack of information. I'm learning to know what is important to provide. I am avoiding a long, long post like this :-) anyway, I have updated the xml files, and I don't understand where to put my TextView in the activity_main.xml, which is a FrameLayout. Mus I add the widget with the same @+id/hello_world as in fragment_main.xml? – sacre Jun 23 '14 at 19:20
  • @sacre: You need to decide whether the activity or the fragment will be managing this `TextView`. You then put the widget in the right layout based upon that decision, and you put your Java code for the `TextView` in the right class based upon that decision. – CommonsWare Jun 23 '14 at 19:25
  • you are both having me derailed and making great sense... You suggest `TextView` must move to `FrameLayout` to use `MainActivity` in java, **OR** move `textView.setText(message)` to `PlaceholderFragment extends fragment { PlaceholderFragment () { onCreateView() { textView.SetText(...) }}}` – sacre Jun 23 '14 at 19:45
  • I can't move my logic into the static PlaceholderFragment, it complains :-( What is an appropriate place in the default `MainActivity.java`? I'm curious about this option... In the meanwhile I'll try moving TextView to activity_main.xml – sacre Jun 23 '14 at 20:03
  • Thank you!! :-D `textView.setText("string")` run great when `` is in `` in activity_main.xml – sacre Jun 23 '14 at 20:23