-1

I am currently breaking my head off, because I can start a normal startActivity(). It is always giving me this error:

Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference

I am starting it from my Application Welcome Activity:

application.getUserFromDatabase(getApplicationContext(), username, password);

Application:

public void getUserFromDatabase(Context context, String username, String password) {
   //new GetFromDatabase().execute("getUser.php", "?username=" + username + "&password=" + password, this, context);
    WelcomeActivity activity = new WelcomeActivity();
    activity.startMainActivity();
}

AndroidManifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.mydomain.app">

    <application
        android:name=".MainApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">

        </activity>
        <activity android:name=".WelcomeActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Thank you in advance :)

EDIT: I forgot adding my Method startMainActivity here:

public void startMainActivity(){
    Intent startMain = new Intent(getApplicationContext(), MainActivity.class)
    startActivity(startMain);
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Samuel M.
  • 5
  • 3

2 Answers2

0

you must use intent for startActivity();

example:

 Intent intent = new Intent(this, WelcomeActivity.class);
 startActivtiy(intent);
0

It definitely causing you a null pointer exception because your WelcomeActivity is not created yet. You can't start an activity by creating a new instance of Activity with:

WelcomeActivity activity = new WelcomeActivity();

Your activity need to be start with:

Intent welcome = new Intent(context, WelcomeActivity.class);
context.startActivtiy(welcome);

or by adding the correct intent-filter so the activity will be launched by the Android system.

You can start your activity from Application with something like this:

// FLAG_ACTIVITY_NEW_TASK is required
// FLAG_ACTIVITY_CLEAR_TOP brings back to the top of the stack
Intent mainActivity = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(mainActivity);

Though I think you should not start an Activity via Application. You need to think throughly about your design so there is no activity connection with the application.

ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96