错误“无法实例化活动”

时间:2012-08-01 13:13:30

标签: android

我知道有很多人有这个问题。我已经尝试了很多方法,但是仍然会遇到错误。

  1. 只有一项活动而且已经注册。
  2. 尝试通过com.hermione.woodenfish.MainActivity完成.MainActivity,没有用
  3. 清理和重建不起作用
  4. 真的需要帮助,谢谢!!

    logcat的:

    08-01 12:09:33.944: E/Trace(1032): error opening trace file: No such file or directory (2)
    08-01 12:09:34.384: D/AndroidRuntime(1032): Shutting down VM
    08-01 12:09:34.424: W/dalvikvm(1032): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
    08-01 12:09:34.465: E/AndroidRuntime(1032): FATAL EXCEPTION: main
    08-01 12:09:34.465: E/AndroidRuntime(1032): java.lang.RuntimeException: Unable to     instantiate activity   ComponentInfo{com.hermione.woodenfish/com.hermione.woodenfish.MainActivity}: java.lang.NullPointerException
    08-01 12:09:34.465: E/AndroidRuntime(1032):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
    08-01 12:09:34.465: E/AndroidRuntime(1032):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
    08-01 12:09:34.465: E/AndroidRuntime(1032):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
    08-01 12:09:34.465: E/AndroidRuntime(1032):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
    08-01 12:09:34.465: E/AndroidRuntime(1032):     at android.os.Handler.dispatchMessage(Handler.java:99)
    08-01 12:09:34.465: E/AndroidRuntime(1032):     at android.os.Looper.loop(Looper.java:137)
    08-01 12:09:34.465: E/AndroidRuntime(1032):     at android.app.ActivityThread.main(ActivityThread.java:4745)
    08-01 12:09:34.465: E/AndroidRuntime(1032):     at java.lang.reflect.Method.invokeNative(Native Method)
    08-01 12:09:34.465: E/AndroidRuntime(1032):     at java.lang.reflect.Method.invoke(Method.java:511)
    08-01 12:09:34.465: E/AndroidRuntime(1032):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    08-01 12:09:34.465: E/AndroidRuntime(1032):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    08-01 12:09:34.465: E/AndroidRuntime(1032):     at dalvik.system.NativeStart.main(Native Method)
    08-01 12:09:34.465: E/AndroidRuntime(1032): Caused by: java.lang.NullPointerException
    08-01 12:09:34.465: E/AndroidRuntime(1032):     at android.content.ContextWrapper.getResources(ContextWrapper.java:81)
    08-01 12:09:34.465: E/AndroidRuntime(1032):     at android.media.MediaPlayer.create(MediaPlayer.java:824)
    08-01 12:09:34.465: E/AndroidRuntime(1032):     at com.hermione.woodenfish.MainActivity.<init>(MainActivity.java:24)
    08-01 12:09:34.465: E/AndroidRuntime(1032):     at java.lang.Class.newInstanceImpl(Native Method)
    08-01 12:09:34.465: E/AndroidRuntime(1032):     at java.lang.Class.newInstance(Class.java:1319)
    08-01 12:09:34.465: E/AndroidRuntime(1032):     at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
    08-01 12:09:34.465: E/AndroidRuntime(1032):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
    08-01 12:09:34.465: E/AndroidRuntime(1032):     ... 11 more
    08-01 12:33:02.784: E/Trace(1171): error opening trace file: No such file or directory (2)
    08-01 12:33:03.125: D/AndroidRuntime(1171): Shutting down VM
    08-01 12:33:03.125: W/dalvikvm(1171): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
    08-01 12:33:03.154: E/AndroidRuntime(1171): FATAL EXCEPTION: main
    08-01 12:33:03.154: E/AndroidRuntime(1171): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.hermione.woodenfish/com.hermione.woodenfish.MainActivity}: java.lang.NullPointerException
    08-01 12:33:03.154: E/AndroidRuntime(1171):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
    08-01 12:33:03.154: E/AndroidRuntime(1171):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
    08-01 12:33:03.154: E/AndroidRuntime(1171):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
    08-01 12:33:03.154: E/AndroidRuntime(1171):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
    08-01 12:33:03.154: E/AndroidRuntime(1171):     at android.os.Handler.dispatchMessage(Handler.java:99)
    08-01 12:33:03.154: E/AndroidRuntime(1171):     at android.os.Looper.loop(Looper.java:137)
    08-01 12:33:03.154: E/AndroidRuntime(1171):     at android.app.ActivityThread.main(ActivityThread.java:4745)
    08-01 12:33:03.154: E/AndroidRuntime(1171):     at java.lang.reflect.Method.invokeNative(Native Method)
    08-01 12:33:03.154: E/AndroidRuntime(1171):     at java.lang.reflect.Method.invoke(Method.java:511)
    08-01 12:33:03.154: E/AndroidRuntime(1171):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    08-01 12:33:03.154: E/AndroidRuntime(1171):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    08-01 12:33:03.154: E/AndroidRuntime(1171):     at dalvik.system.NativeStart.main(Native Method)
    08-01 12:33:03.154: E/AndroidRuntime(1171): Caused by: java.lang.NullPointerException
    08-01 12:33:03.154: E/AndroidRuntime(1171):     at android.content.ContextWrapper.getResources(ContextWrapper.java:81)
    08-01 12:33:03.154: E/AndroidRuntime(1171):     at android.media.MediaPlayer.create(MediaPlayer.java:824)
    08-01 12:33:03.154: E/AndroidRuntime(1171):     at com.hermione.woodenfish.MainActivity.<init>(MainActivity.java:23)
    08-01 12:33:03.154: E/AndroidRuntime(1171):     at java.lang.Class.newInstanceImpl(Native Method)
    08-01 12:33:03.154: E/AndroidRuntime(1171):     at java.lang.Class.newInstance(Class.java:1319)
    08-01 12:33:03.154: E/AndroidRuntime(1171):     at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
    08-01 12:33:03.154: E/AndroidRuntime(1171):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
    08-01 12:33:03.154: E/AndroidRuntime(1171):     ... 11 more
    

    MainActivity.java

    package com.hermione.woodenfish;
    
    import android.app.Activity;
    import android.media.MediaPlayer;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.View;
    
    public class MainActivity extends Activity {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }
    
        private MediaPlayer mp = MediaPlayer.create(this, R.raw.knok);
        public void sendKnok(View view) {
            if (mp.isPlaying()){
                mp.stop();
            }
            mp.start();
        }
    }
    

    清单:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.hermione.woodenfish"
        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>
    

