-1

App launches normally. When I try to minimize it and reopen through the recent apps history, again it works fine. But when I minimize and try to re open it with the app icon, it crashes.

MainActivity.java

public class MainActivity extends AppCompatActivity implements ResetPasswordDialog.Listener {

public static final String TAG = MainActivity.class.getSimpleName();

private LoginFragment mLoginFragment;
private ResetPasswordDialog mResetPasswordDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {

        loadFragment();
    }
}

private void loadFragment(){

    if (mLoginFragment == null) {

        mLoginFragment = new LoginFragment();
    }
    getFragmentManager().beginTransaction().replace(R.id.fragmentFrame,mLoginFragment,LoginFragment.TAG).commit();
}

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);

    String data = intent.getData().getLastPathSegment();
    Log.d(TAG, "onNewIntent: "+data);

    mResetPasswordDialog = (ResetPasswordDialog) getFragmentManager().findFragmentByTag(ResetPasswordDialog.TAG);

    if (mResetPasswordDialog != null)
        mResetPasswordDialog.setToken(data);
}

@Override
public void onPasswordReset(String message) {

    showSnackBarMessage(message);
}

private void showSnackBarMessage(String message) {

    Snackbar.make(findViewById(R.id.activity_main),message, Snackbar.LENGTH_SHORT).show();

}

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-feature
    android:name="android.hardware.camera"
    android:required="true" />
<uses-feature
    android:name="android.hardware.camera.front"
    android:required="false" />
<uses-feature
    android:name="android.hardware.camera.autofocus"
    android:required="false" />

<!--singleTask => flag of launchMode -->
<application
    android:name="io.github.froger.xinger.InstaMaterialApplication"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">

    <activity
        android:name="io.github.froger.xinger.ui.activity.MainActivity"
        android:launchMode="singleTop"
        android:theme="@style/AppTheme.LoginRegister"
        android:screenOrientation="portrait">

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

            <category android:name="android.intent.category.LAUNCHER" />

        </intent-filter>

        <intent-filter>
            <action android:name="android.intent.action.VIEW" />

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />

            <data
                android:host="learn2crack"
                android:scheme="http" />
        </intent-filter>
    </activity>

    <activity
        android:name="io.github.froger.xinger.ui.activity.DashboardActivity"
        android:screenOrientation="portrait">
    </activity>
    <activity
        android:name="io.github.froger.xinger.ui.activity.CommentsActivity"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.TransparentActivity" />
    <activity
        android:name="io.github.froger.xinger.ui.activity.UserProfileActivity"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.TransparentActivity"
        android:launchMode="singleInstance" />
    <activity
        android:name="io.github.froger.xinger.ui.activity.TakePhotoActivity"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.TransparentActivity" />
    <activity
        android:name="io.github.froger.xinger.ui.activity.PublishActivity"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme"
        android:windowSoftInputMode="stateHidden">

    </activity>
</application>

Error

04-01 00:51:26.015 25525-25525/io.github.froger.instamaterial D/AndroidRuntime: Shutting down VM
04-01 00:51:26.016 25525-25525/io.github.froger.instamaterial E/AndroidRuntime: FATAL EXCEPTION: main
                                                                            Process: io.github.froger.instamaterial, PID: 25525
                                                                            java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getLastPathSegment()' on a null object reference
                                                                                at io.github.froger.xinger.ui.activity.MainActivity.onNewIntent(MainActivity.java:44)
                                                                                at android.app.Instrumentation.callActivityOnNewIntent(Instrumentation.java:1245)
                                                                                at android.app.Instrumentation.callActivityOnNewIntent(Instrumentation.java:1257)
                                                                                at android.app.ActivityThread.deliverNewIntents(ActivityThread.java:2804)
                                                                                at android.app.ActivityThread.performNewIntents(ActivityThread.java:2816)
                                                                                at android.app.ActivityThread.handleNewIntent(ActivityThread.java:2825)
                                                                                at android.app.ActivityThread.-wrap15(ActivityThread.java)
                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1552)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                at android.os.Looper.loop(Looper.java:154)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:6123)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Rohit Arya Mar 31 '17 at 19:48
  • A breakpoint and stepping through the code will help you a lot. Or the intent is null (with is not likely because it was passed to the super class) or the data it returns is null. – bichito Mar 31 '17 at 20:37

2 Answers2

0

The stack trace clearly indicates that the call to intent.getData() at line 44 of your MainActivity is returning a null. You should check for the null, and not try to interrogate the Intent if it is null.

GreyBeardedGeek
  • 29,460
  • 2
  • 47
  • 67
0

You could store string data in a static variable, and then check to see if it's null. If it's null use the static data. I know static variables are frowned upon and you should address the root problem, but this will fix your immediate problem.

static String yourStaticString;

...
String data;
if (intent.getData()==NULL) 
{
      data = yourStaticString;
}
else
{
     if (intent.getData().getLastPathSegment()!=NULL)
     {
             data = intent.getData().getLastPathSegment();
             yourStaticString = data;
     } else return;
}
apollosoftware.org
  • 12,161
  • 4
  • 48
  • 69