簡體   English   中英

我的Android應用意外崩潰

[英]My Android app crashes Unexpectedly

我是Android編程的初學者。 我正在通過新的波士頓視頻學習它。 但是,每次我啟動我的應用程序時,它都會崩潰“不幸的是,應用程序停止了工作”。但是,當我單擊“確定”時,它將帶我進入應用程序中的第二個活動。 我不知道為什么會這樣。

這是我的Android清單:

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />

<uses-permission android:name="android.permission.SET_WALLPAPER" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".Splash"
        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=".StartingPoint"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.skm.firstapp.STARTINGPOINT" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Menu"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.skm.firstapp.MENU" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
     <activity
        android:name=".Prefs"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.skm.firstapp.PREFS" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name=".TextPlay"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name=".Email"
        android:label="@string/app_name" >
    </activity>
    <activity
        android:name=".Camera"
        android:label="Camera Application"
        android:screenOrientation="portrait" >
    </activity>
    <activity
        android:name=".Data"
        android:label="Data App"
        android:screenOrientation="portrait" >
    </activity>
    <activity
        android:name=".OpenedClass"
        android:label="Data App"
        android:screenOrientation="portrait" >
    </activity>
    <activity
        android:name=".AboutUs"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Dialog" >
        <intent-filter>
            <action android:name="com.skm.firstapp.ABOUT" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>

</manifest>

這是我的日志:

09-07 05:27:26.522: W/dalvikvm(1032): threadid=1: thread exiting with uncaught exception (group=0xb2b05ba8)
09-07 05:27:26.532: E/AndroidRuntime(1032): FATAL EXCEPTION: main
09-07 05:27:26.532: E/AndroidRuntime(1032): Process: com.skm.firstapp, PID: 1032
09-07 05:27:26.532: E/AndroidRuntime(1032): java.lang.RuntimeException: Unable to pause activity {com.skm.firstapp/com.skm.firstapp.Splash}: java.lang.NullPointerException
09-07 05:27:26.532: E/AndroidRuntime(1032):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3062)
09-07 05:27:26.532: E/AndroidRuntime(1032):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3017)
09-07 05:27:26.532: E/AndroidRuntime(1032):     at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2995)
09-07 05:27:26.532: E/AndroidRuntime(1032):     at android.app.ActivityThread.access$1000(ActivityThread.java:135)
09-07 05:27:26.532: E/AndroidRuntime(1032):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207)
09-07 05:27:26.532: E/AndroidRuntime(1032):     at android.os.Handler.dispatchMessage(Handler.java:102)
09-07 05:27:26.532: E/AndroidRuntime(1032):     at android.os.Looper.loop(Looper.java:136)
09-07 05:27:26.532: E/AndroidRuntime(1032):     at android.app.ActivityThread.main(ActivityThread.java:5017)
09-07 05:27:26.532: E/AndroidRuntime(1032):     at java.lang.reflect.Method.invokeNative(Native Method)
09-07 05:27:26.532: E/AndroidRuntime(1032):     at java.lang.reflect.Method.invoke(Method.java:515)
09-07 05:27:26.532: E/AndroidRuntime(1032):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-07 05:27:26.532: E/AndroidRuntime(1032):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-07 05:27:26.532: E/AndroidRuntime(1032):     at dalvik.system.NativeStart.main(Native Method)
09-07 05:27:26.532: E/AndroidRuntime(1032): Caused by: java.lang.NullPointerException
09-07 05:27:26.532: E/AndroidRuntime(1032):     at com.skm.firstapp.Splash.onPause(Splash.java:38)
09-07 05:27:26.532: E/AndroidRuntime(1032):     at android.app.Activity.performPause(Activity.java:5335)
09-07 05:27:26.532: E/AndroidRuntime(1032):     at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1233)
09-07 05:27:26.532: E/AndroidRuntime(1032):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3048)
09-07 05:27:26.532: E/AndroidRuntime(1032):     ... 12 more

這是我的Splash.java文件:

package com.skm.firstapp;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;

public class Splash extends Activity {

MediaPlayer ourSong;
@Override
protected void onCreate(Bundle LW) {
    // TODO Auto-generated method stub
    super.onCreate(LW);
    setContentView(R.layout.splash);
    MediaPlayer ourSong = MediaPlayer.create(Splash.this, R.raw.drum);
    ourSong.start();
    Thread timer = new Thread(){
        public void run(){
            try{
                sleep(1000);

            }catch(InterruptedException e){
                e.printStackTrace();
            }finally{
                Intent ourIntent = new Intent("com.skm.firstapp.MENU");
                startActivity(ourIntent);
            }
        }
    };
    timer.start();
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    ourSong.release();
    finish();
}

按照您提供的日志貓:在以下位置獲取“ NullPointerException”:檢查此行:

at com.skm.firstapp.Splash.onPause(Splash.java:38)

因此,我認為您應該發布MediaPlayer資源。 但是,如果尚未初始化,則會得到NullPointerException。

因此,請在onCreate()方法中初始化資源。

PS:共享完整的logcat文件,還共享Splash.java

:在添加splash.java之后:

MediaPlayer ourSong;
@Override
protected void onCreate(Bundle LW) {
    // TODO Auto-generated method stub
    super.onCreate(LW);
    setContentView(R.layout.splash);
    MediaPlayer ourSong = MediaPlayer.create(Splash.this, R.raw.drum);
    ourSong.start();

您需要將ourSong聲明為已完成的實例變量。

但您也已在onCreate()方法中將其聲明為局部變量,如以下行所示:

MediaPlayer ourSong = MediaPlayer.create(Splash.this, R.raw.drum);

因此將其更改為:

ourSong = MediaPlayer.create(Splash.this, R.raw.drum);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM