mp应用程序在启动时崩溃。我收到“java.lang.RuntimeException:无法获取提供程序”错误。我假设这与我的sqlite数据库有关,因为我的contactprovider现在持有代码来创建数据库。任何帮助计算这个问题的因果关系。我的Logcat,DB Class和provicer类在下面。
logcat的:
04-24 12:59:07.305: E/AndroidRuntime(1482): FATAL EXCEPTION: main
04-24 12:59:07.305: E/AndroidRuntime(1482): java.lang.RuntimeException: Unable to get provider com.loginplus.home.ListProvider: java.lang.NullPointerException
04-24 12:59:07.305: E/AndroidRuntime(1482): at android.app.ActivityThread.installProvider(ActivityThread.java:4240)
04-24 12:59:07.305: E/AndroidRuntime(1482): at android.app.ActivityThread.installContentProviders(ActivityThread.java:3992)
04-24 12:59:07.305: E/AndroidRuntime(1482): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3946)
04-24 12:59:07.305: E/AndroidRuntime(1482): at android.app.ActivityThread.access$1300(ActivityThread.java:123)
04-24 12:59:07.305: E/AndroidRuntime(1482): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1185)
04-24 12:59:07.305: E/AndroidRuntime(1482): at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 12:59:07.305: E/AndroidRuntime(1482): at android.os.Looper.loop(Looper.java:137)
04-24 12:59:07.305: E/AndroidRuntime(1482): at android.app.ActivityThread.main(ActivityThread.java:4424)
04-24 12:59:07.305: E/AndroidRuntime(1482): at java.lang.reflect.Method.invokeNative(Native Method)
04-24 12:59:07.305: E/AndroidRuntime(1482): at java.lang.reflect.Method.invoke(Method.java:511)
04-24 12:59:07.305: E/AndroidRuntime(1482): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-24 12:59:07.305: E/AndroidRuntime(1482): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-24 12:59:07.305: E/AndroidRuntime(1482): at dalvik.system.NativeStart.main(Native Method)
04-24 12:59:07.305: E/AndroidRuntime(1482): Caused by: java.lang.NullPointerException
04-24 12:59:07.305: E/AndroidRuntime(1482): at android.database.DatabaseUtils.getSqlStatementType(DatabaseUtils.java:1318)
04-24 12:59:07.305: E/AndroidRuntime(1482): at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1893)
04-24 12:59:07.305: E/AndroidRuntime(1482): at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1839)
04-24 12:59:07.305: E/AndroidRuntime(1482): at com.loginplus.home.dataStore.onCreate(dataStore.java:27)
04-24 12:59:07.305: E/AndroidRuntime(1482): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:165)
04-24 12:59:07.305: E/AndroidRuntime(1482): at com.loginplus.home.ListProvider.onCreate(ListProvider.java:75)
04-24 12:59:07.305: E/AndroidRuntime(1482): at android.content.ContentProvider.attachInfo(ContentProvider.java:944)
04-24 12:59:07.305: E/AndroidRuntime(1482): at android.app.ActivityThread.installProvider(ActivityThread.java:4237)
04-24 12:59:07.305: E/AndroidRuntime(1482): ... 12 more
数据库类:
public class dataStore extends SQLiteOpenHelper {
//Table attributes
private static final String DATABASE_NAME = "SiteLogindb";
private static String DSTORE_CREATE;
private static final int DATABASE_VERSION = 2;
dataStore(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(DSTORE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
if(oldVersion == 1 && newVersion == 2){
//Upgrade the database
}
}
}
内容提供商:
public class ListProvider extends ContentProvider {
// Content Provider Uri and Authority
private static String AUTHORITY = "com.loginplus.home.ListProvider";
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
+ "/infotable" );
// Used for searching words or looking up a single definition
public static final String CONTENT_ITEM_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE
+ "/mt-tutorial";
public static final String CONTENT_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE
+ "/mt-tutorial";
//Database Columns
public static final String COLUMN_ROWID = "_id";
public static final String COLUMN_NAME_SITE = "sName";
public static final String COLUMN_NAME_ADDRESS = "wUrl";
public static final String COLUMN_NAME_USERNAME = "uName";
public static final String COLUMN_NAME_PASSWORD = "pWord";
public static final String COLUMN_NAME_NOTES = "lNotes";
// Database related Constants
public static final String DATABASE_NAME = "SiteLogindb";
public static final int DATABASE_VERSION = 2;
public static final String TABLE_NAME_INFOTABLE = "infoTable";
public static final String DSTORE_CREATE = "create table if not exists " +
TABLE_NAME_INFOTABLE + " ("+ COLUMN_ROWID + " integer primary key autoincrement,"
+ COLUMN_NAME_SITE + " text not null,"
+ COLUMN_NAME_ADDRESS + " text not null,"
+ COLUMN_NAME_USERNAME + " text not null,"
+ COLUMN_NAME_PASSWORD + " text not null,"
+ COLUMN_NAME_NOTES + " text not null);";
//UriMatcher stuff
private static final int LIST = 1;
private static final int LIST_ID = 2;
private static final UriMatcher sURIMatcher = buildUriMatcher();
private SQLiteDatabase lDB;
private static UriMatcher buildUriMatcher() {
UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
// get definitions
matcher.addURI(AUTHORITY, "infoTable", LIST);
matcher.addURI(AUTHORITY, "infoTable/#", LIST_ID);
return matcher;
}
@Override
public boolean onCreate() {
lDB = new dataStore(getContext()).getWritableDatabase();
return true;
}
@Override
public Cursor query(Uri uri, String[] ignored1, String ignored2,
String[] ignored3, String ignored4) {
String[] projection = new String[] {
ListProvider.COLUMN_ROWID,
ListProvider.COLUMN_NAME_SITE,
ListProvider.COLUMN_NAME_ADDRESS,
ListProvider.COLUMN_NAME_USERNAME,
ListProvider.COLUMN_NAME_PASSWORD,
ListProvider.COLUMN_NAME_NOTES };
Cursor c;
switch (sURIMatcher.match(uri)) {
case LIST:
c = lDB.query(ListProvider.TABLE_NAME_INFOTABLE, projection, null, null, null, null, null);
break;
case LIST_ID:
c = lDB.query(ListProvider.TABLE_NAME_INFOTABLE, projection, ListProvider.COLUMN_ROWID + "=?",
new String[] { Long.toString(ContentUris.parseId(uri)) },
null, null, null, null);
if (c !=null && c.getCount() > 0) {
c.moveToFirst();
}
break;
default:
throw new IllegalArgumentException("Unknown URI");
}
c.setNotificationUri(getContext().getContentResolver(), uri);
return c;
}
//Inserting DB entries
@Override
public Uri insert(Uri uri, ContentValues values) {
values.remove(ListProvider.COLUMN_ROWID);
long id = lDB.insertOrThrow(TABLE_NAME_INFOTABLE, null, values);
getContext().getContentResolver().notifyChange(uri, null);
return ContentUris.withAppendedId(uri, id);
}
//Deleting DB entries
@Override
public int delete(Uri uri, String ignored1, String[] ignored2) {
int count = lDB.delete(ListProvider.TABLE_NAME_INFOTABLE, ListProvider.COLUMN_ROWID + "=?",
new String[] { Long.toString(ContentUris.parseId(uri)) });
if (count > 0)
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
//updating DB entries
@Override
public int update(Uri uri, ContentValues values, String ignored1,
String[] ignored2) {
int count = lDB.update(TABLE_NAME_INFOTABLE, values, COLUMN_ROWID + "=?",new String[] {
Long.toString(ContentUris.parseId(uri)) });
if( count>0)
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
@Override
public String getType(Uri uri) {
// TODO Auto-generated method stub
switch (sURIMatcher.match(uri)) {
case LIST:
return CONTENT_ITEM_TYPE;
case LIST_ID:
return CONTENT_TYPE;
default:
throw new IllegalArgumentException("Unsupported URI: " + uri);
}
}
}
答案 0 :(得分:2)
可能的解释:在onCreate()里面的db.execSQL(DSTORE_CREATE)中调用它之前,我看不到你初始化DSTORE_CREATE字符串的位置?您已在内容提供程序中定义了它,但dataStore对象并未了解您在内容提供类中所做的此声明。
可能的解决方案: 如果您只是想摆脱它,请将DSTORE_CREATE定义到dataStore类而不是ListProvider类。
希望这会有所帮助。如果您仍有问题,请发表评论。
答案 1 :(得分:1)
您必须在dataStore类中初始化DSTORE_CREATE或使用ListProvider中的DSTORE_CREATE。