【发布时间】:2016-08-31 01:52:46
【问题描述】:
我知道很多人都问过这个问题,但我已经尝试了大多数解决方案,但似乎没有一个适合我。
我是android studio的新手,我需要做一个有登录的应用程序。
我有 2 个活动,分别是 MainActivity 和 Menu 以及 3 个类(MainActivity.java、PHPconnect.java 和 Menu.java)
在主要活动中,我将询问用户名和密码并将其传递给 AsyncTask 所在的 PHPconnect.java,如果登录成功,它将重定向到菜单。登录语法已经正确,但是当我输入意图时,应用程序将关闭(停止工作)
这是在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>
这是日志猫
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
请帮帮我
【问题讨论】:
-
在调用 asynctask 时尝试传递 Context 类型来代替它
-
将您的活动类作为上下文传递
-
尝试 Activity 而不是 Context。并在 PHPConnect 的构造函数中写入
MainActivity.this而不仅仅是this -
您的代码看起来有效。当应用程序崩溃时,您可以发布堆栈跟踪吗?还要确保你的清单文件中有互联网权限
-
你可以发布 logcat。
标签: java android android-intent android-asynctask