android中的PopUp菜单有空指针异常

时间:2013-09-11 09:21:14

标签: java android

package com.example.popupwindow;

import android.os.Bundle;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupWindow;

public class MainActivity extends Activity {

private PopupWindow pwindow;
private Button b,b2;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    b=(Button)findViewById(R.id.openpopup);

按钮b正在工作,但b2无效     我正在制作一个Android应用程序,其中我需要一个弹出窗口点击按钮//但它给出错误     我也使用了try和catch块,但它仍然给出了空指针异常        尝试通过点击按钮获得弹出窗口,然后再次弹出一个按钮,关闭弹出窗口,但它不是在按下。     所以请帮助我。

b.setOnClickListener(new OnClickListener() {

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

}




 //if i remove thr try and catch block then it is not working..see it for b2
    //problem is comming in b2 as click event is not been generated fro that
public void initiatePopUpWindow(){
    try{
            LayoutInflater linflater=(LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
            View popupview=linflater.inflate(R.layout.popup,null);
            pwindow=new PopupWindow(popupview,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
            pwindow.showAsDropDown(b, 200,50);

            b2=(Button)findViewById(R.id.dismiss);
            b2.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    pwindow.dismiss();
                }
            });
        }catch(Exception e){
            e.printStackTrace();
    }
}

} 以下是我的错误日志

09-11 05:28:24.249: W/System.err(449): java.lang.NullPointerException
09-11 05:28:24.269: W/System.err(449):  at com.example.popupwindow.MainActivity.initiatePopUpWindow(MainActivity.java:43)
09-11 05:28:24.269: W/System.err(449):  at com.example.popupwindow.MainActivity$1.onClick(MainActivity.java:29)
09-11 05:28:24.269: W/System.err(449):  at android.view.View.performClick(View.java:4240)
09-11 05:28:24.288: W/System.err(449):  at android.view.View$PerformClick.run(View.java:17721)
09-11 05:28:24.288: W/System.err(449):  at android.os.Handler.handleCallback(Handler.java:730)
09-11 05:28:24.288: W/System.err(449):  at android.os.Handler.dispatchMessage(Handler.java:92)
09-11 05:28:24.300: W/System.err(449):  at android.os.Looper.loop(Looper.java:137)
09-11 05:28:24.300: W/System.err(449):  at android.app.ActivityThread.main(ActivityThread.java:5103)
09-11 05:28:24.300: W/System.err(449):  at java.lang.reflect.Method.invokeNative(Native Method)
09-11 05:28:24.300: W/System.err(449):  at java.lang.reflect.Method.invoke(Method.java:525)
09-11 05:28:24.309: W/System.err(449):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-11 05:28:24.309: W/System.err(449):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)

my main file-
public class MainActivity extends Activity {

    private PopupWindow pwindow;
    private Button b,b2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        b=(Button)findViewById(R.id.openpopup);

        b.setOnClickListener(new OnClickListener() {

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

    }

    public void initiatePopUpWindow(){
        try{
                LayoutInflater linflater=(LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
                View popupview=linflater.inflate(R.layout.popup,null);
                pwindow=new PopupWindow(popupview,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
                pwindow.showAsDropDown(b, 200,50);

                b2=(Button)popupview.findViewById(R.id.dismiss);
                b2.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        pwindow.dismiss();
                    }
                });
            }catch(Exception e){
                e.printStackTrace();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}


my popup.xml file-
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    android:background="@android:color/background_light">
 <LinearLayout 
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:orientation="vertical" 
     android:layout_margin="1dp"
     android:background="@android:color/darker_gray">
     >
     <LinearLayout 
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:orientation="vertical" 
      android:layout_margin="20dp">
      <TextView
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="It's a PopupWindow" />
      <ImageView
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:src="@drawable/ic_launcher" />
      <Button
          android:id="@+id/dismiss"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="Dismiss" />
      </LinearLayout>
 </LinearLayout>
 </LinearLayout

&GT;

1 个答案:

答案 0 :(得分:0)

使用此...

b2=(Button)popupview.findViewById(R.id.dismiss);