gpt4 book ai didi

java - 致命异常主要 - startActivity

转载 作者:行者123 更新时间:2023-12-01 13:11:17 26 4
gpt4 key购买 nike

我对 Android/Java 完全陌生。我正在尝试创建一个应用程序来连接到我的 TCP 服务器以从 mySQL 获取数据。

我有以下内容:MainActivity - 这是登录屏幕。输入用户名和密码后,我连接到我的服务器

->startService(new Intent(this, tcpConnectionService.class));

服务器验证用户+密码,并向我发回响应。我正在分析响应,如果可以,我想打开新的 Activity - 用于某些数据请求。

static public void LOGIN_RESPONSE (String data)
{
Toast.makeText(mainContext, data, Toast.LENGTH_LONG).show();
//open search activity
if (data.equals("LOGIN_OK"))
mainContext.startActivity(new Intent(null, sn_search.class));
}

但不幸的是,我遇到了异常(exception)......

04-03 19:05:11.008: E/AndroidRuntime(807): FATAL EXCEPTION: main
04-03 19:05:11.008: E/AndroidRuntime(807): java.lang.IllegalStateException: Could not execute method of the activity
04-03 19:05:11.008: E/AndroidRuntime(807): at android.view.View$1.onClick(View.java:2144)
04-03 19:05:11.008: E/AndroidRuntime(807): at android.view.View.performClick(View.java:2485)
04-03 19:05:11.008: E/AndroidRuntime(807): at android.view.View$PerformClick.run(View.java:9080)
04-03 19:05:11.008: E/AndroidRuntime(807): at android.os.Handler.handleCallback(Handler.java:587)
04-03 19:05:11.008: E/AndroidRuntime(807): at android.os.Handler.dispatchMessage(Handler.java:92)
04-03 19:05:11.008: E/AndroidRuntime(807): at android.os.Looper.loop(Looper.java:123)
04-03 19:05:11.008: E/AndroidRuntime(807): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-03 19:05:11.008: E/AndroidRuntime(807): at java.lang.reflect.Method.invokeNative(Native Method)
04-03 19:05:11.008: E/AndroidRuntime(807): at java.lang.reflect.Method.invoke(Method.java:507)
04-03 19:05:11.008: E/AndroidRuntime(807): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-03 19:05:11.008: E/AndroidRuntime(807): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-03 19:05:11.008: E/AndroidRuntime(807): at dalvik.system.NativeStart.main(Native Method)
04-03 19:05:11.008: E/AndroidRuntime(807): Caused by: java.lang.reflect.InvocationTargetException
04-03 19:05:11.008: E/AndroidRuntime(807): at java.lang.reflect.Method.invokeNative(Native Method)
04-03 19:05:11.008: E/AndroidRuntime(807): at java.lang.reflect.Method.invoke(Method.java:507)
04-03 19:05:11.008: E/AndroidRuntime(807): at android.view.View$1.onClick(View.java:2139)
04-03 19:05:11.008: E/AndroidRuntime(807): ... 11 more
04-03 19:05:11.008: E/AndroidRuntime(807): Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
04-03 19:05:11.008: E/AndroidRuntime(807): at android.app.ContextImpl.startActivity(ContextImpl.java:621)
04-03 19:05:11.008: E/AndroidRuntime(807): at android.content.ContextWrapper.startActivity(ContextWrapper.java:258)
04-03 19:05:11.008: E/AndroidRuntime(807): at kushnir.mkssql.MainActivity.LOGIN_RESPONSE(MainActivity.java:84)
04-03 19:05:11.008: E/AndroidRuntime(807): at kushnir.mkssql.MainActivity.onClick(MainActivity.java:59)
04-03 19:05:11.008: E/AndroidRuntime(807): ... 14 more
04-03 19:38:53.818: E/AndroidRuntime(3311): FATAL EXCEPTION: main
04-03 19:38:53.818: E/AndroidRuntime(3311): java.lang.IllegalStateException: Could not execute method of the activity
04-03 19:38:53.818: E/AndroidRuntime(3311): at android.view.View$1.onClick(View.java:2144)
04-03 19:38:53.818: E/AndroidRuntime(3311): at android.view.View.performClick(View.java:2485)
04-03 19:38:53.818: E/AndroidRuntime(3311): at android.view.View$PerformClick.run(View.java:9080)
04-03 19:38:53.818: E/AndroidRuntime(3311): at android.os.Handler.handleCallback(Handler.java:587)
04-03 19:38:53.818: E/AndroidRuntime(3311): at android.os.Handler.dispatchMessage(Handler.java:92)
04-03 19:38:53.818: E/AndroidRuntime(3311): at android.os.Looper.loop(Looper.java:123)
04-03 19:38:53.818: E/AndroidRuntime(3311): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-03 19:38:53.818: E/AndroidRuntime(3311): at java.lang.reflect.Method.invokeNative(Native Method)
04-03 19:38:53.818: E/AndroidRuntime(3311): at java.lang.reflect.Method.invoke(Method.java:507)
04-03 19:38:53.818: E/AndroidRuntime(3311): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-03 19:38:53.818: E/AndroidRuntime(3311): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-03 19:38:53.818: E/AndroidRuntime(3311): at dalvik.system.NativeStart.main(Native Method)
04-03 19:38:53.818: E/AndroidRuntime(3311): Caused by: java.lang.reflect.InvocationTargetException
04-03 19:38:53.818: E/AndroidRuntime(3311): at java.lang.reflect.Method.invokeNative(Native Method)
04-03 19:38:53.818: E/AndroidRuntime(3311): at java.lang.reflect.Method.invoke(Method.java:507)
04-03 19:38:53.818: E/AndroidRuntime(3311): at android.view.View$1.onClick(View.java:2139)
04-03 19:38:53.818: E/AndroidRuntime(3311): ... 11 more
04-03 19:38:53.818: E/AndroidRuntime(3311): Caused by: java.lang.NullPointerException
04-03 19:38:53.818: E/AndroidRuntime(3311): at android.content.ComponentName.<init>(ComponentName.java:75)
04-03 19:38:53.818: E/AndroidRuntime(3311): at android.content.Intent.<init>(Intent.java:2702)
04-03 19:38:53.818: E/AndroidRuntime(3311): at kushnir.mkssql.MainActivity.LOGIN_RESPONSE(MainActivity.java:82)
04-03 19:38:53.818: E/AndroidRuntime(3311): at kushnir.mkssql.MainActivity.onClick(MainActivity.java:57)
04-03 19:38:53.818: E/AndroidRuntime(3311): ... 14 more

最佳答案

new Intent(null, sn_search.class)

为什么为空?您应该指定上下文。

看看你的异常,它告诉你问题出在哪里:

Caused by: java.lang.NullPointerException
at android.content.ComponentName.<init>(ComponentName.java:75)
at android.content.Intent.<init>(Intent.java:2702)

Intent 的构造函数引发了 NullPointerException。猜猜为什么:)

关于java - 致命异常主要 - startActivity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22847509/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com