NatJ
NatJ

Reputation: 47

Calling Android Intent from AsyncTask onPostExecute with context not working

I know this question has been asked by many but I've tried most of the solutions and none seem to work for me.

I am a newbie in android studio and I need to make an application which has a login.

I have 2 activities which are MainActivity and Menu and 3 classes(MainActivity.java,PHPconnect.java, and Menu.java)

In main activity I will ask for username and password and pass it to PHPconnect.java where the AsyncTask is located and if the login is successful it will redirect to Menu. The login syntax is already correct but when I input intent the application will close(stop working)

This is the code for calling the PHPconnect in MainActivity

PHPconnect phpconnect = new PHPconnect(this);
phpconnect.execute(type,username,password);

Below is the code in PHPconnect.java

public class PHPconnect extends AsyncTask<String, Void, String> {
    private Context context;
    PHPconnect(Context ctx){
    this.context = ctx;
    }

And this is the post execute method

protected void onPostExecute(String response) {
    alertDialog.setMessage(response);
    alertDialog.show();
    if (response.equals("Login success")){
            Intent i = new Intent(context,Menu.class);
            context.startActivity(i);
            ((Activity)context).finish();
       }
}

I have declared the Menu in manifest.xml

<activity
        android:name=".Menu"
        android:label="@string/title_activity_menu"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="com.checkpoint3.Menu" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

This is the logcat

05-05 10:28:10.272 10414-10414/com.checkpoint3 I/art: Not late-enabling -Xcheck:jni (already on)
05-05 10:28:10.273 10414-10414/com.checkpoint3 I/art: Late-enabling JIT
05-05 10:28:10.292 10414-10414/com.checkpoint3 I/art: JIT created with code_cache_capacity=2MB compile_threshold=1000
05-05 10:28:10.374 10414-10414/com.checkpoint3 W/System: ClassLoader referenced unknown path: /data/app/com.checkpoint3-1/lib/x86
05-05 10:28:10.515 10414-10425/com.checkpoint3 I/art: Background sticky concurrent mark sweep GC freed 10268(453KB) AllocSpace objects, 0(0B) LOS objects, 70% free, 717KB/2MB, paused 7.809ms total 99.611ms
05-05 10:28:10.680 10414-10428/com.checkpoint3 D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
05-05 10:28:17.149 10414-10428/com.checkpoint3 I/OpenGLRenderer: Initialized EGL, version 1.4
05-05 10:28:23.158 10414-10421/com.checkpoint3 W/art: Suspending all threads took: 32.197ms
05-05 10:28:25.046 10414-10428/com.checkpoint3 W/EGL_emulation: eglSurfaceAttrib not implemented
05-05 10:28:25.046 10414-10428/com.checkpoint3 W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xad9616c0, error=EGL_SUCCESS
05-05 10:28:25.149 10414-10414/com.checkpoint3 I/Choreographer: Skipped 843 frames!  The application may be doing too much work on its main thread.
05-05 10:28:36.395 10414-10414/com.checkpoint3 I/Choreographer: Skipped 674 frames!  The application may be doing too much work on its main thread.
05-05 10:28:40.531 10414-10421/com.checkpoint3 W/art: Suspending all threads took: 23.511ms
05-05 10:29:07.711 10414-10421/com.checkpoint3 W/art: Suspending all threads took: 13.287ms
05-05 10:29:23.956 10414-10428/com.checkpoint3 W/EGL_emulation: eglSurfaceAttrib not implemented
05-05 10:29:23.956 10414-10428/com.checkpoint3 W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa2d830c0, error=EGL_SUCCESS
05-05 10:29:25.016 10414-10414/com.checkpoint3 D/AndroidRuntime: Shutting down VM
05-05 10:29:25.017 10414-10414/com.checkpoint3 E/AndroidRuntime: FATAL EXCEPTION: main
                                                             Process: com.checkpoint3, PID: 10414
                                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{com.checkpoint3/com.checkpoint3.Menu}: java.lang.RuntimeException: For ExpandableListView, use setAdapter(ExpandableListAdapter) instead of setAdapter(ListAdapter)
                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                 at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                 at android.os.Looper.loop(Looper.java:148)
                                                                 at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                              Caused by: java.lang.RuntimeException: For ExpandableListView, use setAdapter(ExpandableListAdapter) instead of setAdapter(ListAdapter)
                                                                 at android.widget.ExpandableListView.setAdapter(ExpandableListView.java:554)
                                                                 at com.checkpoint3.Menu.onCreate(Menu.java:50)
                                                                 at android.app.Activity.performCreate(Activity.java:6237)
                                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                 at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                 at android.os.Looper.loop(Looper.java:148) 
                                                                 at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
05-05 10:29:31.764 10414-10414/com.checkpoint3 I/Process: Sending signal. PID: 10414 SIG: 9

Please help me

Upvotes: 1

Views: 940

Answers (2)

Vucko
Vucko

Reputation: 7479

I have a similar thing where I use a Login Activity to authenticate the user via OkHttp, and I do so in an AsyncTask also. When I receive the response from the server, if the response is 200 "OK", I do the following:

Intent navigationDrawerIntent = new Intent(LoginActivity.this, NavigationDrawerActivity.class);
            startActivity(navigationDrawerIntent);
            finish();

I do not pass the context to the AsyncTask and the class is in the same namespace as the LoginActivity. Try this and see if it works for you.

EDIT

I think I know what your problem is: your Activity's name is Menu. Rename it to MenuActivity (Right click on it->Refactor->Rename) because there's already a class in Android library called Menu and it may cause confusion.

It's a good practice anyway to call your Activities with a suffix 'Activity' and every other thing as well, like UsernameTextView (instead of Username) etc. It'll save you a lot of time on stupid errors.

A good reference to check if there's some overlook in the namespaces is this question.

Upvotes: 1

Rahman Mohammadi
Rahman Mohammadi

Reputation: 21

Your code is valid.

Here is your problem :

java.lang.RuntimeException: For ExpandableListView, use setAdapter(ExpandableListAdapter) instead of setAdapter(ListAdapter)

Somewhere in your code you are using ListAdapter for ExpandableListView.

You should use ExpandableListAdapter for ExpandableListView.

Upvotes: 1

Related Questions