-1

当我尝试将活动连接到片段类时收到空指针错误..活动类是一个列表活动,当单击特定列表时,片段类应该打开..任何主体都有示例代码或解决方案可以提供给我..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

}

}

4

1 回答 1

3

You cannot start a fragment like starting a Activity , you have few options you can display/add it in a view or show it as a dialogFragment, Add a fragment in a view in your layout as

        ExampleFragment mFragment = new ExampleFragment();  // ExampleFragment is your fragment
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.add(R.id.fragmentContainer, mFragment).commit(); // fragmentContainer is id of your view in your layout

Execute above task in your onitemClickListener

In order to use DialogFragments check this http://android-developers.blogspot.com/2012/05/using-dialogfragments.html

    TermFragment frag= new TermFragment ();  
    frag.show(fm, "fragment_tag");
于 2013-02-12T13:38:11.753 回答