Question

I'm using this code to open a Activity by inflating the layout but the buttons that are contained within the activity are not clicked, it is as if the task is not called correctly, you can modify this code? or you must use another method? Thanks in advance, Code:

Edit by answer

LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);  
        View popupView = inflater.inflate(R.layout.disclamer, null); cp = new PopupWindow(popupView,
                LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);  
        Button mydisclamer = (Button)popupView.findViewById(R.id.binfo);

        mydisclamer.setOnClickListener(new Button.OnClickListener(){ //problem at this line


        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub





            if (click){
                cp.showAtLocation(v, Gravity.CENTER, 0, 0);
                cp.update(0,0,500,500);
                click=false;
            }else{
                cp.dismiss();
                click=true;
            }
        }
        });

Error logcat:

12-06 15:00:25.608: E/AndroidRuntime(2891): FATAL EXCEPTION: main
12-06 15:00:25.608: E/AndroidRuntime(2891): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.bizzsound1/org.bizzsound1.menu}: java.lang.NullPointerException
12-06 15:00:25.608: E/AndroidRuntime(2891):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
12-06 15:00:25.608: E/AndroidRuntime(2891):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
12-06 15:00:25.608: E/AndroidRuntime(2891):     at android.app.ActivityThread.access$600(ActivityThread.java:140)
12-06 15:00:25.608: E/AndroidRuntime(2891):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
12-06 15:00:25.608: E/AndroidRuntime(2891):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-06 15:00:25.608: E/AndroidRuntime(2891):     at android.os.Looper.loop(Looper.java:137)
12-06 15:00:25.608: E/AndroidRuntime(2891):     at android.app.ActivityThread.main(ActivityThread.java:4898)
12-06 15:00:25.608: E/AndroidRuntime(2891):     at java.lang.reflect.Method.invokeNative(Native Method)
12-06 15:00:25.608: E/AndroidRuntime(2891):     at java.lang.reflect.Method.invoke(Method.java:511)
12-06 15:00:25.608: E/AndroidRuntime(2891):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
12-06 15:00:25.608: E/AndroidRuntime(2891):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
12-06 15:00:25.608: E/AndroidRuntime(2891):     at dalvik.system.NativeStart.main(Native Method)
12-06 15:00:25.608: E/AndroidRuntime(2891): Caused by: java.lang.NullPointerException
12-06 15:00:25.608: E/AndroidRuntime(2891):     at org.bizzsound1.menu.onCreate(menu.java:117)
12-06 15:00:25.608: E/AndroidRuntime(2891):     at android.app.Activity.performCreate(Activity.java:5206)
12-06 15:00:25.608: E/AndroidRuntime(2891):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
12-06 15:00:25.608: E/AndroidRuntime(2891):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
12-06 15:00:25.608: E/AndroidRuntime(2891):     ... 11 more
Was it helpful?

Solution

you have just find your button id by this:

 Button  mydisclamer = (Button)findViewById(R.id.binfo);

and your button is in your PopUp inflated view..

Change with this:

LayoutInflater inflater 
 = (LayoutInflater)this
  .getSystemService(LAYOUT_INFLATER_SERVICE);  
View popupView = layoutInflater.inflate(R.layout.disclamer, null);  
         cp = new PopupWindow(
           popupView, 
           LayoutParams.WRAP_CONTENT,  
                 LayoutParams.WRAP_CONTENT);  

So change it with:

Button mydisclamer = (Button)popupView.findViewById(R.id.binfo);

OTHER TIPS

add this line Button mydisclamer = (Button)cp.findViewById(R.id.binfo);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top