Criei esse código aqui mas algo está errado, pois toda vez que clico no botão que ativa o popup o app fecha.
//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;
}
});
}
});
Erro que aparece:
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
relativeLayoutPesquisa
é nulo, verifique serelativeLayoutPesquisa = (RelativeLayout) findViewById(R.id.activity_pop_up);
está correcto.