简体   繁体   中英

FragmentTransaction.replace() crashes - but only in release build

when I execute the following code in debug (ADB via USB) it works, but when I install a generated release .apk, it crashes. More precisely the following code crashes.

.replace(R.id.settings, new SettingsFragment())

Here is the full version of the code.

public class SettingsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings_activity);
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.settings, new SettingsFragment())
                .commit();
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    static class SettingsFragment extends PreferenceFragmentCompat {
        @Override
        public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
            setPreferencesFromResource(R.xml.root_preferences, rootKey);
        }
    }
}

I found the error from the Google Play which says it throws an IllegalStateExcetion . I do not know any other way of retrieving error messages since it works perfectly fine in debug mode.

This is the corresponding XML file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/settings"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

I believe disabling Instant run may resolve this. Take a look at this thread:

Android App Crashes on Real Device If apk is manually installed

不得不将SettingsFragment公开,现在看起来可以工作了。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM