首页 > 解决方案 > 无论用于启动它的方法如何,Android 活动都不会启动

问题描述

我已经定义了一个 android 活动,无论我如何尝试启动它,它都无法启动。它应该作为主要活动启动,但如果我将其声明为默认活动,应用程序就会挂起,如下所示:

    <activity
        android:name=".activity.StartupActivity"
        android:screenOrientation="portrait"
        android:theme="@style/NoActionAppTheme">
        <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="url"
                android:pathPrefix="/prefix"
                android:scheme="https" />
            <data
                android:host="url"
                android:scheme="https" />
        </intent-filter>
    </activity>

我尝试在此活动的第一行onCreateLog行中放置一个断点,但此活动无法启动。我认为活动的源代码是无关紧要的,因为它永远不会开始。如果需要,请告诉我。我尝试将另一个活动设置为默认活动并StartupActivity从它开始,如下所示:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (! AppRuntime.b) {
        AppRuntime.b = true;
        startActivity(new Intent(this, StartupActivity.class));
        finish();
        return;
    }
// other stuff
}

但结果与我StartupActivity在默认活动时看到的相似。此活动中的断点按预期工作。启动时出现问题StartupActivity。是什么导致了这个问题?

注意:仅StartupActivity扩展AppCompatActivity和覆盖onCreateonActivityResult

编辑:这是活动代码:

public class StartupActivity extends AppCompatActivity {

    SharedPreferences sp;
    String deepLink = "";

    final int SIGNUP_REQUEST_CODE = 0;
    final int TUTORIAL_REQUEST_CODE = 1;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState);
        addressInvitation();

        sp = App.getSharedPreferences();

        MobileAds.initialize(this, Utils.getAdmobID());

        setContentView(R.layout.activity_splash_screen);
        //load the ad
//        mAdView = findViewById(R.id.adView);
//        AdRequest adRequest = new AdRequest.Builder().build();
//        mAdView.loadAd(adRequest);

        Log.d("DEBUGGING", "calling bootstrap");

        bootstrapApp();
    }


    private void bootstrapApp() {
        if (! sp.contains("signed_in")) {
            sp.edit().clear().apply();
            Log.d("DEBUGGING", "starting signup activity");
            startActivityForResult(new Intent(this, SignUp.class), SIGNUP_REQUEST_CODE);
        } else if (! sp.contains("isFirstTime")) {
            Log.d("DEBUGGING", "starting tutorial");
            startActivityForResult(new Intent(this, TutorialsActivity.class), TUTORIAL_REQUEST_CODE);
        } else {
            Log.d("DEBUGGING", "going to splash screen");
            startActivity(new Intent(this, SplashScreen.class));
            finish();
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case SIGNUP_REQUEST_CODE:
                Log.d("DEBUGGING", "signup returned");
                if (resultCode == RESULT_OK) {
                    sp.edit()
                            .putBoolean("signed_in", true)
                            .apply();
                } else {
                    finish();
                }
                break;
            case TUTORIAL_REQUEST_CODE:
                Log.d("DEBUGGING", "tutorial returned");
                if (resultCode == RESULT_OK) {
                    sp.edit()
                            .putBoolean("isFirstTime", true)
                            .apply();
                } else {
                    finish();
                }
                break;
        }
        bootstrapApp();
    }



    private void addressInvitation() {
        FirebaseDynamicLinks.getInstance().getDynamicLink(getIntent())
                .addOnSuccessListener(this, new OnSuccessListener<PendingDynamicLinkData>() {
                    @Override
                    public void onSuccess(PendingDynamicLinkData data) {
                        if (data == null) {
                            return;
                        }

                        // Get the deep link
                        deepLink = data.getLink().toString();

                        // Extract invite
                        FirebaseAppInvite invite = FirebaseAppInvite.getInvitation(data);
                        if (invite != null) {
                            String invitationId = invite.getInvitationId();
                        }
                    }
                })
                .addOnFailureListener(this, new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Log.e("HandleInvitaiton", "COULD NOT HANDLE");
                    }
                });
    }
}

标签: androidandroid-activitystartup

解决方案


我看到了问题。你正在使用

public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState)

代替

protected void onCreate(@Nullable Bundle savedInstanceState)

你覆盖了错误的方法。


推荐阅读