这让我失去了理智。我一直在崩溃。之前曾问过类似的问题,但我没有犯同样的错误。这是我的清单:
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".ReminderListActivity"
android:label="@string/title_activity_reminder_list" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ReminderEditActivity"
android:label="@string/title_activity_reminder_edit" >
</activity>
<provider
android:name=".ReminderListProvider"
android:authorities="com.corsair.android.taskreminder.ReminderListProvider" >
</provider>
</application>
正如你所看到的,我拥有完全合格的权力。我在代码中声明了一个常量用于权限(并在我创建Uri时使用它:
public static final String AUTHORITY = "com.corsair.android.taskreminder.ReminderListProvider";
// basic database info for creation
public static final String DATABASE_NAME = "reminders.db";
public static final String DATABASE_TABLE_NAME = "reminders";
public static final int DATABASE_VERSION = 1;
private ReminderListProviderMetaData() {
}
// inner class describing the Reminders Table
// by implementing BaseColumns, we automatically get the _ID column
// which is necessary for showing the DB contents in a ListView
public static final class RemindersTableMetaData implements BaseColumns {
private RemindersTableMetaData() {
}
public static final String TABLE_NAME = "reminders";
// uri and MIME type definitions
// info for database access
// the content URI is the authority prefixed with "content://"
public static final String BASE_PATH = "reminders";
public static final Uri CONTENT_URI = Uri.parse("content://"
+ AUTHORITY + "/" + BASE_PATH);
这是我的logcat:
08-01 18:11:10.111: I/dalvikvm(1150): threadid=3: reacting to signal 3
08-01 18:11:10.141: I/dalvikvm(1150): Wrote stack traces to '/data/anr/traces.txt'
08-01 18:11:10.481: I/dalvikvm(1150): threadid=3: reacting to signal 3
08-01 18:11:10.694: I/dalvikvm(1150): Wrote stack traces to '/data/anr/traces.txt'
08-01 18:11:11.951: I/Process(1150): Sending signal. PID: 1150 SIG: 9
08-01 18:12:50.231: I/dalvikvm(1196): threadid=3: reacting to signal 3
08-01 18:12:50.661: I/dalvikvm(1196): Wrote stack traces to '/data/anr/traces.txt'
08-01 18:12:50.671: I/dalvikvm(1196): threadid=3: reacting to signal 3
08-01 18:12:50.801: I/ActivityThread(1196): Pub com.corsair.android.taskreminder.ReminderListProvider: com.corsair.android.taskreminder.ReminderListProvider
08-01 18:12:50.833: D/ReminderProvider(1196): main oncreate called
08-01 18:12:50.851: I/dalvikvm(1196): Wrote stack traces to '/data/anr/traces.txt'
08-01 18:12:50.871: D/ReminderProvider(1196): inner oncreate called
08-01 18:12:50.893: D/AndroidRuntime(1196): Shutting down VM
08-01 18:12:50.893: W/dalvikvm(1196): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
08-01 18:12:50.911: E/AndroidRuntime(1196): FATAL EXCEPTION: main
08-01 18:12:50.911: E/AndroidRuntime(1196): java.lang.RuntimeException: Unable to get provider com.corsair.android.taskreminder.ReminderListProvider: java.lang.NullPointerException
08-01 18:12:50.911: E/AndroidRuntime(1196): at android.app.ActivityThread.installProvider(ActivityThread.java:4240)
08-01 18:12:50.911: E/AndroidRuntime(1196): at android.app.ActivityThread.installContentProviders(ActivityThread.java:3992)
08-01 18:12:50.911: E/AndroidRuntime(1196): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3946)
08-01 18:12:50.911: E/AndroidRuntime(1196): at android.app.ActivityThread.access$1300(ActivityThread.java:123)
08-01 18:12:50.911: E/AndroidRuntime(1196): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1185)
08-01 18:12:50.911: E/AndroidRuntime(1196): at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 18:12:50.911: E/AndroidRuntime(1196): at android.os.Looper.loop(Looper.java:137)
08-01 18:12:50.911: E/AndroidRuntime(1196): at android.app.ActivityThread.main(ActivityThread.java:4424)
08-01 18:12:50.911: E/AndroidRuntime(1196): at java.lang.reflect.Method.invokeNative(Native Method)
08-01 18:12:50.911: E/AndroidRuntime(1196): at java.lang.reflect.Method.invoke(Method.java:511)
08-01 18:12:50.911: E/AndroidRuntime(1196): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-01 18:12:50.911: E/AndroidRuntime(1196): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-01 18:12:50.911: E/AndroidRuntime(1196): at dalvik.system.NativeStart.main(Native Method)
08-01 18:12:50.911: E/AndroidRuntime(1196): Caused by: java.lang.NullPointerException
08-01 18:12:50.911: E/AndroidRuntime(1196): at com.corsair.android.taskreminder.ReminderListProvider$DatabaseHelper.onCreate(ReminderListProvider.java:75)
08-01 18:12:50.911: E/AndroidRuntime(1196): at com.corsair.android.taskreminder.ReminderListProvider.onCreate(ReminderListProvider.java:102)
08-01 18:12:50.911: E/AndroidRuntime(1196): at android.content.ContentProvider.attachInfo(ContentProvider.java:944)
08-01 18:12:50.911: E/AndroidRuntime(1196): at android.app.ActivityThread.installProvider(ActivityThread.java:4237)
08-01 18:12:50.911: E/AndroidRuntime(1196): ... 12 more
08-01 18:12:51.241: I/dalvikvm(1196): threadid=3: reacting to signal 3
08-01 18:12:51.291: I/dalvikvm(1196): Wrote stack traces to '/data/anr/traces.txt'
08-01 18:12:51.541: I/dalvikvm(1196): threadid=3: reacting to signal 3
08-01 18:12:51.601: I/dalvikvm(1196): Wrote stack traces to '/data/anr/traces.txt'
08-01 18:12:58.262: I/Process(1196): Sending signal. PID: 1196 SIG: 9
这是第75行(在logcat中引用):
@Override
public void onCreate(SQLiteDatabase db) {
Log.d(TAG, "inner oncreate called");
db.execSQL(RemindersTableMetaData.DATABASE_CREATE_SCHEMA);
}
这是DATABASE_CREATE_SCHEMA:
public static final String DATABASE_CREATE_SCHEMA = "create table "
+ DATABASE_TABLE_NAME + " (" + RemindersTableMetaData._ID
+ " integer primary key autoincrement, " + REMINDER_TITLE
+ " text not null, " + REMINDER_BODY + " text not null, "
+ REMINDER_DATE_TIME + " text not null" + ");";
这是我的ContentProvider的onCreate():
// database helper member variable
private DatabaseHelper mOpenHelper;
private SQLiteDatabase db;
// component creation callback
@Override
public boolean onCreate() {
Log.d(TAG, "main oncreate called");
mOpenHelper = new DatabaseHelper(getContext());
mOpenHelper.onCreate(db);
return true;
}
这是我的DataBaseHelper的构造函数:
// this class helps open, create, and upgrade the database
private static class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context) {
super(context, ReminderListProviderMetaData.DATABASE_NAME, null,
ReminderListProviderMetaData.DATABASE_VERSION);
}
真的在我的智慧结束了。谢谢你的帮助。
答案 0 :(得分:1)
我想我在ContentProvider中看到了问题,我的评论详细说明了这些变化:
public boolean onCreate() {
Log.d(TAG, "main oncreate called");
mOpenHelper = new DatabaseHelper(getContext());
/* Remove this line next line:
* 1) db is null
* 2) DatabaseHelper's constructor already silently calls this
*/
//mOpenHelper.onCreate(db);
return true;
}