2014-11-03 89 views
0

我在我的項目中找不到問題。我打開一個帶有四個按鈕的菜單,他們應該將用戶帶到其他活動中,但他們不會。當你按任何按鈕時,應用程序崩潰。主要非法狀態異常

這裏是我的菜單類

package com.example.androidintermediate; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 

public class Menu extends Activity implements OnClickListener { 

    String activities[] = { "ACTIVITYMAIN", "SETTINGS", "INTERNALSTORE", 
      "READING" }; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.menu); 

    } 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     int id = v.getId(); 
     for (int i = 0; i < activities.length; i++) { 
      if (id == getResources().getIdentifier("b" + i, "id", 
        "com.example.androidintermediate")) { 
       Intent intent = new Intent("com.example.androidintermediate." 
         + activities[i]); 
       startActivity(intent); 
      } 
     } 

    } 
} 

logcat的

11-03 20:02:54.927: D/OpenGLRenderer(1384): Enabling debug mode 0 
11-03 20:02:56.267: D/AndroidRuntime(1384): Shutting down VM 
11-03 20:02:56.267: W/dalvikvm(1384): threadid=1: thread exiting with uncaught exception (group=0xa6178908) 
11-03 20:02:56.271: E/AndroidRuntime(1384): FATAL EXCEPTION: main 
11-03 20:02:56.271: E/AndroidRuntime(1384): java.lang.IllegalStateException: Could not execute method of the activity 
11-03 20:02:56.271: E/AndroidRuntime(1384):  at android.view.View$1.onClick(View.java:3599) 
11-03 20:02:56.271: E/AndroidRuntime(1384):  at android.view.View.performClick(View.java:4204) 
11-03 20:02:56.271: E/AndroidRuntime(1384):  at android.view.View$PerformClick.run(View.java:17355) 
11-03 20:02:56.271: E/AndroidRuntime(1384):  at android.os.Handler.handleCallback(Handler.java:725) 
11-03 20:02:56.271: E/AndroidRuntime(1384):  at android.os.Handler.dispatchMessage(Handler.java:92) 
11-03 20:02:56.271: E/AndroidRuntime(1384):  at android.os.Looper.loop(Looper.java:137) 
11-03 20:02:56.271: E/AndroidRuntime(1384):  at android.app.ActivityThread.main(ActivityThread.java:5041) 
11-03 20:02:56.271: E/AndroidRuntime(1384):  at java.lang.reflect.Method.invokeNative(Native Method) 
11-03 20:02:56.271: E/AndroidRuntime(1384):  at java.lang.reflect.Method.invoke(Method.java:511) 
11-03 20:02:56.271: E/AndroidRuntime(1384):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
11-03 20:02:56.271: E/AndroidRuntime(1384):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
11-03 20:02:56.271: E/AndroidRuntime(1384):  at dalvik.system.NativeStart.main(Native Method) 
11-03 20:02:56.271: E/AndroidRuntime(1384): Caused by: java.lang.reflect.InvocationTargetException 
11-03 20:02:56.271: E/AndroidRuntime(1384):  at java.lang.reflect.Method.invokeNative(Native Method) 
11-03 20:02:56.271: E/AndroidRuntime(1384):  at java.lang.reflect.Method.invoke(Method.java:511) 
11-03 20:02:56.271: E/AndroidRuntime(1384):  at android.view.View$1.onClick(View.java:3594) 
11-03 20:02:56.271: E/AndroidRuntime(1384):  ... 11 more 
11-03 20:02:56.271: E/AndroidRuntime(1384): Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.example.androidintermediate.ACTIVITYMAIN } 
11-03 20:02:56.271: E/AndroidRuntime(1384):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1622) 
11-03 20:02:56.271: E/AndroidRuntime(1384):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417) 
11-03 20:02:56.271: E/AndroidRuntime(1384):  at android.app.Activity.startActivityForResult(Activity.java:3370) 
11-03 20:02:56.271: E/AndroidRuntime(1384):  at android.app.Activity.startActivityForResult(Activity.java:3331) 
11-03 20:02:56.271: E/AndroidRuntime(1384):  at android.app.Activity.startActivity(Activity.java:3566) 
11-03 20:02:56.271: E/AndroidRuntime(1384):  at android.app.Activity.startActivity(Activity.java:3534) 
11-03 20:02:56.271: E/AndroidRuntime(1384):  at com.example.androidintermediate.Menu.onClick(Menu.java:31) 
11-03 20:02:56.271: E/AndroidRuntime(1384):  ... 14 more 
11-03 20:02:58.603: I/Process(1384): Sending signal. PID: 1384 SIG: 9 

艙單

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.androidintermediate" 
    android:versionCode="1" 
    android:versionName="1.0" > 

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

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".Menu" 
      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=".MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.ACTIVITYMAIN" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".Settings" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.SETTINGS" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".InternalStorage" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.INTERNALSTORAGE" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".Reading" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.READING" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".Numbers" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.NUMBERS" /> 

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

</manifest> 
+0

您正在混合的意圖組件和行動領域。請注意清單中的動作'android:name =「android.intent.action.ACTIVITYMAIN」'與錯誤日誌 – 2014-11-03 20:31:45

+0

中的'act = com.example.androidintermediate.ACTIVITYMAIN'完全匹配。非常感謝你的克里斯。 – 2014-11-03 21:09:26

回答

0

當您創建的意圖你不拼寫出完整的包名。這樣做:

Intent intent = new Intent(Menu.this, YourActivity.class); 
+0

你也可以這樣做,因爲我也是。不管怎樣,謝謝你。 – 2014-11-03 21:10:12