2 个答案:

答案 0 :(得分:1)

我认为问题出在这一行,

 private MediaPlayer mp = MediaPlayer.create(this, R.raw.knok);

您已尝试在onCreate()之前初始化Context对象。这可能会造成问题。你怎么样在你的onCreate()中初始化你的MediaPlayer

private MediaPlayer mp =null;  //Global Declaration

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    mp = MediaPlayer.create(this, R.raw.knok);
}

答案 1 :(得分:0)

你的问题在这里:

08-01 12:33:03.154: E/AndroidRuntime(1171): Caused by: java.lang.NullPointerException 08-01 12:33:03.154: E/AndroidRuntime(1171): at android.content.ContextWrapper.getResources(ContextWrapper.java:81) 08-01 12:33:03.154: E/AndroidRuntime(1171): at android.media.MediaPlayer.create(MediaPlayer.java:824) 08-01 12:33:03.154: E/AndroidRuntime(1171): at com.hermione.woodenfish.MainActivity.<init>(MainActivity.java:23) 08-01 12:33:03.154: E/AndroidRuntime(1171): at java.lang.Class.newInstanceImpl(Native Method) 08-01 12:33:03.154: E/AndroidRuntime(1171): at java.lang.Class.newInstance(Class.java:1319) 08-01 12:33:03.154: E/AndroidRuntime(1171): at android.app.Instrumentation.newActivity(Instrumentation.java:1053) 08-01 12:33:03.154: E/AndroidRuntime(1171): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974) 08-01 12:33:03.154: E/AndroidRuntime(1171): ... 11 more

你得到NullPointerException。在调试中查看MainActivity中的第23行,看看发生了什么。