-1

I receive the null pointer error when i tried to connect an activity to an fragment class.. activity class is an listactivity and when clicking on particular list, fragment class should open..any body has sample code or solution can provide me..my fragment class name is Termfragment..

i used this code in my listactivity class:

           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 class:

          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

}

}

user
  • 17
  • 5
  • Is TermFragment is only a Fragment class (say extends Fragment)...?? If so.. you will need a activity using the "TermFragment" or FragmentActivity. – Arpit Garg Feb 12 '13 at 13:22

1 Answers1

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");
baboo
  • 1,983
  • 17
  • 23
  • thankx for reply..i have done as u said but i receive null pointer exception error in the line: FragmentTransaction ft = getFragmentManager().beginTransaction(); – user Feb 13 '13 at 07:26
  • Check http://stackoverflow.com/questions/13213693/getfragmentmanager-returns-null-with-android-support-v4-app-dialogfragment-su – baboo Feb 13 '13 at 07:40
  • and also http://stackoverflow.com/questions/10477997/difference-between-activity-and-fragmentactivity – baboo Feb 13 '13 at 08:02
  • I used support library and no problem with that..just i get null pointer exception error – user Feb 13 '13 at 08:29
  • no need to add fragment in container when using dialog Fragment, i have edited my answer, plz check, GOOD LUCK – baboo Feb 13 '13 at 08:34