2011-02-06 38 views
1

我想創建一個非常簡單的自定義意圖示例。我搜索了這個錯誤,沒有任何論壇的答案適合我。下面是我的文件:Android ActivityNotFoundException

public class DemoImplicit extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 

    public void whenButtonIsClicked(View view) { 
     Intent intent = new Intent("com.example.action.NEW_ACTION"); //<<<<<<< 
     intent.addCategory("android.intent.category.DEFAULT");  //<<<<<<< 
//  Intent intent = new Intent("android.intent.action.VIEW"); 
//  intent.addCategory("com.example.MY_CATEGORY"); 
     startActivity(intent);          //<<<<<<< 
    } 
} 

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.demos" android:versionCode="1" 
    android:versionName="1.0"> 
    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
     <activity android:name=".SatisfyIntent" android:label="@string/app_name"> 

      <intent-filter> 
      <!-- action android:name="android.intent.action.VIEW"/--> 
      <!-- category android:name="com.example.MY_CATEGORY"/--> 
      <action android:name="com.example.action.NEW_ACTION" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 

    </application> 
    <uses-sdk android:minSdkVersion="9" /> 

</manifest> 

這兩個單獨的文件是在兩個不同的Eclipse項目,但我要確保包含startActivity調用文件加載到仿真器之前加載包含意圖過濾器在仿真器上的項目。無論如何,我總是得到一個ActivityNotFoundException。 我在做什麼錯?

P.S.下面是含有DemoImplicit.java項目AndroidManifest.xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="com.example.demos" 
     android:versionCode="1" 
     android:versionName="1.0"> 
    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
     <activity android:name=".DemoImplicit" 
        android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

    </application> 
    <uses-sdk android:minSdkVersion="9" /> 

</manifest> 

回答

4

Fisrtly你shuld確保AndroidManifest.xml文件必須在此定義了DemoImplicit活動。

由於這樣的:<activity android:name=".DemoImplicit"/>

另外,在您的代碼已經aspecified的SatisfyIntent因爲發射活動

<activity android:name=".SatisfyIntent" android:label="@string/app_name">

但這裏好像你在你的Java代碼這樣的事。

所以底線是:Activity你想運行的文件必須在你的AndroidManifest.xml文件中定義。

+0

我已經有兩個項目,所以我有兩個AndroidManifest.xml文件。我編輯的問題包括兩個。一個列出了一個啓動器的活動;另一個沒有。所以我不認爲這是問題。它可能是標識符和它的值之間的區別,比如com.example.demos.MY_ACTIVITY與值MY_NEW_ACTIVITY? – hwexler2 2011-02-06 16:46:12