IgorKushnir
IgorKushnir

Reputation: 89

Fatal Exception Main - startActivity

I am totally new at Android / Java. I am trying to create an app that will connect to my TCP server to get data from mySQL.

I have the following: MainActivity - this is login screen. After entering the username and password, I connect to my server

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

The server verifies the user+pwd, and send me back response. I am analyzing the response and if it is OK, I want to open new activity - for some data request.

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

but unfortunately, I get the 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

Upvotes: 0

Views: 774

Answers (3)

ajb
ajb

Reputation: 31699

In this call:

mainContext.startActivity(new Intent(null, sn_search.class));

mainContext needs to be an Activity; setting it to getApplicationContext() makes it a different kind of Context instead of an Activity, which leads to the exception. To resolve this, either set mainContext (or some other field) to this somewhere in the body of the Activity class. If the startActivity call itself occurs inside the body of your Activity class (or a Fragment class), you don't need an object at all; this is good enough:

startActivity(new Intent(null, sn_search.class));

(but it's probably necessary to use an Activity, instead of null, as the first parameter of new Intent; there seem to be two exceptions being thrown from your app).

Upvotes: 0

kupsef
kupsef

Reputation: 3367

new Intent(null, sn_search.class)

Why null? You should specify the context.

Look at your exception, it tells you where the problem is:

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

The Intent's constructor raised a NullPointerException. Guess why:)

Upvotes: 1

Gabor Novak
Gabor Novak

Reputation: 286

Just add this to the intent before you start the Activity: intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Upvotes: 0

Related Questions