PopUp menu in android having null pointer exception
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);
Button b is working but b2 is not working i am making an android app in
which i require a pop up window on the click of the button //but it is
giving error i have also used try and catch block but it is still giving
null pointer exception try to get the popup on a click of button and in
pop up again there is a button which close the pop up but it is not
woking. So please help me for this.
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();
}
}
}
No comments:
Post a Comment