Android app error "Unfortunately App has Stopped"

19,893

The problem is here:

com.example.thebasicseries.menu.onCreate(menu.java:35)

You're getting a NullPointerException.

Seems like you have no Tutorial2 button in your layout XML.

Share:
19,893

Related videos on Youtube

MariaZ
Author by

MariaZ

Updated on June 04, 2022

Comments

  • MariaZ
    MariaZ about 2 years

    I am learning android development and I am following a tutorial on youtube, and I am trying to run this code and I keep getting "Unfortunately App has Stopped", it looks that the problem is when i start the Activity "startActivity(menuIntent);" this is the MainActivity.java:

    package com.example.thebasicseries;
    import android.media.MediaPlayer;
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    
    
    public class MainActivity extends Activity {
    
    MediaPlayer logoMusic;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
    
        logoMusic = MediaPlayer.create(MainActivity.this, R.raw.sound);
        logoMusic.start();
    
        Thread logoTimer = new Thread(){
            public void run(){
                try {
                    sleep(5000);
                    Intent menuIntent = new Intent("com.example.thebasicseries.menu");
                    startActivity(menuIntent);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } finally{
                    finish();
                }
            }
        };
        logoTimer.start();
    }
    
    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        logoMusic.release();
    }
    
    
     }
    

    And this is the menu.java

     package com.example.thebasicseries;
     import android.app.Activity;
     import android.content.Intent;
     import android.media.MediaPlayer;
     import android.os.Bundle;
     import android.view.View;
     import android.widget.Button;
    
     public class menu extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        //Button sound
        final MediaPlayer buttonSound = MediaPlayer.create(menu.this, R.raw.button);
    
        //Setting up the button references
        Button tut1 = (Button) findViewById(R.id.tutorial1);
        Button tut2 = (Button) findViewById(R.id.tutorial2);
    
        tut1.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                buttonSound.start();
                startActivity(new Intent("com.example.thebasicseries.TutorialOne"));
            }
        });     
    
    
        tut2.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                buttonSound.start();
                startActivity(new Intent("com.example.thebasicseries.TutorialOne"));
            }
        });     
    
    
    
    
    }
    
    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }
    
      }
    

    This is the manifest:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.thebasicseries"
    android:versionCode="1"
    android:versionName="1.0" >
    
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.thebasicseries.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
         <activity
            android:name="com.example.thebasicseries.menu"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.example.thebasicseries.menu" />
    
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.thebasicseries.TutorialOne"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.example.thebasicseries.TutorialOne" />
    
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>
    
    </manifest>
    

    The error log is the following:

         04-20 11:13:29.780: E/AndroidRuntime(820): FATAL EXCEPTION: main
         04-20 11:13:29.780: E/AndroidRuntime(820): java.lang.RuntimeException: Unable to start   activity ComponentInfo{com.example.thebasicseries/com.example.thebasicseries.menu}: java.lang.NullPointerException
         04-20 11:13:29.780: E/AndroidRuntime(820):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
         04-20 11:13:29.780: E/AndroidRuntime(820):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
        04-20 11:13:29.780: E/AndroidRuntime(820):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
       04-20 11:13:29.780: E/AndroidRuntime(820):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
       04-20 11:13:29.780: E/AndroidRuntime(820):   at android.os.Handler.dispatchMessage(Handler.java:99)
       04-20 11:13:29.780: E/AndroidRuntime(820):   at android.os.Looper.loop(Looper.java:137)
       04-20 11:13:29.780: E/AndroidRuntime(820):   at android.app.ActivityThread.main(ActivityThread.java:5039)
       04-20 11:13:29.780: E/AndroidRuntime(820):   at java.lang.reflect.Method.invokeNative(Native Method)
       04-20 11:13:29.780: E/AndroidRuntime(820):   at java.lang.reflect.Method.invoke(Method.java:511)
       04-20 11:13:29.780: E/AndroidRuntime(820):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
       04-20 11:13:29.780: E/AndroidRuntime(820):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
       04-20 11:13:29.780: E/AndroidRuntime(820):   at dalvik.system.NativeStart.main(Native Method)
       04-20 11:13:29.780: E/AndroidRuntime(820): Caused by: java.lang.NullPointerException
        04-20 11:13:29.780: E/AndroidRuntime(820):  at com.example.thebasicseries.menu.onCreate(menu.java:35)
        04-20 11:13:29.780: E/AndroidRuntime(820):  at android.app.Activity.performCreate(Activity.java:5104)
        04-20 11:13:29.780: E/AndroidRuntime(820):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
        04-20 11:13:29.780: E/AndroidRuntime(820):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
        04-20 11:13:29.780: E/AndroidRuntime(820):  ... 11 more
        04-20 11:13:33.220: I/Process(820): Sending signal. PID: 820 SIG: 9
    

    Thanks!! `

    • Sankar V
      Sankar V about 11 years
      complete stacktrace from logcat please? post your manifest file code.
    • Glenn
      Glenn about 11 years
      Post the stacktrace which is 'red highlighted', that might be your error.
    • Alexander Kulyakhtin
      Alexander Kulyakhtin about 11 years
      You should figure that out yourself as a part of your learning
    • MariaZ
      MariaZ about 11 years
      Hey Alex, if i come here to ask this question is because I had a haard time to find out an answer....
    • Sankar V
      Sankar V about 11 years
      could you please format your logcat code?
    • dumbfingers
      dumbfingers about 11 years
      what's in the line 35 of menu.java?
    • MariaZ
      MariaZ about 11 years
      I am sorry, I got a little confused it seems that some else did it
    • MariaZ
      MariaZ about 11 years
      line 35 is tut2.setOnClickListener(new View.OnClickListener() {