单击打开弹出窗口的按钮时应用程序崩溃 window

App crashes when I click the button that opens a Popup window

我正在尝试创建一个弹出窗口,其中包含一个供用户输入新预算的 EditText 字段和一个用于保存该值的按钮。单击 "save" 按钮后,主 activity 中的 TextView 应根据用户在弹出窗口中输入的预算 (EditText) 进行更改。但是,当我在弹出窗口中为 "save" 按钮实现 OnClickListener 时,一旦我尝试从主 activity 导航到弹出窗口,应用程序就会崩溃。

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);
            }
        });


    }

上面的代码显示了从主 activity 打开 PopupWindow 的 OnClick 方法。而"button_savebudget"是保存值并更改主activity中的TextView。

logcat 如下:-

    --------- 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.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_savebudget" 是导致崩溃的原因,因为一旦我删除了那段特定的代码,应用程序就不会崩溃,但我不确定还需要更改什么来修复它!

尝试

Button button_savebudget = popupView.findViewById(R.id.budget_save);