从Android中的片段启动PopupWindow实例作为类

时间:2014-12-05 19:26:21

标签: android android-fragments nullpointerexception popupwindow

当我尝试从FirstFragment调用showPopup2时,我得到了一个空指针异常。这是我的ShowPopup类,

public class ShowPopup extends Activity {
public void showPopup2(View v) {
    Button btnDismiss;
    LayoutInflater layoutInflater = (LayoutInflater)showPopup.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layout = layoutInflater.inflate(R.layout.popup_layout, null);
    PopupWindow popupWindow = new PopupWindow(layout, 580, 500, true); 
    popupWindow.showAtLocation(layout, Gravity.CENTER, 0, 40);
    btnDismiss=(Button) layout.findViewById(R.id.btnDismissxml);
    btnDismiss.setOnClickListener(new OnClickListener() {   

    @Override
    public void onClick(View arg0){
        popupWindow.dismiss();
        }
    });
}
} 

这是片段,

public class FirstFragment extends Fragment implements OnClickListener{
public static Context context;
Button btnPopup;
public ShowPopup ShowPopup;

@Override
public void onCreate(Bundle savedDataEntryInstanceState){
    super.onCreate(savedDataEntryInstanceState);
}

//@Override
public View onCreateView(LayoutInflater inflater{   
    View v = inflater.inflate(R.layout.first_fragment, container, false);   
    btnPopup = (Button)v.findViewById(R.id.btnPopupxml);
    btnPopup.setOnClickListener(this);
    populateFirstFragment(v);
    return v;
}

//@Override
public void onViewCreated(View v) {
    btnPopup = (Button)v.findViewById(R.id.btnPopupxml);
    btnPopup.setOnClickListener(this);
}

//@Override
public void onClick(View v) {
        ShowPopup.showPopup2(v);
    } 
}

导致空指针异常的原因是什么?有没有更好的方法来构建它?我的目标是能够(1)从我的应用程序中的其他方法访问它,以及(2)没有这种方法使我的碎片混乱。
提前谢谢....

2 个答案:

答案 0 :(得分:0)

你已经在FirstFragment中声明了一个ShowPopup Activity的实例,但是在你调用ShowPopup.showPopup2(v)时没有初始化它导致空指针。

此外,不应该像对象一样使用Activity来调用它上面的函数。您可以将showPopup2()函数放在FirstFragment本身中。

public class FirstFragment extends Fragment implements OnClickListener{
public static Context context;
Button btnPopup;


@Override
public void onCreate(Bundle savedDataEntryInstanceState){
super.onCreate(savedDataEntryInstanceState);
}

//@Override
public View onCreateView(LayoutInflater inflater{   
View v = inflater.inflate(R.layout.first_fragment, container, false);   
btnPopup = (Button)v.findViewById(R.id.btnPopupxml);
btnPopup.setOnClickListener(this);
populateFirstFragment(v);
return v;
}

//@Override
public void onViewCreated(View v) {
btnPopup = (Button)v.findViewById(R.id.btnPopupxml);
btnPopup.setOnClickListener(this);
}

//@Override
public void onClick(View v) {
    showPopup2(v);
} 

public void showPopup2(View v) {
Button btnDismiss;
LayoutInflater layoutInflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.popup_layout, null);
PopupWindow popupWindow = new PopupWindow(layout, 580, 500, true); 
popupWindow.showAtLocation(layout, Gravity.CENTER, 0, 40);
btnDismiss=(Button) layout.findViewById(R.id.btnDismissxml);
btnDismiss.setOnClickListener(new OnClickListener() {   

@Override
public void onClick(View arg0){
    popupWindow.dismiss();
    }
});
}

}

答案 1 :(得分:0)

这通常有效,

<强> ShowPopup.java

public class showPopup {
Context ctx;

public showPopup(Context ctx){
    this.ctx = ctx; 
}

public void goJoe(View parent){
    final PopupWindow popup = new PopupWindow(ctx);
    LayoutParams para = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    TextView tvMessage = new TextView(ctx);
    Button btnDismiss = new Button (ctx);

    tvMessage.setLayoutParams(para);
    tvMessage.setText("This is the popup window.");
    popup.setContentView(tvMessage);

    btnDismiss.findViewById(R.id.btnDismissxml);
    btnDismiss.setLayoutParams(para);
    btnDismiss.setWidth(120);
    btnDismiss.setHeight(20);
    btnDismiss.setText("Close popupWindow.java");
    popup.setContentView(btnDismiss);

    popup.setWidth(400);
    popup.setHeight(180);
    popup.showAtLocation(parent, Gravity.CENTER_HORIZONTAL, 10, 10);
    popup.update();

    btnDismiss.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            popup.dismiss();    
        }
    });
  }
}

<强> ThirdTab.java

public class ThirdTab extends Fragment implements OnClickListener{
Button btnPopup;
showPopup showPopup;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.thirdtab_fragment, container, false);    
    btnPopup = (Button)v.findViewById(R.id.btnPopupxml);
    btnPopup.setOnClickListener(this);
    return v;
}

//@Override
public void onViewCreated(View v) {
    btnPopup = (Button)v.findViewById(R.id.btnPopupxml);
    btnPopup.setOnClickListener(this);
}
//@Override
    @Override
    public void onClick(View parent) {
        new showPopup(getActivity().getApplicationContext()).goJoe(parent);
    }
}