Android 当我单击打开弹出窗口的按钮时,应用程序崩溃
我正在尝试一个弹出窗口,它有一个EditText字段供用户输入新预算,还有一个保存值的按钮。单击“保存”按钮后,主活动中的文本视图应根据用户在弹出窗口的预算(EditText)中输入的内容进行更改。但是,当我为popupwindow中的“保存”按钮实现OnClickListener时,一旦我尝试从主活动导航到popupwindow,应用程序就会崩溃Android 当我单击打开弹出窗口的按钮时,应用程序崩溃,android,button,nullpointerexception,crash,popupwindow,Android,Button,Nullpointerexception,Crash,Popupwindow,我正在尝试一个弹出窗口,它有一个EditText字段供用户输入新预算,还有一个保存值的按钮。单击“保存”按钮后,主活动中的文本视图应根据用户在弹出窗口的预算(EditText)中输入的内容进行更改。但是,当我为popupwindow中的“保存”按钮实现OnClickListener时,一旦我尝试从主活动导航到popupwindow,应用程序就会崩溃 public void onButtonShowPopupWindowClick(View view) { // inflate
public void onButtonShowPopupWindowClick(View view) {
// inflate the layout of the popup window
LayoutInflater inflater = (LayoutInflater)
getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = inflater.inflate(R.layout.spending_budgetpopup, null);
// create the popup window
int width = RelativeLayout.LayoutParams.WRAP_CONTENT;
int height = RelativeLayout.LayoutParams.WRAP_CONTENT;
boolean focusable = true; // lets taps outside the popup also dismiss it
final PopupWindow popupWindow = new PopupWindow(popupView, width, height, focusable);
// show the popup window
// which view you pass in doesn't matter, it is only used for the window tolken
popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
// dismiss the popup window when touched
popupView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
popupWindow.dismiss();
return true;
}
});
Button button_savebudget = findViewById(R.id.budget_save);
button_savebudget.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String budget = newbudget.getText().toString();
amount_budget.setText(budget);
}
});
}
上面的代码显示了从主活动打开PopupWindow的OnClick方法。“button_savebudget”用于保存值并更改主活动中的文本视图
public void onButtonShowPopupWindowClick(View view) {
// inflate the layout of the popup window
LayoutInflater inflater = (LayoutInflater)
getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = inflater.inflate(R.layout.spending_budgetpopup, null);
// create the popup window
int width = RelativeLayout.LayoutParams.WRAP_CONTENT;
int height = RelativeLayout.LayoutParams.WRAP_CONTENT;
boolean focusable = true; // lets taps outside the popup also dismiss it
final PopupWindow popupWindow = new PopupWindow(popupView, width, height, focusable);
// show the popup window
// which view you pass in doesn't matter, it is only used for the window tolken
popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
// dismiss the popup window when touched
popupView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
popupWindow.dismiss();
return true;
}
});
Button button_savebudget = findViewById(R.id.budget_save);
button_savebudget.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String budget = newbudget.getText().toString();
amount_budget.setText(budget);
}
});
}
日志如下:-
--------- beginning of crash
10-11 16:08:25.552 4980-4980/com.example.hannzern1998.spendingtest E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.hannzern1998.spendingtest, PID: 4980
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.hannzern1998.spendingtest.MainActivity.onButtonShowPopupWindowClick(MainActivity.java:100)
at com.example.hannzern1998.spendingtest.MainActivity$1.onClick(MainActivity.java:44)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
我很确定“button\u savebudget”是导致崩溃的原因,因为一旦我删除了那段特定的代码,应用程序就不会崩溃,但我不确定还需要修改什么来修复它 试试看
Button button_savebudget = popupView.findViewById(R.id.budget_save);
为什么不使用自定义对话框?请尝试此按钮\u savebudget=popupView。findViewById(R.id.budget\u save);这管用!谢谢!!