简体   繁体   中英

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.

确保您已在 AndroidManifest.xml 中添加了您的 NewActivity。

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

当您处于片段中并用于打开活动时,请使用

context.startActivity(your_intent)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM