从AsyncTask onPostExecute调用Android Intent,上下文不起作用

时间:2016-05-05 10:09:36

标签: java android android-intent android-asynctask

我知道很多人已经问过这个问题,但是我已经尝试了大部分解决方案,似乎没有一个对我有用。

我是android studio的新手,我需要创建一个登录的应用程序。

我有2个活动,分别是MainActivity和Menu以及3个类(MainActivity.java,PHPconnect.java和Menu.java)

在主要活动中,我会询问用户名和密码,并将其传递给AsyncTask所在的PHPconnect.java,如果登录成功,它将重定向到Menu。登录语法已经正确,但是当我输入intent时,应用程序将关闭(停止工作)

这是在MainActivity中调用PHPconnect的代码

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

以下是PHPconnect.java中的代码

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

这是后执行方法

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();
       }
}

我在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>

这是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

请帮帮我

2 个答案:

答案 0 :(得分:1)

我有类似的事情,我使用登录活动通过OkHttp对用户进行身份验证,我也在AsyncTask进行身份验证。当我收到服务器的响应时,如果响应是200&#34; OK&#34;,我会执行以下操作:

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

我没有将上下文传递给AsyncTask,并且该类与LoginActivity位于同一名称空间中。试试这个,看看它是否适合你。

修改

我想我知道你的问题是什么:你的活动名称是Menu。将其重命名为MenuActivityRight click on it->Refactor->Rename),因为Android库中已经有一个名为Menu的类,可能会引起混淆。

  

无论如何,使用后缀&#39;活动&#39;来调用您的活动是一种很好的做法。以及其他所有内容,例如UsernameTextView(而不是Username)等。它可以为您节省大量的愚蠢错误。

检查命名空间中是否有一些忽略的好参考是this question.

答案 1 :(得分:1)

您的代码有效。

这是你的问题:

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

您的代码中某处使用了ListAdapter for ExpandableListView。

您应该将ExpandableListAdapter用于ExpandableListView。