-2

I am doing the Android Development Tutorial My First App. After doing the action bar class, I have problems running the app on my emulator. There are no errors on any of my files (java or xml). Yet, the very first red line in my logcat is "04-03 15:49:37.597: E/AndroidRuntime(1115): FATAL EXCEPTION: main". I have researched all previous posts about Fatal Exceptions, and have not found my answer yet. Please help me. My Main Activity java file is:

package com.example.myfirstapp2;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

public class MainActivity extends ActionBarActivity {
    public final static String EXTRA_MESSAGE = "com.example.myfirstapp2.MESSAGE";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_displaymessage);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        // If your miniSdkVersion is 11 or higher, instead use:
        // getActionBar().setDisplayHomeAsUpEnabled(true);

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_activity_actions, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int itemId = item.getItemId();
        if (itemId == R.id.action_search) {
            openSearch();
            return true;
        } else if (itemId == R.id.action_settings) {
            openSettings();
            return true;
        } else {
            return super.onOptionsItemSelected(item);
        }
    }

    private void openSettings() {
        // TODO Auto-generated method stub

    }

    private void openSearch() {
        // TODO Auto-generated method stub

    }

    /**
     * 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;
        }
    }

    /**Called when the user clicks the Send button */
    public void sendMessage(View view) {
        Intent intent = new Intent(this, DisplayMessageActivity.class);
        EditText editText = (EditText) findViewById(R.id.edit_message);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);
    }   


}

My Main Activity xml is:

<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="com.example.myfirstapp2.MainActivity"
tools:ignore="MergeRootFrame" />

My Display Message Activity Java file is:

package com.example.myfirstapp2;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class DisplayMessageActivity extends ActionBarActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Get the message from the intent
        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

        // Create the text view
        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);

        // Set the text view as the activity layout
        setContentView(textView);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * 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_display_message,
                    container, false);
            return rootView;
        }
    }

}

My fragment_display_message xml is:

<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.myfirstapp2.DisplayMessageActivity$PlaceholderFragment" >

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

</RelativeLayout>

My activity_display_message xml is:

<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="com.example.myfirstapp2.DisplayMessageActivity"
tools:ignore="MergeRootFrame" />

My fragment_main xml is:

<LinearLayout 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:orientation="horizontal" >

<EditText android:id="@+id/edit_message"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:hint="@string/edit_message"/>

<Button 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_send"
    android:onClick="sendMessage" />    

</LinearLayout>

My logcat is:

