In Android in litemitemclick I am using this piece of code:
TextView tvofpostedText = (TextView)findViewById(R.id.postedgtext);
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
long arg3) {
final AlertDialog.Builder builder = new AlertDialog.Builder(
DemoPage2.this);
LayoutInflater factory = LayoutInflater.from(DemoPage2.this);
final View textEntryView = factory.inflate(
R.layout.layoutforalertdialog, null);
// builder.setMessage("" + s.get(pos))
try {
tvofpostedText.setText("username:" + s.get(pos));
} catch (Exception e) {
// do nothing
}
builder.setMessage("" + username + (CharSequence) s.get(pos))
.setCancelable(true)
.setTitle("post Detail")
.setView(textEntryView)
.setPositiveButton("View Comments",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
// ListViewActivity.this.finish();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
}
})
.setNeutralButton("Post Comments",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
}
});
AlertDialog alert = builder.create();
alert.show();
tvofpostedText.setText("username:" + s.get(pos));
}
});
s is an ArrayList. I am getting a NullPointerException on the line tvofpostedText.setText("username:" + s.get(pos));. At least “username” should have been set as text even if s.get(pos) is null. Please help me out.In the logcat i am getting null pointer exception.tvofpostedText is comming null.Plese HELP>
Well, you’ve got two possibilites, neither of which anyone here can determine with the code you’ve given.
Figure out which one is null and fix that. You don’t show where either of these is declared, but that’s the only two possibilities given the line it’s crashing at.