我想点击Dialog
行时打开ListView
,并从Spinner
加载SQLiteDatabase
的数据。
填写Spinner
时,应用程序强制关闭。
崩溃就在这一行:
spinner.setAdapter(DataAdapter的);
DBAdapter.java
public List<String> getAllLabels() {
List<String> labels = new ArrayList<String>();
// Select All Query
String selectQuery = "SELECT * FROM " + DATABASE_MAINTABLE;
SQLiteDatabase db = DBHelper.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
labels.add(cursor.getString(1));
} while (cursor.moveToNext());
}
// closing connection
cursor.close();
db.close();
// returning lables
return labels;
}
MSmsActivity.java
public void passengerInformationPopup() {
final Dialog dialog = new Dialog(MSmsActivity.this);
dialog.setContentView(R.layout.activity_dialog);
loadSpinnerData(); //{"Mr.","Mrs.","Ms"};
// String [] tittleSpinner = {"Mr.","Mrs.","Ms"};
// Spinner tittleSpinner = (Spinner) dialog.findViewById(R.id.spinner);
// ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
// android.R.layout.simple_spinner_item, tittleSpinner);
// dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
// tittleSpinner.setAdapter(dataAdapter);
dialog.show();
}
private void loadSpinnerData() {
// DatabaseHandler db = new DatabaseHandler(getApplicationContext());
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Spinner Drop down elements
List<String> lables = db.getAllLabels();
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, lables);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
}
错误
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at www.project_category.ir.project_category.MSmsActivity.loadSpinnerData(MSmsActivity.java:301)
at www.project_category.ir.project_category.MSmsActivity.passengerInformationPopup(MSmsActivity.java:276)
at www.project_category.ir.project_category.MSmsActivity$2.onItemClick(MSmsActivity.java:176)
at android.widget.AdapterView.performItemClick(AdapterView.java:301)
at android.widget.AbsListView.performItemClick(AbsListView.java:1507)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3336)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:177)
at android.app.ActivityThread.main(ActivityThread.java:5493)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1225)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1041)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
如果你的微调器在对话框内,那么很可能你的微调器id为null。 尝试找到这样的id和popup方法
spinner=dialog.findViewById(R.id.spinner);
答案 1 :(得分:0)
Spinner
位于R.layout.activity_dialog
,这是Dialog
的布局。
您需要使用
Spinner spinner=dialog.findViewById(R.id.spinner);