04-03 15:49:37.577: D/AndroidRuntime(1115): Shutting down VM
04-03 15:49:37.577: W/dalvikvm(1115): threadid=1: thread exiting with uncaught exception      (group=0xb1abdba8)
04-03 15:49:37.597: E/AndroidRuntime(1115): FATAL EXCEPTION: main
04-03 15:49:37.597: E/AndroidRuntime(1115): Process: com.example.myfirstapp2, PID: 1115
04-03 15:49:37.597: E/AndroidRuntime(1115): java.lang.RuntimeException: Unable to start     activity ComponentInfo{com.example.myfirstapp2/com.example.myfirstapp2.MainActivity}:     java.lang.NullPointerException
04-03 15:49:37.597: E/AndroidRuntime(1115):     at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-03 15:49:37.597: E/AndroidRuntime(1115):     at     android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-03 15:49:37.597: E/AndroidRuntime(1115):     at     android.app.ActivityThread.access$800(ActivityThread.java:135)
04-03 15:49:37.597: E/AndroidRuntime(1115):     at     android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-03 15:49:37.597: E/AndroidRuntime(1115):     at     android.os.Handler.dispatchMessage(Handler.java:102)
04-03 15:49:37.597: E/AndroidRuntime(1115):     at android.os.Looper.loop(Looper.java:136)
04-03 15:49:37.597: E/AndroidRuntime(1115):     at     android.app.ActivityThread.main(ActivityThread.java:5017)
04-03 15:49:37.597: E/AndroidRuntime(1115):     at     java.lang.reflect.Method.invokeNative(Native Method)
04-03 15:49:37.597: E/AndroidRuntime(1115):     at     java.lang.reflect.Method.invoke(Method.java:515)
04-03 15:49:37.597: E/AndroidRuntime(1115):     at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-03 15:49:37.597: E/AndroidRuntime(1115):     at     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-03 15:49:37.597: E/AndroidRuntime(1115):     at dalvik.system.NativeStart.main(Native     Method)
04-03 15:49:37.597: E/AndroidRuntime(1115): Caused by: java.lang.NullPointerException
04-03 15:49:37.597: E/AndroidRuntime(1115):     at     android.view.ViewGroup.addViewInner(ViewGroup.java:3561)
04-03 15:49:37.597: E/AndroidRuntime(1115):     at     android.view.ViewGroup.addView(ViewGroup.java:3415)
04-03 15:49:37.597: E/AndroidRuntime(1115):     at     android.view.ViewGroup.addView(ViewGroup.java:3391)
04-03 15:49:37.597: E/AndroidRuntime(1115):     at     com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:309)
04-03 15:49:37.597: E/AndroidRuntime(1115):     at     com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:299)
04-03 15:49:37.597: E/AndroidRuntime(1115):     at     android.app.Activity.setContentView(Activity.java:1949)
04-03 15:49:37.597: E/AndroidRuntime(1115):     at     android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:220)
04-03 15:49:37.597: E/AndroidRuntime(1115):     at     android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDelegateICS.jav    a:106)
04-03 15:49:37.597: E/AndroidRuntime(1115):     at     android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:81)
04-03 15:49:37.597: E/AndroidRuntime(1115):     at     com.example.myfirstapp2.MainActivity.onCreate(MainActivity.java:20)
04-03 15:49:37.597: E/AndroidRuntime(1115):     at     android.app.Activity.performCreate(Activity.java:5231)
04-03 15:49:37.597: E/AndroidRuntime(1115):     at     android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-03 15:49:37.597: E/AndroidRuntime(1115):     at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-03 15:49:37.597: E/AndroidRuntime(1115):     ... 11 more
04-03 15:49:42.597: I/Process(1115): Sending signal. PID: 1115 SIG: 9
04-03 15:50:36.957: D/AndroidRuntime(1135): Shutting down VM
04-03 15:50:36.957: W/dalvikvm(1135): threadid=1: thread exiting with uncaught exception     (group=0xb1abdba8)
04-03 15:50:36.977: E/AndroidRuntime(1135): FATAL EXCEPTION: main
04-03 15:50:36.977: E/AndroidRuntime(1135): Process: com.example.myfirstapp2, PID: 1135
04-03 15:50:36.977: E/AndroidRuntime(1135): java.lang.RuntimeException: Unable to start     activity ComponentInfo{com.example.myfirstapp2/com.example.myfirstapp2.MainActivity}:     java.lang.NullPointerException
04-03 15:50:36.977: E/AndroidRuntime(1135):     at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-03 15:50:36.977: E/AndroidRuntime(1135):     at     android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-03 15:50:36.977: E/AndroidRuntime(1135):     at     android.app.ActivityThread.access$800(ActivityThread.java:135)
04-03 15:50:36.977: E/AndroidRuntime(1135):     at     android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-03 15:50:36.977: E/AndroidRuntime(1135):     at     android.os.Handler.dispatchMessage(Handler.java:102)
04-03 15:50:36.977: E/AndroidRuntime(1135):     at android.os.Looper.loop(Looper.java:136)
04-03 15:50:36.977: E/AndroidRuntime(1135):     at     android.app.ActivityThread.main(ActivityThread.java:5017)
04-03 15:50:36.977: E/AndroidRuntime(1135):     at     java.lang.reflect.Method.invokeNative(Native Method)
04-03 15:50:36.977: E/AndroidRuntime(1135):     at     java.lang.reflect.Method.invoke(Method.java:515)
04-03 15:50:36.977: E/AndroidRuntime(1135):     at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-03 15:50:36.977: E/AndroidRuntime(1135):     at     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-03 15:50:36.977: E/AndroidRuntime(1135):     at dalvik.system.NativeStart.main(Native     Method)
04-03 15:50:36.977: E/AndroidRuntime(1135): Caused by: java.lang.NullPointerException
04-03 15:50:36.977: E/AndroidRuntime(1135):     at     android.view.ViewGroup.addViewInner(ViewGroup.java:3561)
04-03 15:50:36.977: E/AndroidRuntime(1135):     at     android.view.ViewGroup.addView(ViewGroup.java:3415)
04-03 15:50:36.977: E/AndroidRuntime(1135):     at     android.view.ViewGroup.addView(ViewGroup.java:3391)
04-03 15:50:36.977: E/AndroidRuntime(1135):     at     com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:309)
04-03 15:50:36.977: E/AndroidRuntime(1135):     at     com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:299)
04-03 15:50:36.977: E/AndroidRuntime(1135):     at     android.app.Activity.setContentView(Activity.java:1949)
04-03 15:50:36.977: E/AndroidRuntime(1135):     at     android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:220)
04-03 15:50:36.977: E/AndroidRuntime(1135):     at     android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDelegateICS.jav    a:106)
04-03 15:50:36.977: E/AndroidRuntime(1135):     at     android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:81)
04-03 15:50:36.977: E/AndroidRuntime(1135):     at     com.example.myfirstapp2.MainActivity.onCreate(MainActivity.java:20)
04-03 15:50:36.977: E/AndroidRuntime(1135):     at     android.app.Activity.performCreate(Activity.java:5231)
04-03 15:50:36.977: E/AndroidRuntime(1135):     at     android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-03 15:50:36.977: E/AndroidRuntime(1135):     at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-03 15:50:36.977: E/AndroidRuntime(1135):     ... 11 more
04-03 15:50:41.527: I/Process(1135): Sending signal. PID: 1135 SIG: 9
04-03 15:54:55.657: D/AndroidRuntime(1174): Shutting down VM
04-03 15:54:55.657: W/dalvikvm(1174): threadid=1: thread exiting with uncaught exception     (group=0xb1abdba8)
04-03 15:54:55.687: E/AndroidRuntime(1174): FATAL EXCEPTION: main
04-03 15:54:55.687: E/AndroidRuntime(1174): Process: com.example.myfirstapp2, PID: 1174
04-03 15:54:55.687: E/AndroidRuntime(1174): java.lang.RuntimeException: Unable to start     activity ComponentInfo{com.example.myfirstapp2/com.example.myfirstapp2.MainActivity}:     java.lang.NullPointerException
04-03 15:54:55.687: E/AndroidRuntime(1174):     at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-03 15:54:55.687: E/AndroidRuntime(1174):     at    android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-03 15:54:55.687: E/AndroidRuntime(1174):     at   04-03 15:54:55.687: E/AndroidRuntime(1174):    at     android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-03 15:54:55.687: E/AndroidRuntime(1174):     at     android.os.Handler.dispatchMessage(Handler.java:102)
04-03 15:54:55.687: E/AndroidRuntime(1174):     at android.os.Looper.loop(Looper.java:136)
04-03 15:54:55.687: E/AndroidRuntime(1174):     at     android.app.ActivityThread.main(ActivityThread.java:5017)
04-03 15:54:55.687: E/AndroidRuntime(1174):     at     java.lang.reflect.Method.invokeNative(Native Method)
04-03 15:54:55.687: E/AndroidRuntime(1174):     at     java.lang.reflect.Method.invoke(Method.java:515)
04-03 15:54:55.687: E/AndroidRuntime(1174):     at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-03 15:54:55.687: E/AndroidRuntime(1174):     at     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-03 15:54:55.687: E/AndroidRuntime(1174):     at     dalvik.system.NativeStart.main(Native Method)
04-03 15:54:55.687: E/AndroidRuntime(1174): Caused by:     java.lang.NullPointerException
04-03 15:54:55.687: E/AndroidRuntime(1174):     at     android.view.ViewGroup.addViewInner(ViewGroup.java:3561)
04-03 15:54:55.687: E/AndroidRuntime(1174):     at     android.view.ViewGroup.addView(ViewGroup.java:3415)
04-03 15:54:55.687: E/AndroidRuntime(1174):     at     android.view.ViewGroup.addView(ViewGroup.java:3391)
04-03 15:54:55.687: E/AndroidRuntime(1174):     at     com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:309)
04-03 15:54:55.687: E/AndroidRuntime(1174):     at     com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:299)
04-03 15:54:55.687: E/AndroidRuntime(1174):     at     android.app.Activity.setContentView(Activity.java:1949)
04-03 15:54:55.687: E/AndroidRuntime(1174):     at     android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:220    )
04-03 15:54:55.687: E/AndroidRuntime(1174):     at     android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDel    egateICS.java:106)
04-03 15:54:55.687: E/AndroidRuntime(1174):     at     android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:81)
04-03 15:54:55.687: E/AndroidRuntime(1174):     at     com.example.myfirstapp2.MainActivity.onCreate(MainActivity.java:20)
04-03 15:54:55.687: E/AndroidRuntime(1174):     at     android.app.Activity.performCreate(Activity.java:5231)
04-03 15:54:55.687: E/AndroidRuntime(1174):     at     android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-03 15:54:55.687: E/AndroidRuntime(1174):     at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-03 15:54:55.687: E/AndroidRuntime(1174):     ... 11 more
04-03 15:55:06.527: I/Process(1174): Sending signal. PID: 1174 SIG: 9

