Can't run new activity from onPostExecute

Alex.M.K

I'm having trouble starting my mainActivity after I have gotten a response from a httpUrlConnection. Here is my onPostExecute method

    protected void onPostExecute(String[] result) {
        serv.httphelper.handleResults(result);

    }

In the method handleResult() im handling the response code. If the response code is 200 i want to run a new method inside my loginActivity class

public void handleResults(String[] result) {

    status = result[0].toString();
    instructions = result[1].toString();
    jsonString = result[2];

    Log.d("DEBUG", status);

    if (status.equals("200")) {
        serv.loginActivity.proceed();

    } else if (status.equals("400")) {
        serv.loginActivity.loginError();

    } else if (status.equals("401")) {
        serv.loginActivity.loginError();

    }

}

When i try to start a new activity from the proceed() method i get a nullpointerexception

    public void proceed(){
    startActivity(new Intent (LoginActivity.this, MainActivity.class));
    Log.d("TEST", "Proceed success");


}

My service class for anyone wondering:

public class Service {

public static HttpHelper httphelper = new HttpHelper();
public static HttpConnect conn = new HttpConnect();
public static LoginActivity loginActivity = new LoginActivity();

}

And here is my logCat: LogCat

Thanks in advance!

Yaniv

Your problem is when you start the activity at the line:

startActivity(new Intent (LoginActivity.this, MainActivity.class));

Well, you creates an intent that tries to get to LoginActivity.this which probably does not exists!
Instead of this, you have to give an instance of a real activity that is currently running.

A solution that I can suggest you is to pass to proceed() the instance of the currently running activity and put it instead of LoginActivity.this.
In case you don't have a current activity, try to put your application's context instead and add it a flag of a new task.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Can't able to start new activity from onPostExecute method in Android

From Dev

I can't pass value from onPostExecute to another activity?

From Dev

Android can't update TextView from onPostExecute

From Dev

Android: Can't call new activity from class that extends Thread

From Dev

Link to new activity usin AsyncTask onPostExecute?

From Dev

Send Value From onPostExecute() to Activity is always NULL

From Dev

Start activity from onPostExecute() method and pass the arrayList to it

From Dev

unable to start activity from onPostexecute() using interface

From Dev

Call activity in method onPostExecute from asynctask class

From Dev

Starting activity from onPostExecute(String result) doesn't set ui flags

From Dev

Can't add a new activity on android studio

From Dev

Can't open new Activity in Android Studio

From Dev

Android Studio: Can't switch to new activity

From Dev

Can't pass image created from RelativeLayout to a new activity using intent

From Dev

Can't access Activity methods from AppCompatActivity

From Dev

Can't start fragment from activity

From Dev

Can't remove padding from Activity Dialog

From Dev

Can't go to other activity from fragment?

From Dev

Can't start fragment from activity

From Dev

Can't find Android Activity from ADB

From Dev

Can't update/redraw listView inside of onPostExecute

From Dev

Can't run new firebase in node

From Dev

Jquery function can't run on new elements

From Dev

Asynctask can't pass variable from doInBackground to onPostExecute - changing public var. in postexecute

From Dev

If I call an asyncTask from a thread will the onPostExecute() execute on the thread or the calling activity

From Dev

If I call an asyncTask from a thread will the onPostExecute() execute on the thread or the calling activity

From Dev

AsyncTask onPostExecute() is not called from Fragment class but works in Activity class

From Dev

Android: call main activity method from Async onPostExecute method

From Dev

Display to another activity json array result from onPostExecute

Related Related

  1. 1

    Can't able to start new activity from onPostExecute method in Android

  2. 2

    I can't pass value from onPostExecute to another activity?

  3. 3

    Android can't update TextView from onPostExecute

  4. 4

    Android: Can't call new activity from class that extends Thread

  5. 5

    Link to new activity usin AsyncTask onPostExecute?

  6. 6

    Send Value From onPostExecute() to Activity is always NULL

  7. 7

    Start activity from onPostExecute() method and pass the arrayList to it

  8. 8

    unable to start activity from onPostexecute() using interface

  9. 9

    Call activity in method onPostExecute from asynctask class

  10. 10

    Starting activity from onPostExecute(String result) doesn't set ui flags

  11. 11

    Can't add a new activity on android studio

  12. 12

    Can't open new Activity in Android Studio

  13. 13

    Android Studio: Can't switch to new activity

  14. 14

    Can't pass image created from RelativeLayout to a new activity using intent

  15. 15

    Can't access Activity methods from AppCompatActivity

  16. 16

    Can't start fragment from activity

  17. 17

    Can't remove padding from Activity Dialog

  18. 18

    Can't go to other activity from fragment?

  19. 19

    Can't start fragment from activity

  20. 20

    Can't find Android Activity from ADB

  21. 21

    Can't update/redraw listView inside of onPostExecute

  22. 22

    Can't run new firebase in node

  23. 23

    Jquery function can't run on new elements

  24. 24

    Asynctask can't pass variable from doInBackground to onPostExecute - changing public var. in postexecute

  25. 25

    If I call an asyncTask from a thread will the onPostExecute() execute on the thread or the calling activity

  26. 26

    If I call an asyncTask from a thread will the onPostExecute() execute on the thread or the calling activity

  27. 27

    AsyncTask onPostExecute() is not called from Fragment class but works in Activity class

  28. 28

    Android: call main activity method from Async onPostExecute method

  29. 29

    Display to another activity json array result from onPostExecute

HotTag

Archive