我在这里看过类似的帖子,但我无法得到这些答案的结果。我试图在自定义对话框中填充SQLite中的微调器但我在特定行上不断获得空指针异常
private void showUploadDialog(){
Dialog dialog = new Dialog(MyClass.this);
dialog.setContentView(R.layout.uploaddialog);
dialog.setTitle("This is my custom dialog box");
dialog.setCancelable(true);
final SimpleCursorAdapter dataSource;
final String fields[] = { "siteName","_id" };
int[] to = new int[] { R.id.spinner_site_item };
s = (Spinner) findViewById(R.id.spinner1);
DatabaseHelper helper = new DatabaseHelper(MyClass.this);
database = helper.getWritableDatabase();
Cursor ftpCursor = database.query("ftpsites", fields, null, null, null, null, null);
startManagingCursor(ftpCursor);
if (ftpCursor != null && ftpCursor.moveToFirst()){
dataSource = new SimpleCursorAdapter(this, R.layout.spinner_item, ftpCursor, fields, to);
dataSource.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(dataSource);
} else {
Toast toast = Toast.makeText(getApplicationContext(), "Cursor's fucked up", Toast.LENGTH_SHORT);
toast.show();
}
database.close();
Button button = (Button) dialog.findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
//now that the dialog is set up, it's time to show it
dialog.show();
}
我在s.setAdapter(dataSource);
中得到了异常这是LogCat输出
01-24 15:47:08.198: E/AndroidRuntime(10834): FATAL EXCEPTION: main
01-24 15:47:08.198: E/AndroidRuntime(10834): java.lang.NullPointerException
01-24 15:47:08.198: E/AndroidRuntime(10834): at app.me.myapp.MyClass.showUploadDialog(MyClass.java:257)
01-24 15:47:08.198: E/AndroidRuntime(10834): at app.me.myapp.MyClass.access$15(MyClass.java:233)
01-24 15:47:08.198: E/AndroidRuntime(10834): at app.me.myapp.MyClass$11.onClick(MyClass.java:741)
01-24 15:47:08.198: E/AndroidRuntime(10834): at android.view.View.performClick(View.java:2532)
01-24 15:47:08.198: E/AndroidRuntime(10834): at android.view.View$PerformClick.run(View.java:9293)
01-24 15:47:08.198: E/AndroidRuntime(10834): at android.os.Handler.handleCallback(Handler.java:587)
01-24 15:47:08.198: E/AndroidRuntime(10834): at android.os.Handler.dispatchMessage(Handler.java:92)
01-24 15:47:08.198: E/AndroidRuntime(10834): at android.os.Looper.loop(Looper.java:150)
01-24 15:47:08.198: E/AndroidRuntime(10834): at android.app.ActivityThread.main(ActivityThread.java:4263)
01-24 15:47:08.198: E/AndroidRuntime(10834): at java.lang.reflect.Method.invokeNative(Native Method)
01-24 15:47:08.198: E/AndroidRuntime(10834): at java.lang.reflect.Method.invoke(Method.java:507)
01-24 15:47:08.198: E/AndroidRuntime(10834): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-24 15:47:08.198: E/AndroidRuntime(10834): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-24 15:47:08.198: E/AndroidRuntime(10834): at dalvik.system.NativeStart.main(Native Method)
01-24 15:47:09.559: I/Process(10834): Sending signal. PID: 10834 SIG: 9
答案 0 :(得分:1)
试试这个
s =(Spinner)dialog.findViewById(R.id.spinner1);
而不是
s =(Spinner)findViewById(R.id.spinner1);