您好我一直在寻找如何从光标中填充微调器一段时间而无法正常操作,这是我的代码:
private void fillSpinner(){
DatabaseConnector dbConnector = new DatabaseConnector(this);
Cursor c = dbConnector.getAllCasas();
startManagingCursor(c);
String[] from = new String[]{"nombrecasa"};
int[] to = new int[]{android.R.id.text1};
SimpleCursorAdapter adapter =
new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c, from, to );
adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
Spinner s = (Spinner) findViewById( R.id.casaspinner);
s.setAdapter(adapter);
}
其中DAtabaseConnector是我获取所有游标的类,并且在XML文件中正确声明了casaspinner。
从onCreate方法调用此方法但它崩溃“抱歉,应用程序意外停止”logcat显示“NullPointerException” 从这里尝试了一些答案,但它一直在崩溃。
这是logcat:
12-09 10:02:49.399: W/ResourceType(322): Attempt to retrieve bag 0x7f060003 which is invalid or in a cycle.
12-09 10:02:49.489: D/AndroidRuntime(322): Shutting down VM
12-09 10:02:49.489: W/dalvikvm(322): threadid=1: thread exiting with uncaught exception (group=0x40015560)
12-09 10:02:49.509: E/AndroidRuntime(322): FATAL EXCEPTION: main
12-09 10:02:49.509: E/AndroidRuntime(322): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.appvino/com.example.appvino.Actualizacion}: java.lang.NullPointerException
12-09 10:02:49.509: E/AndroidRuntime(322): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-09 10:02:49.509: E/AndroidRuntime(322): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-09 10:02:49.509: E/AndroidRuntime(322): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-09 10:02:49.509: E/AndroidRuntime(322): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-09 10:02:49.509: E/AndroidRuntime(322): at android.os.Handler.dispatchMessage(Handler.java:99)
12-09 10:02:49.509: E/AndroidRuntime(322): at android.os.Looper.loop(Looper.java:123)
12-09 10:02:49.509: E/AndroidRuntime(322): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-09 10:02:49.509: E/AndroidRuntime(322): at java.lang.reflect.Method.invokeNative(Native Method)
12-09 10:02:49.509: E/AndroidRuntime(322): at java.lang.reflect.Method.invoke(Method.java:507)
12-09 10:02:49.509: E/AndroidRuntime(322): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-09 10:02:49.509: E/AndroidRuntime(322): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-09 10:02:49.509: E/AndroidRuntime(322): at dalvik.system.NativeStart.main(Native Method)
12-09 10:02:49.509: E/AndroidRuntime(322): Caused by: java.lang.NullPointerException
12-09 10:02:49.509: E/AndroidRuntime(322): at com.example.appvino.DatabaseConnector.getAllCepas(DatabaseConnector.java:95)
12-09 10:02:49.509: E/AndroidRuntime(322): at com.example.appvino.Actualizacion.llenaCepaSpinner(Actualizacion.java:129)
12-09 10:02:49.509: E/AndroidRuntime(322): at com.example.appvino.Actualizacion.onCreate(Actualizacion.java:40)
12-09 10:02:49.509: E/AndroidRuntime(322): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-09 10:02:49.509: E/AndroidRuntime(322): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-09 10:02:49.509: E/AndroidRuntime(322): ... 11 more
答案 0 :(得分:0)
我错过了可写入的数据库。这是解决方案:
private void llenaCasaSpinner(){
DatabaseConnector dbConnector = new DatabaseConnector(this);
dbConnector.open();
Cursor c = dbConnector.raw("SELECT _id, nombrecasa FROM casa");
startManagingCursor(c);
String[] from = new String[]{"nombrecasa"};
int[] to = new int[]{android.R.id.text1};
SimpleCursorAdapter adapter =
new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c, from, to );
adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
Spinner s = (Spinner) findViewById( R.id.casaspinner);
s.setAdapter(adapter);
dbConnector.close();
}