2011-02-06 103 views
3

我開始使用android網站上的記事本示例。我所做的是向數據庫中添加另一個表,並在NotePad類中添加另一個表(增加了另一個名爲Table的內部類,擴展了BaseColumns)。看起來數據庫正在被正確讀取,並且我可以直接啓動我的視圖。但是,當我嘗試以與谷歌啓動NoteEditor(通過ACTION_INSERT意圖)相同的方式啓動它時,應用程序崩潰。活動未發現異常

我已經更新了新表的URI,並且我想要啓動的活動的意圖過濾器位於清單(具有適當的mimetype)中。但是,它似乎無法找到並啓動我想要的活動。

這裏的輸出是相關的:

02-04 01:13:54.964: DEBUG/AndroidRuntime(342): Shutting down VM 
02-04 01:13:54.964: WARN/dalvikvm(342): threadid=1: thread exiting with uncaught exception (group=0x40015560) 
02-04 01:13:54.984: ERROR/AndroidRuntime(342): FATAL EXCEPTION: main 
**02-04 01:13:54.984: ERROR/AndroidRuntime(342): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.INSERT dat=content://com.google.provider.NotePad/courses }** 
02-04 01:13:54.984: ERROR/AndroidRuntime(342):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1408) 
02-04 01:13:54.984: ERROR/AndroidRuntime(342):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378) 
02-04 01:13:54.984: ERROR/AndroidRuntime(342):  at android.app.Activity.startActivityForResult(Activity.java:2827) 
02-04 01:13:54.984: ERROR/AndroidRuntime(342):  at android.app.Activity.startActivity(Activity.java:2933) 
02-04 01:13:54.984: ERROR/AndroidRuntime(342):  at com.example.android.notepad.CoursesList.onOptionsItemSelected(CoursesList.java:154) 
02-04 01:13:54.984: ERROR/AndroidRuntime(342):  at android.app.Activity.onMenuItemSelected(Activity.java:2205) 
02-04 01:13:54.984: ERROR/AndroidRuntime(342):  at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:748) 
02-04 01:13:54.984: ERROR/AndroidRuntime(342):  at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143) 
02-04 01:13:54.984: ERROR/AndroidRuntime(342):  at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855) 
02-04 01:13:54.984: ERROR/AndroidRuntime(342):  at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:532) 
02-04 01:13:54.984: ERROR/AndroidRuntime(342):  at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122) 
02-04 01:13:54.984: ERROR/AndroidRuntime(342):  at android.view.View$PerformClick.run(View.java:9080) 
02-04 01:13:54.984: ERROR/AndroidRuntime(342):  at android.os.Handler.handleCallback(Handler.java:587) 
02-04 01:13:54.984: ERROR/AndroidRuntime(342):  at android.os.Handler.dispatchMessage(Handler.java:92) 
02-04 01:13:54.984: ERROR/AndroidRuntime(342):  at android.os.Looper.loop(Looper.java:123) 
02-04 01:13:54.984: ERROR/AndroidRuntime(342):  at android.app.ActivityThread.main(ActivityThread.java:3647) 
02-04 01:13:54.984: ERROR/AndroidRuntime(342):  at java.lang.reflect.Method.invokeNative(Native Method) 
02-04 01:13:54.984: ERROR/AndroidRuntime(342):  at java.lang.reflect.Method.invoke(Method.java:507) 
02-04 01:13:54.984: ERROR/AndroidRuntime(342):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
02-04 01:13:54.984: ERROR/AndroidRuntime(342):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
02-04 01:13:54.984: ERROR/AndroidRuntime(342):  at dalvik.system.NativeStart.main(Native Method) 
02-04 01:13:55.014: WARN/ActivityManager(61): Force finishing activity com.example.android.notepad/.CoursesList 

我在我的清單中intent-filter:

<activity android:name="AddCourseView" 
     android:theme="@android:style/Theme.Light" 
     android:label="@string/title_note" 
     android:screenOrientation="sensor" 
     android:configChanges="keyboardHidden|orientation" 
    > 
     <!-- This filter says that we can view or edit the data of 
      a single note --> 
     <intent-filter android:label="@string/resolve_course_edit"> 
      <action android:name="android.intent.action.VIEW" /> 
      <action android:name="android.intent.action.EDIT" /> 
      <action android:name="com.android.notepad.action.EDIT_COURSE" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
      <data android:mimeType="vnd.android.cursor.item/vnd.google.course" /> 
     </intent-filter> 

     <!-- This filter says that we can create a new note inside 
      of a directory of notes. --> 
     <intent-filter> 
      <action android:name="android.intent.action.INSERT" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
      <data android:mimeType="vnd.android.cursor.dir/vnd.google.course" /> 
     </intent-filter> 

    </activity> 

這裏是意圖被推出代碼:

 Uri intentUri = getIntent().getData(); 
     Intent intent = new Intent(Intent.ACTION_INSERT, intentUri); 
     startActivity(intent); 

NotePadProvider中的getType()方法:

@Override 
public String getType(Uri uri) { 
    switch (sUriMatcher.match(uri)) { 
    case NOTES: 
    case LIVE_FOLDER_NOTES: 
     return Notes.CONTENT_TYPE; 

    case NOTE_ID: 
     return Notes.CONTENT_ITEM_TYPE; 

    case COURSES: 
     return Courses.CONTENT_TYPE; 

    case COURSE_ID: 
     return Courses.CONTENT_ITEM_TYPE; 

    default: 
     throw new IllegalArgumentException("Unknown URI " + uri); 
    } 
} 

這裏是其中i定義類型:

