0

当我尝试从FirstFragment调用showPopup2时,我得到一个空指针异常。这是我的ShowPopup类,从Android中的片段启动PopupWindow的实例作为一个类中的类

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)不会让此方法混乱我的碎片。
在此先感谢....

回答

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); 
    } 
} 
0

你已经宣布你FirstFragment的ShowPopup活动的情况,但还没有初始化这个,当你调用ShowPopup.showPopup2这是造成空指针(V)。
另外,一个活动不应该像对象一样用来调用它的函数。您可以将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(); 
    } 
}); 
} 

} 
+0

我强烈希望在有showPopup()单独的文件。任何有关最佳方式的建议?谢谢。 – portsample 2014-12-05 20:01:53

相关问题