【发布时间】:2017-09-28 14:34:33
【问题描述】:
在我的项目中,当单击 ListView 的每个项目时,我会显示一个对话框。 但是在第三个位置之后我得到一个错误。我的代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rapor);
raporList = new ArrayList<>();
liste = (ListView) findViewById(R.id.list);
new RaporServis().execute();
//ListView Select
liste.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, final View view, int position,
long id) {
final CharSequence[] items = {"Görev Ataması Yapıldı", "İşlem Tamamlandı"};
View curr = parent.getChildAt((int) id);
//I'm getting an error here.
TextView c = (TextView) curr.findViewById(R.id.gorevNo);
String gorevNo = c.getText().toString();
AlertDialog.Builder builder = new AlertDialog.Builder(RaporActivity.this);
builder.setTitle("İş Durumu");
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
DialogSecimi = items[item].toString();
}
});
builder.setPositiveButton("Kaydet",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
TextView tv = (TextView) view.findViewById(R.id.isDurum);
tv.setText(DialogSecimi);
tv.setTextColor(Color.parseColor("#27ae60"));
if (DialogSecimi == "Görev Ataması Yapıldı") {
isDurumuKodu = "1";
} else if (DialogSecimi == "İşlem Tamamlandı") {
isDurumuKodu = "3";
}
Toast.makeText(RaporActivity.this, "Saved", Toast.LENGTH_SHORT).show();
} catch (Exception ex) {
Toast.makeText(RaporActivity.this, "Error ! Don't Save", Toast.LENGTH_SHORT).show();
}
}
});
builder.setNegativeButton("İptal Et",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(RaporActivity.this, "İptal Edildi", Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
}
错误:
尝试调用虚拟方法'android.view.View android.view.View.findViewById(int)' 在空对象引用上
【问题讨论】:
-
使用 view.findViewById(R.id.gorevNo) 代替 View curr = parent.getChildAt((int) id);
-
@mudit_sen 不幸的是,我又遇到了同样的错误
-
为什么要使用 getchildAt(id)
-
尝试使用位置来获取ChildAt(位置)
-
请添加列表视图的getView()方法。