当我尝试将活动连接到片段类时收到空指针错误..活动类是一个列表活动,当单击特定列表时,片段类应该打开..任何主体都有示例代码或解决方案可以提供给我..my片段类名是 Termfragment..
我在我的 listactivity 类中使用了这段代码:
public class HelpActivity extends ListActivity {
Context myContext=this;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
String[] helplist = getResources().getStringArray(R.array.helplist);
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_view, R.id.label, helplist));
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
TermFragment mytermfragment = new TermFragment(myContext);
getSupportFragmentManager().beginTransaction().add(R.id.mytermfragment_container, mytermfragment).commit();
mytermfragment.show(getSupportFragmentManager(),"mytermfragment");
}
});
}
}
TermFragment 类:
public class TermFragment extends DialogFragment implements DialogInterface.OnClickListener {
private Context termContext;
private AlertDialogListener mListener;
public TermFragment(Context context) {
termContext=context;
}
public interface AlertDialogListener
{
public void onDialogPositiveClick(DialogFragment dialogFragment);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Log.i("help","onCreateDialog");
AlertDialog.Builder builder = new AlertDialog.Builder(termContext);
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setTitle("Terms");
builder.setView(inflater.inflate(R.layout.terms_view, null));
builder.setPositiveButton("I AGREE", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mListener.onDialogPositiveClick(TermFragment.this);
}
});
AlertDialog alertDialog = builder.create();
Log.i("alert","alertDialog Created");
return alertDialog;
}
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}