Question

I am working on a popup window, i am having two imagebuttons.at start all is looking good but when i put onclick event on my both image buttons than my app is crashed. i Don't understand why onclick is givving error. following is my code:

protected void popup() {

RelativeLayout viewGroup(RelativeLayout)MainActivity.this.findViewById(R.id.popup); 
LayoutInflater layoutInflater = (LayoutInflater)MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.popup, viewGroup);
final PopupWindow popup = new PopupWindow(this);
popup.setContentView(layout);

popup.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
popup.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
popup.setBackgroundDrawable(null);
popup.setFocusable(true);   
popup.showAtLocation(layout, Gravity.CENTER, 1, 1);
popcash = (ImageButton )findViewById(R.id.popcash);
popcash.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {


        Toast.makeText(getApplicationContext(), "on click image button", Toast.LENGTH_LONG).show();

    }
});

popcoin = (ImageButton) findViewById(R.id.popcoin);
popcoin.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        Log.e("hi", "there");

    }
});



}

but i am getting following error

04-25 17:18:13.856: E/AndroidRuntime(1521): java.lang.NullPointerException
04-25 17:18:13.856: E/AndroidRuntime(1521):     at         com.example.scratchcarddemo.MainActivity.popup(MainActivity.java:304)
04-25 17:18:13.856: E/AndroidRuntime(1521):     at com.example.scratchcarddemo.MainActivity$2.onTouch(MainActivity.java:225)
04-25 17:18:13.856: E/AndroidRuntime(1521):     at android.view.View.dispatchTouchEvent(View.java:3762)
04-25 17:18:13.856: E/AndroidRuntime(1521):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
04-25 17:18:13.856: E/AndroidRuntime(1521):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
04-25 17:18:13.856: E/AndroidRuntime(1521):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
04-25 17:18:13.856: E/AndroidRuntime(1521):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
04-25 17:18:13.856: E/AndroidRuntime(1521):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
04-25 17:18:13.856: E/AndroidRuntime(1521):     at     com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1671)
04-25 17:18:13.856: E/AndroidRuntime(1521):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
04-25 17:18:13.856: E/AndroidRuntime(1521):     at android.app.Activity.dispatchTouchEvent(Activity.java:2086)
04-25 17:18:13.856: E/AndroidRuntime(1521):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1655)
04-25 17:18:13.856: E/AndroidRuntime(1521):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1785)
04-25 17:18:13.856: E/AndroidRuntime(1521):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-25 17:18:13.856: E/AndroidRuntime(1521):     at android.os.Looper.loop(Looper.java:123)
04-25 17:18:13.856: E/AndroidRuntime(1521):     at android.app.ActivityThread.main(ActivityThread.java:4627)
04-25 17:18:13.856: E/AndroidRuntime(1521):     at java.lang.reflect.Method.invokeNative(Native Method)
04-25 17:18:13.856: E/AndroidRuntime(1521):     at java.lang.reflect.Method.invoke(Method.java:521)
04-25 17:18:13.856: E/AndroidRuntime(1521):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-25 17:18:13.856: E/AndroidRuntime(1521):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-25 17:18:13.856: E/AndroidRuntime(1521):     at dalvik.system.NativeStart.main(Native Method)
Was it helpful?

Solution

You should change this

popcash = (ImageButton)findViewById(R.id.popcash);

With

popcash = (ImageButton)layout.findViewById(R.id.popcash);

and also change this

popcoin = (ImageButton) findViewById(R.id.popcoin);

with

popcoin = (ImageButton) layout.findViewById(R.id.popcoin);

It's because your both buttons coming from view layout that you inflated.

OTHER TIPS

Use this

   popcash = (ImageButton )popup.findViewById(R.id.popcash);
   popcoin = (ImageButton)popup.findViewById(R.id.popcoin);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top