我是Android / Java的全新人物。我正在尝试创建一个应用程序,它将连接到我的TCP服务器以从mySQL获取数据。
我有以下内容:MainActivity - 这是登录屏幕。输入用户名和密码后,我连接到我的服务器
->startService(new Intent(this, tcpConnectionService.class));
服务器验证用户+ pwd,然后向我发送回复。 我正在分析响应,如果没问题,我想打开新的活动 - 对于某些数据请求。
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));
}
但不幸的是,我得到了例外......
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
答案 0 :(得分:1)
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。猜猜为什么:)
答案 1 :(得分:0)
在开始活动之前,只需将其添加到意图中:intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
答案 2 :(得分:0)
在此电话会议中:
mainContext.startActivity(new Intent(null, sn_search.class));
mainContext
必须是Activity
;将其设置为getApplicationContext()
会使其变为Context
而不是Activity
,这会导致异常。要解决此问题,请在mainContext
类的正文中的某处将this
(或其他某个字段)设置为Activity
。如果startActivity
调用本身发生在Activity
类(或Fragment
类)的主体内,则根本不需要对象;这很好:
startActivity(new Intent(null, sn_search.class));
(但可能需要使用Activity
而不是null
作为new Intent
的第一个参数;似乎有两个异常从您的应用中抛出。)< / p>