Create popUp window on android?

0

I created this code here but something is wrong, because every time I click the button that activates the popup the app closes.

 //Button
private Button pesquisar;
private PopupWindow popUpWindowPesquisa;
private LayoutInflater layoutInflaterPesquisa;
private RelativeLayout relativeLayoutPesquisa;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Button pesquisar and popup
    pesquisar = (Button) findViewById(R.id.pesquisar);
    relativeLayoutPesquisa = (RelativeLayout) findViewById(R.id.activity_pop_up);
    pesquisar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            layoutInflaterPesquisa = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
            ViewGroup containerPesquisa = (ViewGroup) layoutInflaterPesquisa.inflate(R.layout.activity_pop_up, null);
            popUpWindowPesquisa = new PopupWindow(containerPesquisa, 600, 800, true);
            popUpWindowPesquisa.showAtLocation(relativeLayoutPesquisa, Gravity.NO_GRAVITY, 500, 500);
            containerPesquisa.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    popUpWindowPesquisa.dismiss();
                    return true;

                }
            });
        }
    });

Error appearing:

FATAL EXCEPTION: main java.lang.NullPointerException
    at android.widget.PopupWindow.showAtLocation(PopupWindow.java:811)
    at study.popup.MainActivity$1.onClick(MainActivity.java:41)
    at android.view.View.performClick(View.java:4240)
    at android.view.View$PerformClick.run(View.java:17721)
    at android.os.Handler.handleCallback(Handler.java:730)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5103)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    at dalvik.system.NativeStart.main(Native Method)
    10
    
asked by anonymous 29.10.2016 / 20:58

0 answers