Create popup window on android?

Asked

Viewed 419 times

0

I created this code here but something is wrong, because every time I click on 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 that appears:

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
  • What’s the mistake ?

  • As I mentioned in the question when I click the button the app closes.

  • Sorry, it’s the nullpointerexception.

  • It seems that relativeLayoutPesquisa is void, check whether relativeLayoutPesquisa = (RelativeLayout) findViewById(R.id.activity_pop_up); is correct.

  • The worst is that it is. The name of xml is yes.

  • Danilo, could put the two xmls that are referenced in this activity: the activity_main and the activity_pop_up?

  • Brother, I’ll pass this because I found another solution here. I changed td to something simpler. A friend of mine made with a Fragment and an inflate.

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.