我正在制作Android应用,我发现了一个问题。 我有活动,我必须从它做出警告对话。这很好,但我也需要在该对话框中使用微调器,我无法正确设置适配器。应用程序崩溃与NullPointer ex。 带有微调器的Dialog xml:
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:drawSelectorOnTop="true" />
这是代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_list);
MyListView = (ListView) findViewById(android.R.id.list);
registerForContextMenu(MyListView);
myList = getIntent().getParcelableArrayListExtra(
MainActivity.NUMBER_LIST);
for (MyCallLog m : myList) {
stringList.add(m.getNumber());
}
MyListView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, stringList) {
});
}
和对话代码:
public void DialogData(final int DialogTyp) {
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(LAYOUT_INFLATER_SERVICE);
final View layout = inflater.inflate(R.layout.dialog, null);
AlertDialog.Builder MyBuilder = new AlertDialog.Builder(this);
MyBuilder.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
if (DialogTyp == 1) {
}
else if (DialogTyp == 2) {
stringList.remove(aktual);
}
MyListView.invalidateViews();
return;
}
});
MyBuilder.setNegativeButton("Storno",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
return;
}
});
if (DialogTyp != 2)
MyBuilder.setView(layout);
AlertDialog MyAlertDialog = MyBuilder.create();
if (DialogTyp == 0)
MyAlertDialog.setTitle("Nový záznam");
else if (DialogTyp == 1) {
addItemsOnSpinner();
MyAlertDialog.setTitle("Editace záznamu");
((EditText)layout.findViewById(R.id.cislo)).setText(myCallLog.getNumber());
((EditText)layout.findViewById(R.id.delka)).setText(String.valueOf(myCallLog.getDuration()));
((EditText)layout.findViewById(R.id.datum)).setText("25.2.2015");
((Spinner)layout.findViewById(R.id.spinner)).setSelection(getSelectedOperator(myCallLog.getOperator()));
} else if (DialogTyp == 2) {
MyAlertDialog.setTitle("Smazání záznamu" + aktual);
MyAlertDialog.setMessage("Opravdu chcete smazat vybraný záznam?");
}
MyAlertDialog.show();
}
public void editList(int pozition, String number, String date, String spinner){
}
public void addItemsOnSpinner() {
List<String> list = new ArrayList<String>();
list.add("O2");
list.add("T_MOBILE");
list.add("VODAFONE");
spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, list);
spinner.setAdapter(adapter);
}
这里最后一行应该有错误。 问题在哪里或我必须做些什么才能正确设置?
答案 0 :(得分:1)
spinner
为空,因为您正在尝试从您的Activity findViewById
,这不是对话框布局。而是传递你在这里检索的微调器
((Spinner)layout.findViewById(R.id.spinner))
进入public void addItemsOnSpinner() {...}
方法。
答案 1 :(得分:1)
您正试图从不同的视图中获取Spinner,如果Spinner在对话框中,那么您应该这样做:
AlertDialog MyAlertDialog = MyBuilder.create();
Spinner spinnerInsideDialog = (Spinner) MyAlertDialog.findViewById(R.id.spinner);