公共最終類記事本{ 公共靜態最終字符串AUTHORITY = 「com.google.provider.NotePad」;

// This class cannot be instantiated 
private NotePad() {} 

/** 
* Notes table 
*/ 
public static final class Notes implements BaseColumns { 
    // This class cannot be instantiated 
    private Notes() {} 

    /** 
    * The content:// style URL for this table 
    */ 
    public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/notes"); 

    /** 
    * The MIME type of {@link #CONTENT_URI} providing a directory of notes. 
    */ 
    public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.google.note"; 

    /** 
    * The MIME type of a {@link #CONTENT_URI} sub-directory of a single note. 
    */ 
    public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.google.note"; 

    /** 
    * The default sort order for this table 
    */ 
    public static final String DEFAULT_SORT_ORDER = "modified DESC"; 

    /** 
    * The title of the note 
    * <P>Type: TEXT</P> 
    */ 
    public static final String TITLE = "title"; 

    /** 
    * The note itself 
    * <P>Type: TEXT</P> 
    */ 
    public static final String NOTE = "note"; 

    /** 
    * The task content 
    * <P>Type: TEXT</P> 
    */ 
    public static final String TASK_DESCRIPTION = "description"; 
    /** 
    * The task location 
    * <P>Type: TEXT</P> 
    */ 
    public static final String TASK_LOCATION = "location"; 

    /** 
    * The task start time 
    * as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC 
    */ 
    public static final String TASK_START_TIME = "starttime"; 

    /** 
    * The task end time 
    * as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC 
    */ 
    public static final String TASK_END_TIME = "endtime"; 


    /** 
    * The timestamp for when the note was created 
    * <P>Type: INTEGER (long from System.curentTimeMillis())</P> 
    */ 
    public static final String CREATED_DATE = "created"; 

    /** 
    * The timestamp for when the note was last modified 
    * <P>Type: INTEGER (long from System.curentTimeMillis())</P> 
    */ 
    public static final String MODIFIED_DATE = "modified"; 

    /** 
    * if it is an assignment/test, the weight of it is here 
    */ 
    public static final String WEIGHT = "weight"; 

    /** 
    * the course the note is assigned to 
    */ 
    public static final String RELATED_COURSE = "relatedcourse"; 


} 

/** 
* Courses table 
*/ 
public static final class Courses implements BaseColumns { 
    // This class cannot be instantiated 
    private Courses() {} 

    /** 
    * The content:// style URL for this table 
    */ 
    public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/courses"); 

    /** 
    * The MIME type of {@link #CONTENT_URI} providing a directory of notes. 
    */ 
    public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.google.course"; 

    /** 
    * The MIME type of a {@link #CONTENT_URI} sub-directory of a single note. 
    */ 
    public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.google.course"; 

    /** 
    * The default sort order for this table 
    */ 
    public static final String DEFAULT_SORT_ORDER = "modified DESC"; 

    /** 
    * The title of the note 
    * <P>Type: TEXT</P> 
    */ 
    public static final String TITLE = "title"; 

    public static final String PROFESSOR = "professor"; 

    public static final String DESCRIPTION = "description"; 

    /** 
    * The timestamp for when the note was created 
    * <P>Type: INTEGER (long from System.curentTimeMillis())</P> 
    */ 
    public static final String CREATED_DATE = "created"; 

    /** 
    * The timestamp for when the note was last modified 
    * <P>Type: INTEGER (long from System.curentTimeMillis())</P> 
    */ 
    public static final String MODIFIED_DATE = "modified"; 
} 

}

這裏是清單的供應部件:

<provider android:name="NotePadProvider" 
     android:authorities="com.google.provider.NotePad"/> 

好吧,我改變的意圖推出這樣:

 Intent intent = new Intent(Intent.ACTION_INSERT); 
     intent.setType(Courses.CONTENT_TYPE); 
     startActivity(intent); 

所以應該不存在類型混淆。現在我得到這個錯誤:

02-06 22:48:26.076:錯誤/ AndroidRuntime(640):java.lang.RuntimeException:無法啓動活動ComponentInfo {com.example.android.notepad/com.example .android.notepad.AddCourseView}:java.lang.NullPointerException

+0

顯示完整的NPE堆棧跟蹤 – 2011-02-06 22:52:44

+0

我會一直猜測... com.example.android.notepad.AddCourseView不存在。 包裹路徑是否正確?班級名稱是否正確? AddCourseView擴展Activity嗎? – techiServices 2011-02-06 23:43:17

+0

這就是我的想法,我所做的只是將AddCourseView的名稱更改爲CourseEditor,並在eclipse中「更新引用」,它似乎工作。我也從「AddCourseView」更改爲「CourseEditor」的清單 – evan 2011-02-06 23:50:31

回答

2

我會猜測你的意圖有問題。

編輯:

請發表您的清單:)

編輯2的供應部件:

com.example.android.notepad.AddCourseView不存在。

包的路徑是否正確? 類名是否正確? AddCourseView擴展Activity嗎?

隨意標記我... 2更多,我可以刪除帖子並獲得徽章...

0

您的活動僅響應內容類型爲「vnd.android.cursor.item/vnd.google.course」的意圖。可能您的內容提供商不會爲此URI提供此內容類型(在getType()方法中):content://com.google.provider.NotePad/courses。

0

好的,你的問題非常好,但它錯過了一些信息。

首先,這裏是意圖:

act=android.intent.action.INSERT dat=content://com.google.provider.NotePad/courses 

這樣的動作是正確的,但數據似乎是不正確的。您已將數據設置爲項目的Uri,但必須將其設置爲vnd.android.cursor.dir/vnd.google.course的內容類型。這樣做,它會打開活動。

0

我通過在清單中添加活動來解決此問題,但是也將確切的區分大小寫的類名稱與包信息一起使用。

e.g:com.faceworld.galleryActivity