What must I do? When I run it on the emulator I get a message "Unfortunately MyFirstApp has stopped"

  • You have a NullPointerException coming from line 20. Which one is that? – thegrinner Apr 03 '14 at 20:59
  • 04-03 15:49:37.597: E/AndroidRuntime(1115): Caused by: java.lang.NullPointerException – user3495505 Apr 03 '14 at 21:10
  • Line 20 of your code, not the stack trace. If you read down from the NPE in logcat, you'll see the 10th line is `at com.example.myfirstapp2.MainActivity.onCreate(MainActivity.java:20) 04-03 15:49:37.597: E/AndroidRuntime(1115): at android.app.Activity.performCreate(Activity.java:5231)`. – thegrinner Apr 04 '14 at 12:00

1 Answers1

2
activity_display_message

is the name of XML file and

setContentView(R.layout.activity_displaymessage);

So there no underline between "display" and "message"

markubik
  • 646
  • 4
  • 9
  • I added an "_" between "display" and "message" so that it looks like this now: setContentView(R.layout.activity_display_message); app still crashes on opening. – user3495505 Apr 03 '14 at 21:07
  • But with the some report? First line of stack trace is not the important one usually, check the first line that refers to your code, not libs. In above example it would be: 04-03 15:49:37.597: E/AndroidRuntime(1115): at com.example.myfirstapp2.MainActivity.onCreate(MainActivity.java:20) – markubik Apr 03 '14 at 21:10
  • okay. I need to learn more about reading the logcat. I found this thread: http://stackoverflow.com/questions/6065258/how-to-interpret-logcat Are there any more helpful ones about readin logcat errors out there? – user3495505 Apr 03 '14 at 21:31
  • Yeah, link provides actually quite good answer. In LogCat you are looking for two things: kind of exception (usually first line or "Cause by" somewhere) and the first occurence (from the top) of your code. Because that is where it crasher. Earlier lines are about core Android classes, so we can't do anything with what is inside (and they are usually working well, it is our code that makes errors). Then look at that line (double click will redirect you there) and search for reasons of exception – markubik Apr 03 '14 at 21:46