gpt4 book ai didi

java - 如何在警报对话框中为微调器设置适配器

转载 作者:行者123 更新时间:2023-11-29 20:52:39 24 4
gpt4 key购买 nike

我在制作 Android 应用程序时发现了一个问题。我有 Activity ,我必须从中发出警报对话框。这很好,但我也需要那个对话框中的微调器,我无法正确设置适配器。应用程序因 NullPointer ex 而崩溃。一段带有微调器的对话框 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);
}

最后一行应该有错误。问题出在哪里或我必须如何正确设置它?

最佳答案

spinner 为空,因为您正尝试从您的 Activity 中findViewById,这不是对话框布局。而是在此处传递您检索到的微调器

((Spinner)layout.findViewById(R.id.spinner))

进入您的 public void addItemsOnSpinner() {...} 方法。

关于java - 如何在警报对话框中为微调器设置适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28708151/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com