0

有谁知道为什么这不起作用?它在 logcat 中给了我一个非常基本的错误,所以我想我不必添加它。

MainActivity.java:

package com.sydcul.badluckboris;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.ImageButton;

public class MainActivity extends Activity {

    ImageButton boris;
    MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.ouch);
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        boris = (ImageButton) findViewById(R.id.ibBoris);

        boris.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                mp.start();
            }
        });
        }
}

活动主.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" >

    <ImageButton
        android:id="@+id/ibBoris"
        android:layout_width="250dp"
        android:layout_height="300dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:contentDescription="@string/boris"
        android:scaleType="fitXY"
        android:src="@drawable/badluckboris2" />

</RelativeLayout>

和 AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sydcul.badluckboris"
    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"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

编辑:LogCat:

10-31 17:15:05.670: E/AndroidRuntime(6500): FATAL EXCEPTION: main
10-31 17:15:05.670: E/AndroidRuntime(6500): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.sydcul.badluckboris/com.sydcul.badluckboris.MainActivity}: java.lang.NullPointerException
10-31 17:15:05.670: E/AndroidRuntime(6500):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573)
10-31 17:15:05.670: E/AndroidRuntime(6500):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
10-31 17:15:05.670: E/AndroidRuntime(6500):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-31 17:15:05.670: E/AndroidRuntime(6500):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
10-31 17:15:05.670: E/AndroidRuntime(6500):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-31 17:15:05.670: E/AndroidRuntime(6500):     at android.os.Looper.loop(Looper.java:130)
10-31 17:15:05.670: E/AndroidRuntime(6500):     at android.app.ActivityThread.main(ActivityThread.java:3691)
10-31 17:15:05.670: E/AndroidRuntime(6500):     at java.lang.reflect.Method.invokeNative(Native Method)
10-31 17:15:05.670: E/AndroidRuntime(6500):     at java.lang.reflect.Method.invoke(Method.java:507)
10-31 17:15:05.670: E/AndroidRuntime(6500):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
10-31 17:15:05.670: E/AndroidRuntime(6500):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
10-31 17:15:05.670: E/AndroidRuntime(6500):     at dalvik.system.NativeStart.main(Native Method)
10-31 17:15:05.670: E/AndroidRuntime(6500): Caused by: java.lang.NullPointerException
10-31 17:15:05.670: E/AndroidRuntime(6500):     at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:100)
10-31 17:15:05.670: E/AndroidRuntime(6500):     at com.sydcul.badluckboris.MainActivity.<init>(MainActivity.java:12)
10-31 17:15:05.670: E/AndroidRuntime(6500):     at java.lang.Class.newInstanceImpl(Native Method)
10-31 17:15:05.670: E/AndroidRuntime(6500):     at java.lang.Class.newInstance(Class.java:1409)
10-31 17:15:05.670: E/AndroidRuntime(6500):     at android.app.Instrumentation.newActivity(Instrumentation.java:1024)
10-31 17:15:05.670: E/AndroidRuntime(6500):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565)
10-31 17:15:05.670: E/AndroidRuntime(6500):     ... 11 more
10-31 17:15:05.680: E/(1749): Dumpstate > /data/log/dumpstate_app_error
10-31 17:15:05.680: W/ActivityManager(1749):   Force finishing activity com.sydcul.badluckboris/.MainActivity

提前致谢。值得注意的是,我是 Android 的菜鸟。做了很多VB.NET。

4

1 回答 1

2

错误更容易。我所看到的纯粹猜测是您从未准备过 MediaPlayer,但我不确定。看到在 onCreate 之外设置的 MediaPlayer 也有点奇怪,可能当时上下文还没有完全有效,但我不知道这是否真的会导致问题。

于 2012-10-31T16:10:30.853 回答