我熟悉Android和ContectProviders的编程。我已经创建了代码(用于测试目的)来检查数据库的读/写
这是代码 TestProvider.java
package com.example.testapp;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
public class TestProvider extends ContentProvider
{
private static final String DBNAME = "testdb";
private static final String SQL_CREATE_MAIN = "CREATE TABLE if not exists test(id INTEGER PRIMARY KEY, word TEXT)";
private MainDatabaseHelper helper;
@Override
public int delete(Uri uri, String selection, String[] selectionArgs)
{
return 0;
}
@Override
public String getType(Uri uri)
{
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values)
{
return null;
}
@Override
public boolean onCreate()
{
helper = new MainDatabaseHelper(getContext());
return false;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
{
return helper.getReadableDatabase().rawQuery("SELECT * FROM test", null);
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs)
{
return 0;
}
protected static final class MainDatabaseHelper extends SQLiteOpenHelper
{
MainDatabaseHelper(Context context)
{
super(context, DBNAME, null, 1);
}
public void onCreate(SQLiteDatabase db)
{
db.execSQL(SQL_CREATE_MAIN);
db.execSQL("INSERT INTO test (word) VALUES ('AAA')");
db.execSQL("INSERT INTO test (word) VALUES ('BBB')");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
}
}
}
这是MainActivity.java
package com.example.testapp;
import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TestProvider provider = new TestProvider();
Cursor c = provider.query(Uri.parse("content://com.example.testapp.provider/test"), null, null, null, null);
do
{
Log.d("MainActivity", String.format("ID:%s / Word:%s", c.getInt(c.getColumnIndex("id")), c.getString(c.getColumnIndex("word"))));
}
while(c.moveToNext());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
来自logcat的Stacktrace:
07-30 15:38:25.695: E/Trace(3533): error opening trace file: No such file or directory (2)
07-30 15:38:26.255: D/AndroidRuntime(3533): Shutting down VM
07-30 15:38:26.305: W/dalvikvm(3533): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
07-30 15:38:26.345: E/AndroidRuntime(3533): FATAL EXCEPTION: main
07-30 15:38:26.345: E/AndroidRuntime(3533): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testapp/com.example.testapp.MainActivity}: java.lang.NullPointerException
07-30 15:38:26.345: E/AndroidRuntime(3533): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-30 15:38:26.345: E/AndroidRuntime(3533): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-30 15:38:26.345: E/AndroidRuntime(3533): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-30 15:38:26.345: E/AndroidRuntime(3533): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-30 15:38:26.345: E/AndroidRuntime(3533): at android.os.Handler.dispatchMessage(Handler.java:99)
07-30 15:38:26.345: E/AndroidRuntime(3533): at android.os.Looper.loop(Looper.java:137)
07-30 15:38:26.345: E/AndroidRuntime(3533): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-30 15:38:26.345: E/AndroidRuntime(3533): at java.lang.reflect.Method.invokeNative(Native Method)
07-30 15:38:26.345: E/AndroidRuntime(3533): at java.lang.reflect.Method.invoke(Method.java:511)
07-30 15:38:26.345: E/AndroidRuntime(3533): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-30 15:38:26.345: E/AndroidRuntime(3533): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-30 15:38:26.345: E/AndroidRuntime(3533): at dalvik.system.NativeStart.main(Native Method)
07-30 15:38:26.345: E/AndroidRuntime(3533): Caused by: java.lang.NullPointerException
07-30 15:38:26.345: E/AndroidRuntime(3533): at com.example.testapp.TestProvider.query(TestProvider.java:45)
07-30 15:38:26.345: E/AndroidRuntime(3533): at com.example.testapp.MainActivity.onCreate(MainActivity.java:19)
07-30 15:38:26.345: E/AndroidRuntime(3533): at android.app.Activity.performCreate(Activity.java:5104)
07-30 15:38:26.345: E/AndroidRuntime(3533): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-30 15:38:26.345: E/AndroidRuntime(3533): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-30 15:38:26.345: E/AndroidRuntime(3533): ... 11 more
问题出在哪里?感谢