Android设置片段

ovm*_*vc4 5 java settings android preferences android-activity

我正在尝试将设置片段添加到我的android-app中.所以,我添加了一个xml文件和这个活动:

public class SettingsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Display the fragment as the main content.
    getFragmentManager().beginTransaction()
            .replace(android.R.id.content, new SettingsFragment())
            .commit();
}

public static class SettingsFragment extends PreferenceFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.preferences);
    }
}
}
Run Code Online (Sandbox Code Playgroud)

它是从主活动中的onOptionsItemSelected函数调用的:

if (id == R.id.action_settings) {
        Intent intent = new Intent(this, SettingsActivity.class);
        startActivity(intent);
        return true;
    }
Run Code Online (Sandbox Code Playgroud)

所以,现在,当我尝试从菜单中打开活动时,强制关闭并在控制台中收到此错误:

Force-removing child win Window{4190a9a8 u0 PopupWindow:418fc208} from container         Window{418dd448 u0 org.name.app/org.name.app.MainActivity}
Failed looking up window
java.lang.IllegalArgumentException: Requested window android.os.BinderProxy@423b1fc0 does not exist
        at com.android.server.wm.WindowManagerService.windowForClientLocked(WindowManagerService.java:7934)
        at com.android.server.wm.WindowManagerService.windowForClientLocked(WindowManagerService.java:7925)
        at com.android.server.wm.WindowState$DeathRecipient.binderDied(WindowState.java:1047)
        at android.os.BinderProxy.sendDeathNotice(Binder.java:493)
        at dalvik.system.NativeStart.run(Native Method)
Run Code Online (Sandbox Code Playgroud)

Mar*_*ini 4

您正在替换之前从未添加过的片段\xe2\x80\xa6

\n\n
getFragmentManager().beginTransaction()\n            .replace(android.R.id.content, new SettingsFragment())\n            .commit();\n
Run Code Online (Sandbox Code Playgroud)\n\n

尝试

\n\n
            .add(android.R.id.content, new SettingsFragment())\n
Run Code Online (Sandbox Code Playgroud)\n\n

但正确的代码应该是(伪代码)

\n\n
    \n
  1. 通过 id 或 tag 查找片段
  2. \n
  3. 如果找到的片段为空,则创建一个。
  4. \n
\n\n

您可以在网络和 stackoverflow 上找到大量示例。;)

\n\n

更新好的,这里有一些示例代码供您用作起点。

\n\n

假设:您对多个片段使用相同的活动,因此您需要恢复现有的或创建一个新的。

\n\n
FragmentManager fm = getSupportFragmentManager();\nFragmentTransaction ft = fm.beginTransaction();\nFragment newFragment = fm.findFragmentByTag("Some_Tag");\nif (newFragment == null) {\n     newFragment = SomeFragment.newInstance(); //create a new frag\n}\n// Find the old one to know if we have to replace or simply add to this container\nFragment oldFragment = fm.findFragmentById(R.id.content_container);\nif (oldFragment != null) {\n    ft.replace(R.id.content_container, newFragment, "Some_Tag");\n} else {\n    ft.add(R.id.content_container, newFragment, "Some_Tag");\n}\n// optionally use a nice transition\nft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);\n// \xe2\x80\xa6and to the backstack if you wish\xe2\x80\xa6\nft.addToBackStack(null).commit();\n
Run Code Online (Sandbox Code Playgroud)\n\n

另一方面,如果您只想要简单的版本,没有任何花哨的\xe2\x80\xa6

\n\n
FragmentManager fm = getSupportFragmentManager();//if using support lib\nFragment fragment = fm.findFragmentById(R.id.your_container);\nif (fragment == null) {\n   fragment = YourFragment.newInstance();\n   fm.beginTransaction()\n   .add(R.id.your_container, fragment, "some_tag_if_you_wish_to_use_find_by_tag_later")\n   .commit();\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

如果容器没有片段,这只会添加片段。否则什么都不用做,因为容器已经有了你的片段。:)

\n