Android: Call Method from another Class which starts a new Intent

13,122
((Login_Activity())mContext).LoginSuccess();

where mContext is your Login_Activity reference

you need pass activity reference

Share:
13,122
3dDi92
Author by

3dDi92

Updated on June 05, 2022

Comments

  • 3dDi92
    3dDi92 almost 2 years

    as the title already said, I’m trying to call a method LoginSuccess() from the Class JSON_Factroy_Return_Handler.java which then should start an Intent. I always get a java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference.

    To make it more clear what I want to do, I’m going to post the important code snippet.

    Login_Activity.java

    public void Login(View view) { //Button Login pressed calls Login-Method
        ...
        new JSON_Factory_Test().execute(result); //Calls AsyncTask-Class
        ...
    }
    
    
    public void LoginSuccess() {
        Intent mainIntent = new Intent(getApplicationContext(), Main_Activity.class);       
        startActivity(mainIntent);
        finish();
    }
    

    JSON_Factory_Test.java

    public class JSON_Factory_Test extends AsyncTask<String, String, JSONObject> {
    
        @Override
        protected JSONObject doInBackground(String... args) {
            ...
            return jsonobject;
        }
    
        @Override
        protected void onPostExecute(JSONObject jsonobject) {
            new JSON_Factory_Return_Handler().JSON_Factory_Retrun(jsonobject);
        }
    
    }
    

    JSON_Factroy_Return_Handler.java

    public class JSON_Factory_Return_Handler {
        ...
        public void JSON_Factory_Retrun(JSONObject jsonresult) {
            ...
            if(jsonParams.get("result").toString().equals("TRUE")) {
                new Login_Activity().LoginSuccess();
            } else {
                ...     
            }
            ...
        }
    
    }
    

    When I then run this Project I alway get a NullPointerException!

    FATAL EXCEPTION: main
    Process: com.example.project, PID: 2855
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
        at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:105)
        at com.example.project.main.Login_Activity.LoginSuccess(Login_Activity.java:138)
        at com.example.project.json.JSON_Factory_Return_Handler.JSON_Factory_Retrun(JSON_Factory_Return_Handler.java:27)
        at com.example.project.json.JSON_Factory_Test.onPostExecute(JSON_Factory_Test.java:49)
        at com.example.project.json.JSON_Factory_Test.onPostExecute(JSON_Factory_Test.java:1)
        at android.os.AsyncTask.finish(AsyncTask.java:632)
        at android.os.AsyncTask.access$600(AsyncTask.java:177)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5070)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:836)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:631)
    

    I also tried this for the Login_Activity.java but this also won't work either

    public void LoginSuccess() {
        Intent mainIntent = new Intent(Login_Activity.this, Main_Activity.class);       
        Login_Activity.this.startActivity(mainIntent);
        finish();
    }
    

    When I then run this Project I also get a NullPointerException!

        FATAL EXCEPTION: main
    Process: com.example.project, PID: 3187
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
        at android.content.ContextWrapper.getPackageName(ContextWrapper.java:131)
        at android.content.ComponentName.<init>(ComponentName.java:77)
        at android.content.Intent.<init>(Intent.java:4011)
        at com.example.project.main.Login_Activity.LoginSuccess(Login_Activity.java:138)
        at com.example.project.json.JSON_Factory_Return_Handler.JSON_Factory_Retrun(JSON_Factory_Return_Handler.java:27)
        at com.example.project.json.JSON_Factory_Test.onPostExecute(JSON_Factory_Test.java:49)
        at com.example.project.json.JSON_Factory_Test.onPostExecute(JSON_Factory_Test.java:1)
        at android.os.AsyncTask.finish(AsyncTask.java:632)
        at android.os.AsyncTask.access$600(AsyncTask.java:177)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5070)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:836)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:631)
    

    I know that I could do everything in one Class but I need the AsyncTask more than once. And I also need the JSON_Factory_Return_Handler.java because this Class checks the returned values and then it should call new methods.

    If you guys need more information I would be happy if I could give them to you :)

    Thanks for your help in advance, Cheers 3dDi

  • 3dDi92
    3dDi92 over 9 years
    Thank you so much :) I just need to pass the reference and I didn't saw that...new Login_Activity().LoginSuccess(mContext);