Android application works fine in emulator but not

2019-08-27 03:06发布

问题:

I'm making a simple Music Player application. It already works perfectly on my emulator but after I install it on my device, it gives "The application MusicShare (process com.example.musicshare) has stopped unexpectedly. Please try again".

My device is a Galaxy S2 and I got the apk by taking it from the bin folder. I have tried using "Export w/o unsigned key and it cannot be installed instead. Here's the manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.musicshare"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".AndroidBuildingMusicPlayerActivity"
            android:label="@string/app_name"
            android:configChanges="keyboardHidden|orientation">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <activity
            android:name=".PlayListActivity" />
    </application>

</manifest>

Any help will be appreciated. Thanks!

EDIT:

Here's the error log from logcat after I debugged it on the device:

11-05 17:31:17.705: D/dalvikvm(6222): GC_EXTERNAL_ALLOC freed 44K, 53% free 2551K/5379K, external 0K/0K, paused 23ms
11-05 17:31:17.760: D/dalvikvm(6222): GC_EXTERNAL_ALLOC freed 11K, 53% free 2575K/5379K, external 466K/518K, paused 28ms
11-05 17:31:17.790: D/AndroidRuntime(6222): Shutting down VM
11-05 17:31:17.790: W/dalvikvm(6222): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
11-05 17:31:17.795: E/AndroidRuntime(6222): FATAL EXCEPTION: main
11-05 17:31:17.795: E/AndroidRuntime(6222): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.musicshare/com.example.musicshare.AndroidBuildingMusicPlayerActivity}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
11-05 17:31:17.795: E/AndroidRuntime(6222):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
11-05 17:31:17.795: E/AndroidRuntime(6222):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
11-05 17:31:17.795: E/AndroidRuntime(6222):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-05 17:31:17.795: E/AndroidRuntime(6222):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
11-05 17:31:17.795: E/AndroidRuntime(6222):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-05 17:31:17.795: E/AndroidRuntime(6222):     at android.os.Looper.loop(Looper.java:123)
11-05 17:31:17.795: E/AndroidRuntime(6222):     at android.app.ActivityThread.main(ActivityThread.java:3691)
11-05 17:31:17.795: E/AndroidRuntime(6222):     at java.lang.reflect.Method.invokeNative(Native Method)
11-05 17:31:17.795: E/AndroidRuntime(6222):     at java.lang.reflect.Method.invoke(Method.java:507)
11-05 17:31:17.795: E/AndroidRuntime(6222):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
11-05 17:31:17.795: E/AndroidRuntime(6222):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
11-05 17:31:17.795: E/AndroidRuntime(6222):     at dalvik.system.NativeStart.main(Native Method)
11-05 17:31:17.795: E/AndroidRuntime(6222): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
11-05 17:31:17.795: E/AndroidRuntime(6222):     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
11-05 17:31:17.795: E/AndroidRuntime(6222):     at java.util.ArrayList.get(ArrayList.java:311)
11-05 17:31:17.795: E/AndroidRuntime(6222):     at com.example.musicshare.AndroidBuildingMusicPlayerActivity.playSong(AndroidBuildingMusicPlayerActivity.java:273)
11-05 17:31:17.795: E/AndroidRuntime(6222):     at com.example.musicshare.AndroidBuildingMusicPlayerActivity.onCreate(AndroidBuildingMusicPlayerActivity.java:80)
11-05 17:31:17.795: E/AndroidRuntime(6222):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-05 17:31:17.795: E/AndroidRuntime(6222):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
11-05 17:31:17.795: E/AndroidRuntime(6222):     ... 11 more

回答1:

@Pleerock is correct. Apparently it was the problem with the path. It should be "/sdcard/media/audio/Music/". Thanks for the help.



回答2:

Sometimes you need to export the whole project and get the .apk file and install it