Android 当我按下设置菜单中的向上(后退)按钮时,应用程序崩溃

Android 当我按下设置菜单中的向上(后退)按钮时,应用程序崩溃,android,android-studio,Android,Android Studio,所以我对Android编程还是相当陌生,一直在开发一款应用程序。我刚刚创建了一个设置菜单,其中保存了所有设置,我遇到了一个问题,当按下操作栏上的后退按钮时,应用程序崩溃。如果我按下手机上的“后退”按钮,一切正常,但只有在按下“设置”操作栏上的“后退”按钮时才会崩溃 我查看了MainActivity.java文件,注意到当我删除onDestroy()函数时,这种情况就不会发生了,但是按照我的应用程序的工作方式,我需要在onDestroy函数中包含这段代码,以便我的应用程序在每次关闭时都能完全关闭。

所以我对Android编程还是相当陌生,一直在开发一款应用程序。我刚刚创建了一个设置菜单,其中保存了所有设置,我遇到了一个问题,当按下操作栏上的后退按钮时,应用程序崩溃。如果我按下手机上的“后退”按钮,一切正常,但只有在按下“设置”操作栏上的“后退”按钮时才会崩溃

我查看了MainActivity.java文件,注意到当我删除onDestroy()函数时,这种情况就不会发生了,但是按照我的应用程序的工作方式,我需要在onDestroy函数中包含这段代码,以便我的应用程序在每次关闭时都能完全关闭。以下是我的故事功能:

//Kill app completely when app is closed
@Override
protected void onDestroy() {

    super.onDestroy();

        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        startActivity(intent);
        int pid = android.os.Process.myPid();
        android.os.Process.killProcess(pid);

}
我尝试过将内容改为finish()和其他各种修复,但都没有效果。似乎主页上没有任何东西在保存

My SettingsActivity.java文件:

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

    public static class SettingsFragment extends PreferenceFragmentCompat {

        @Override
        public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
            setPreferencesFromResource(R.xml.root_preferences, rootKey);
        }
    }
}
这是我的日志,它对正在发生的事情没有多大意义

2021-01-07 15:22:46.118 1881-1894/? E/memtrack: Couldn't load memtrack module
2021-01-07 15:22:46.190 1881-1966/? E/InputDispatcher: channel 'eefb3fd com.example.obd/com.example.obd.SettingsActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
2021-01-07 15:22:46.190 1881-1966/? E/InputDispatcher: channel '466b0d7 com.example.obd/com.example.obd.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
2021-01-07 15:22:46.221 2553-4044/? E/s.nexuslaunche: No package ID ff found for ID 0xffffffff.
2021-01-07 15:22:46.221 2553-4044/? E/IconLoader: Could not find icon drawable from resource
    android.content.res.Resources$NotFoundException: Resource ID #0xffffffff
        at android.content.res.ResourcesImpl.getValueForDensity(ResourcesImpl.java:225)
        at android.content.res.Resources.getDrawableForDensity(Resources.java:887)
        at android.content.res.Resources.getDrawable(Resources.java:827)
        at com.android.systemui.shared.recents.model.IconLoader.createNewIconForTask(SourceFile:118)
        at com.android.systemui.shared.recents.model.IconLoader.getAndInvalidateIfModified(SourceFile:94)
        at com.android.systemui.shared.recents.model.RecentsTaskLoader.getAndUpdateActivityIcon(SourceFile:325)
        at com.android.systemui.shared.recents.model.RecentsTaskLoadPlan.executePlan(SourceFile:188)
        at com.android.systemui.shared.recents.model.RecentsTaskLoader.loadTasks(SourceFile:173)
        at com.android.quickstep.RecentsModel.onTaskStackChangedBackground(SourceFile:214)
        at com.android.systemui.shared.system.TaskStackChangeListeners.onTaskStackChanged(SourceFile:80)
        at android.app.ITaskStackListener$Stub.onTransact(ITaskStackListener.java:50)
        at android.os.Binder.execTransact(Binder.java:731)
2021-01-07 15:22:46.309 2300-2384/? E/ModuleIdSetter: exception when setting module id
    java.lang.IllegalStateException: Unable to get current module info in ModuleManager created with non-module Context
        at com.google.android.chimera.config.ModuleManager.getCurrentModule(:com.google.android.gms@204516031@20.45.16 (100700-344294571):2)
        at afxj.a(:com.google.android.gms@204516031@20.45.16 (100700-344294571):4)
        at afxm.Y(:com.google.android.gms@204516031@20.45.16 (100700-344294571):1)
        at aftx.a(Unknown Source:0)
        at scv.a(:com.google.android.gms@204516031@20.45.16 (100700-344294571):0)
        at ryz.f(:com.google.android.gms@204516031@20.45.16 (100700-344294571):1)
        at ryx.e(:com.google.android.gms@204516031@20.45.16 (100700-344294571):1)
        at sbr.p(:com.google.android.gms@204516031@20.45.16 (100700-344294571):2)
        at sbr.v(:com.google.android.gms@204516031@20.45.16 (100700-344294571):3)
        at sbr.e(:com.google.android.gms@204516031@20.45.16 (100700-344294571):2)
        at sbv.handleMessage(:com.google.android.gms@204516031@20.45.16 (100700-344294571):71)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at afms.iD(:com.google.android.gms@204516031@20.45.16 (100700-344294571):0)
        at afms.dispatchMessage(:com.google.android.gms@204516031@20.45.16 (100700-344294571):11)
        at android.os.Looper.loop(Looper.java:193)
        at android.os.HandlerThread.run(HandlerThread.java:65)
非常感谢你的帮助

删除:

ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
    actionBar.setDisplayHomeAsUpEnabled(true);
}
在oncreate方法之外使用以下代码

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
        super.onBackPressed();
        return true;
    }
    return super.onOptionsItemSelected(item);
}
并将下面的行添加到清单中的活动中

希望这能起作用。

删除:

ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
    actionBar.setDisplayHomeAsUpEnabled(true);
}
在oncreate方法之外使用以下代码

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
        super.onBackPressed();
        return true;
    }
    return super.onOptionsItemSelected(item);
}
并将下面的行添加到清单中的活动中


希望这能奏效。

是的,成功了!我不需要将parentActivity添加到清单文件中,因为我已经将“.MainActivity”添加为清单文件中的父活动。此外,我还必须将您的代码添加到我的设置活动中,它工作得非常好!调用这个.finish()或super.onBackPressed()(如您所示)也可以实现onOptionItemsSelected方法中的技巧。是的,这很有效!我不需要将parentActivity添加到清单文件中,因为我已经将“.MainActivity”添加为清单文件中的父活动。此外,我还必须将您的代码添加到我的设置活动中,它工作得非常好!如您所示调用this.finish()或super.onBackPressed()也可以实现OnOption方法中的技巧。