通过单击菜单中的应用程序图标,应用程序在恢复时崩溃

时间:2017-03-31 19:46:50

标签: java android android-manifest

应用程序正常启动。当我尝试最小化它并重新打开最近的应用程序历史记录时,它再次正常工作。但是当我最小化并尝试使用应用程序图标重新打开它时,它会崩溃。

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>

错误

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)

2 个答案:

答案 0 :(得分:0)

堆栈跟踪清楚地表明对MainActivity第44行的intent.getData()的调用返回null。您应该检查null,如果它为null,则不要尝试询问Intent。

答案 1 :(得分:0)

您可以将字符串数据存储在静态变量中,然后检查它是否为空。如果它为null则使用静态数据。我知道静态变量是不受欢迎的,你应该解决根问题,但这将解决你当前的问题。

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