使可点击的布局inflater?

时间:2013-12-06 13:33:10

标签: android layout android-activity layout-inflater

我正在使用此代码通过膨胀布局来打开一个Activity 但是没有单击活动中包含的按钮,就好像未正确调用任务一样,您可以修改此代码吗?或者你必须使用另一种方法? 提前致谢, 代码:

通过回答编辑

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

错误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

2 个答案:

答案 0 :(得分:3)

您刚刚找到了您的按钮ID:

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

并且您的按钮位于PopUp充气视图中..

改变:

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

所以改为:

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

答案 1 :(得分:0)

添加此行Button mydisclamer = (Button)cp.findViewById(R.id.binfo);