【问题标题】:make clickable layout inflater?使可点击布局充气?
【发布时间】:2013-12-06 13:33:09
【问题描述】:

我正在使用此代码通过扩展布局来打开 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;
            }
        }
        });

错误日志:

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

【问题讨论】:

    标签: android layout android-activity layout-inflater


    【解决方案1】:

    您刚刚通过以下方式找到了您的按钮 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);
    

    【讨论】:

    • 代码中高亮显示,然而 mydisclamer.setOnClickListener (new Button.OnClickListener () {
    • 按钮是否处于免责声明布局中??
    • 哦...所以你必须从 Button mydisclamer = (Button)popupView.findViewById(R.id.binfo); 中删除 popupview;
    • 终于可以使用这个了:android-er.blogspot.in/2012/03/…
    • 我在这个Button中改了mydisclamer = (Button) findViewById (R.id.binfo) 因为否则不能调用布局。但布局中的按钮效果不一样
    【解决方案2】:

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

    【讨论】:

    • 方法 findViewById(int) 未定义 PopupWindow 类型?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多