I can't find the mistake. I want to set the text of a textview. But when I'm setting the text I get an error message (see below for this). What I'm doing wrong? A little further down I call a button. This call works perfectly. What am I doing wrong when I call up the text view and set the text of this?
private void popUp(int layout, int button) {
epicDialog.setContentView(layout);
epicDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
TextView test = (TextView) epicDialog.findViewById(R.id.textView_popup);
test.setText("Hello");
....
Button button;
button= epicDialog.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do something
}
});
}
the error
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
you forgot to show the dialog before finding the textview.
private void popUp(int layout, int button) {
epicDialog.setContentView(layout);
epicDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
epicDialog.show();
TextView test = (TextView) epicDialog.findViewById(R.id.textView_popup);
test.setText("Hello");
....
Button button;
button= epicDialog.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do something
}
});
}
I think you add wrong id in java code. check your xml.
android:id="@+id/textView"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.