Im trying to get an AlertDialog to inflate from a customview. I think I know what the problem is but I dont know how to fix it. I have a (View V) on LongClick and Button1 is using "alertdialog" to find data. I'm getting a NullPointer Exception on line TextView stax1=(TextView)findViewById(R.id.xxx); The only thing that makes sense is I have the Dialog looking in "alerthelp" when it should looking in the activity it came from. How do I fix this?
lay1.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v)
{
LayoutInflater myLayout = LayoutInflater.from(context);
View dialogView = myLayout.inflate(R.layout.alerthelp, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialogBuilder.setView(dialogView);
alertDialog.show();
Button button1 = (Button)dialogView.findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
TextView stax1=(TextView)findViewById(R.id.xxx);
String sax1 = stax1.getText().toString();
double sx1 = Double.parseDouble(sax1);
TextView textviewlay1 =(TextView)findViewById(R.id.m1ab);
String stringl1 = textviewlay1.getText().toString();
Double doubl1 = Double.parseDouble(stringl1);
TextView textviewp1 = (TextView)findViewById(R.id.inputPrice);
String stringp1 = textviewp1.getText().toString();
Double intp1 = Double.parseDouble(stringp1);
double resultl1 = intp1 - (doubl1*sx1);
int precision21t = 100; //keep 4 digits
resultl1= Math.floor(resultl1 * precision21t +.5)/precision21t;
textViewtotalproduct.setText(String.valueOf(resultl1));
}
});
Button button2 = (Button) dialogView.findViewById(R.id.button2);
button2.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
TextView textviewlay1 =(TextView)findViewById(R.id.m1ab);
String stringl1 = textviewlay1.getText().toString();
Double doubl1 = Double.parseDouble(stringl1);
TextView textviewp1 = (TextView)findViewById(R.id.inputPrice);
String stringp1 = textviewp1.getText().toString();
Double intp1 = Double.parseDouble(stringp1);
double resultl1 = intp1 - doubl1;
int precision21t = 100; //keep 4 digits
resultl1= Math.floor(resultl1 * precision21t +.5)/precision21t;
textViewtotalproduct.setText(String.valueOf(resultl1));
}
});
Button button3 = (Button)dialogView.findViewById(R.id.button3);
button3.setOnClickListener(new OnClickListener(){
public void onClick(View v){
TextView ss1=(TextView)findViewById(R.id.s2);
ss1.setText("st");
}
});
Button button4 = (Button)dialogView.findViewById(R.id.button4);
button4.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
TextView ss1=(TextView)findViewById(R.id.s2);
ss1.setText("");
}
});
Button button5 = (Button)dialogView.findViewById(R.id.button5);
button5.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Intent mainIntent = new Intent(xxxActivity.this, SettingsActivity.class);
MenuView1Activity.this.startActivity(mainIntent);
MenuView1Activity.this.finish();
}
});
Button button6 = (Button)dialogView.findViewById(R.id.button6);
button6.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Intent mainIntent = new Intent(xxxActivity.this, SettingsActivity.class);
xxxActivity.this.startActivity(mainIntent);
xxxActivity.this.finish();
}
});
return false;
}});
It seems you are trying to load text from static TextView. May be it should be EditText?
TextView stax1=(TextView)dialogView.findViewById(R.id.xxx);
String sax1 = stax1.getText().toString();
EDITED:
Try this code:
lay1.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v)
{
LayoutInflater myLayout = LayoutInflater.from(context);
final View dialogView = myLayout.inflate(R.layout.alerthelp, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
alertDialogBuilder.setView(dialogView);
AlertDialog alertDialog = alertDialogBuilder.create();
Button button1 = (Button) alertDialog.findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
TextView stax1=(TextView)dialogView.findViewById(R.id.xxx);
String sax1 = stax1.getText().toString();
double sx1 = Double.parseDouble(sax1);
TextView textviewlay1 =(TextView)dialogView.findViewById(R.id.m1ab);
String stringl1 = textviewlay1.getText().toString();
Double doubl1 = Double.parseDouble(stringl1);
TextView textviewp1 = (TextView)dialogView.findViewById(R.id.inputPrice);
String stringp1 = textviewp1.getText().toString();
Double intp1 = Double.parseDouble(stringp1);
double resultl1 = intp1 - (doubl1*sx1);
int precision21t = 100; //keep 4 digits
resultl1= Math.floor(resultl1 * precision21t +.5)/precision21t;
textViewtotalproduct.setText(String.valueOf(resultl1));
}
});
return false;
}});
Change
TextView stax1=(TextView)findViewById(R.id.xxx);
to
TextView stax1=(TextView)dialogView.findViewById(R.id.xxx);
Hope this helps.
来源:https://stackoverflow.com/questions/17338362/using-layoutinflater-and-alertdialog