How to open an actitivity from a java Fragment

Asked

Viewed 239 times

0

How to open an Activity after clicking on a Listview that is in Fragment, I am using:

 public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);

        OnItemClickListener listener = new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

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

        getListView().setOnItemClickListener(listener);

    }

but the app stops after clicking. Please if anyone knows how to fix this I appreciate.

Logcat:

07-01 16:06:32.979: V/21st Polling:(408): clicked
07-01 16:06:36.489: V/21st Polling:(408): clicked
07-01 16:06:39.789: D/AndroidRuntime(408): Shutting down VM
07-01 16:06:39.789: W/dalvikvm(408): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-01 16:06:39.849: E/AndroidRuntime(408): FATAL EXCEPTION: main
07-01 16:06:39.849: E/AndroidRuntime(408): java.lang.RuntimeException: Unable to pause activity {com.example.appsherlok/com.example.appsherlok.MainActivity}: java.lang.NullPointerException
07-01 16:06:39.849: E/AndroidRuntime(408):  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3348)
07-01 16:06:39.849: E/AndroidRuntime(408):  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3305)
07-01 16:06:39.849: E/AndroidRuntime(408):  at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3288)
07-01 16:06:39.849: E/AndroidRuntime(408):  at android.app.ActivityThread.access$2500(ActivityThread.java:125)
07-01 16:06:39.849: E/AndroidRuntime(408):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2040)
07-01 16:06:39.849: E/AndroidRuntime(408):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-01 16:06:39.849: E/AndroidRuntime(408):  at android.os.Looper.loop(Looper.java:123)
07-01 16:06:39.849: E/AndroidRuntime(408):  at android.app.ActivityThread.main(ActivityThread.java:4627)
07-01 16:06:39.849: E/AndroidRuntime(408):  at java.lang.reflect.Method.invokeNative(Native Method)
07-01 16:06:39.849: E/AndroidRuntime(408):  at java.lang.reflect.Method.invoke(Method.java:521)
07-01 16:06:39.849: E/AndroidRuntime(408):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-01 16:06:39.849: E/AndroidRuntime(408):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-01 16:06:39.849: E/AndroidRuntime(408):  at dalvik.system.NativeStart.main(Native Method)
07-01 16:06:39.849: E/AndroidRuntime(408): Caused by: java.lang.NullPointerException
07-01 16:06:39.849: E/AndroidRuntime(408):  at android.support.v4.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1576)
07-01 16:06:39.849: E/AndroidRuntime(408):  at android.support.v4.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1617)
07-01 16:06:39.849: E/AndroidRuntime(408):  at android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:481)
07-01 16:06:39.849: E/AndroidRuntime(408):  at com.actionbarsherlock.app.SherlockFragmentActivity.onSaveInstanceState(SherlockFragmentActivity.java:126)
07-01 16:06:39.849: E/AndroidRuntime(408):  at android.app.Activity.performSaveInstanceState(Activity.java:1036)
07-01 16:06:39.849: E/AndroidRuntime(408):  at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1180)
07-01 16:06:39.849: E/AndroidRuntime(408):  at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3330)
07-01 16:06:39.849: E/AndroidRuntime(408):  ... 12 more

Friends, a detail! the app has 3 tabs, this listview is in the first. When I open the app displays the list, I click, and it works right. But when I go to another tab and go back to the list, I click and the error happens.

  • 1

    In principle it should not generate any error with this code, it could include the error log?

  • The way Cicero spoke in his reply is inefficient and consumes a lot of memory. If you have a Listview with 10,000 items, each of them will have a Listener implementation. Imagine how slow the scroll is. I suggest you edit the question with Stacktrace from Logcat, as the principle of your code is correct.

  • Agree with @Wakim, should not give error this code snippet. I think the error is in onPause of Fragment Activity.

  • @Marcelofilho you can be more specific?

  • When you are starting the new Activity, the current (fragment parent) is going through onPause. See if Null is not at this time.

  • friend @marcelofilho I am beginner in java, you can show me how to check and how to use?

  • @Emersonbarcellos, your app has an Activity that in turn has a fragment, which in turn has a correct listview? In that your Tax Code is over writing the onPause method? If yes see if inside it there is no problem with the code inside. If not, you can give more details on how you are implementing your app.

  • @Marcelofilho you noticed that I added more data to my question? where/how the error happens?

  • Dude, by the stack t giving error in the onPause of sherlok... Always give that same stack? This Sherlock is the action bar?

  • 1

    @Marcelofilho I think the problem was in the structure maybe in the tab Adapter. I change for that and did not present more error. Thank you very much guy!

  • Emerson, don’t you want to post your solution, in detail, in answer form below? This can help other people who might have a similar problem.

Show 6 more comments

1 answer

1

If your ListView have an Adapter, put the event in the custom view (Row).

But exemplifying your situation...

Create the event within the OnCreateView() even.

Consider the implementation of this function:

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {



}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.