-1

I'm trying to have a popupwindow that has an EditText field for user to enter a new budget and a button to save the value. Once the "save" button is clicked, the TextView in the main activity should change according to what the user input in the budget (EditText) in the popupwindow. But when I implement the OnClickListener in for the "save" button in the popupwindow, the app crashes once I try to navigate to the popupwindow from the main 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);
            }
        });


    }

The code above shows the OnClick method that opens up the PopupWindow from the main activity. And the "button_savebudget" is to save the value and change the TextView in the main activity.

The logcat is below :-

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

I'm pretty sure the "button_savebudget" is the one that causes the crash because once I remove that particular piece of code the app doesn't crash but I'm not sure what else to change to fix it !

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
shz1998
  • 53
  • 8

1 Answers1

1

Try

Button button_savebudget = popupView.findViewById(R.id.budget_save);
Liar
  • 1,235
  • 1
  • 9
  • 19