ESM
ESM

Reputation: 185

Open an Activity from a Fragment List Item

So there are many topics with a similar question to the one I'm posting, and all the answers are typically as follows:

Intent intent = new Intent(getActivity(), NewActivity.class);
startActivity(intent);

I understand this much, but for some reason my app keeps crashing every time I click on a list item.

Here's my code:

        listViewer.setAdapter(itemListAdapter);
        listViewer.setOnItemClickListener(new 
                   AdapterView.OnItemClickListener()
 {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                long arg3) {
        //  Toast.makeText(getActivity(), "Test button click", 
        //  Toast.LENGTH_SHORT).show();


          Intent intent = new Intent(getActivity(), NewActivity.class);
          startActivity(intent);
        }
    });

The Toast code that's commented out does work as intended, however when I try to call a new activity, it just crashes. I even tried it on a new empty activity to no avail. No idea why the regular "start new intent" isn't working and I've looked all over this site and others and nobody seems to have the same problem I'm experiencing.

Any help is appreciated, thanks.

Upvotes: 2

Views: 94

Answers (2)

Nandu
Nandu

Reputation: 63

As you are in fragment and for opening an activity use

context.startActivity(your_intent)

Upvotes: 0

Sifat Ullah Chowdhury
Sifat Ullah Chowdhury

Reputation: 81

Make sure that you have added your NewActivity in the AndroidManifest.xml.

<activity android:label="@string/app_name" android:name="NewActivity"/>

Upvotes: 1

Related Questions