我正在尝试从sqlite数据库填充一个微调器。数据库是+ 400mb,我手动放入我的SD卡中的/ data / data / myapplicationpackage / databases目录。
我还有一个ContentProvider来访问数据。以下是代码的一些相关部分:
private static final String DATABASE_NAME = "mydb.db";
private static final int DATABASE_VERSION = 1;
private static final String DOSIS_PA_TABLE = "mytable";
public static final String KEY_ID_PA = "_id";
public static final String KEY_DESCRIPTION = "description";
public static final String CONTENT_AUTHORITY = "com.mypackage.myappname";
public static final Uri BASE_CONTENT_URI = Uri.parse("content://"
+ CONTENT_AUTHORITY);
private static final String PATH_DOSISPED_PA = "mytable";
public static final Uri CONTENT_URI = BASE_CONTENT_URI.buildUpon()
.appendPath(PATH_DOSISPED_PA).build();
public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.myappname.mytable";
public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.myappname.mytable";
private static final int DOSISPAS = 1;
private static final int DOSISPA_ID = 2;
private static final UriMatcher uriMatcher;
static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI("CONTENT_AUTHORITY", "mytable", DOSISPAS);
uriMatcher.addURI("CONTENT_AUTHORITY", "mytable/*", DOSISPA_ID);
}
这是我触发查询的活动的一部分:
contentResolver = getContentResolver();
Uri uri = MyContentProvider.CONTENT_URI;
Cursor cursor = contentResolver.query(uri, null, null, null, null);
// create an array to specify which fields we want to display
String[] from = new String[] { MyContentProvider.KEY_DESCRIPTION };
// create an array of the display item we want to bind our data to
int[] to = new int[] { android.R.id.text1 };
// create simple cursor adapter
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_spinner_item, cursor, from, to);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// get reference to our spinner
Spinner spinner = (Spinner) findViewById(R.id.spinner_pas);
spinner.setAdapter(adapter);//THIS IS WHERE THE EXCEPTION IS THRWON
在这行代码中抛出了NullPointerException:
spinner.setAdapter(adapter);
这是logcat:
10-02 18:41:32.109: E/AndroidRuntime(2783): FATAL EXCEPTION: main
10-02 18:41:32.109: E/AndroidRuntime(2783): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.edoctores.android.apps.idoctus/com.edoctores.android.apps.idoctus.ui.PediatricDosisActivity}: java.lang.NullPointerException
10-02 18:41:32.109: E/AndroidRuntime(2783): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
10-02 18:41:32.109: E/AndroidRuntime(2783): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-02 18:41:32.109: E/AndroidRuntime(2783): at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-02 18:41:32.109: E/AndroidRuntime(2783): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-02 18:41:32.109: E/AndroidRuntime(2783): at android.os.Handler.dispatchMessage(Handler.java:99)
10-02 18:41:32.109: E/AndroidRuntime(2783): at android.os.Looper.loop(Looper.java:137)
10-02 18:41:32.109: E/AndroidRuntime(2783): at android.app.ActivityThread.main(ActivityThread.java:4745)
10-02 18:41:32.109: E/AndroidRuntime(2783): at java.lang.reflect.Method.invokeNative(Native Method)
10-02 18:41:32.109: E/AndroidRuntime(2783): at java.lang.reflect.Method.invoke(Method.java:511)
10-02 18:41:32.109: E/AndroidRuntime(2783): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-02 18:41:32.109: E/AndroidRuntime(2783): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-02 18:41:32.109: E/AndroidRuntime(2783): at dalvik.system.NativeStart.main(Native Method)
10-02 18:41:32.109: E/AndroidRuntime(2783): Caused by: java.lang.NullPointerException
10-02 18:41:32.109: E/AndroidRuntime(2783): at com.edoctores.android.apps.idoctus.ui.PediatricDosisActivity.onCreate(PediatricDosisActivity.java:72)
10-02 18:41:32.109: E/AndroidRuntime(2783): at android.app.Activity.performCreate(Activity.java:5008)
10-02 18:41:32.109: E/AndroidRuntime(2783): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
10-02 18:41:32.109: E/AndroidRuntime(2783): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
10-02 18:41:32.109: E/AndroidRuntime(2783): ... 11 more
我一整天都在努力修复它而没有任何运气。有人可以帮助我并指出我正确的方向吗?非常感谢提前
答案 0 :(得分:1)
findViewById()
仅在显示当前时返回视图,否则返回null
。 必须在您传递给spinner_pas
的布局中(或者在已经夸大的布局中)中有一个ID为setContentView()
的微调器。