【问题标题】:Android Settings FragmentAndroid 设置片段
【发布时间】:2014-01-11 23:52:34
【问题描述】:

我正在尝试将设置片段添加到我的 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);
    }
}
}

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

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

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

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)

【问题讨论】:

    标签: java android android-activity settings preferences


    【解决方案1】:

    您正在替换以前从未添加过的片段……

    getFragmentManager().beginTransaction()
                .replace(android.R.id.content, new SettingsFragment())
                .commit();
    

    试试

                .add(android.R.id.content, new SettingsFragment())
    

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

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

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

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

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

    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    Fragment newFragment = fm.findFragmentByTag("Some_Tag");
    if (newFragment == null) {
         newFragment = SomeFragment.newInstance(); //create a new frag
    }
    // Find the old one to know if we have to replace or simply add to this container
    Fragment oldFragment = fm.findFragmentById(R.id.content_container);
    if (oldFragment != null) {
        ft.replace(R.id.content_container, newFragment, "Some_Tag");
    } else {
        ft.add(R.id.content_container, newFragment, "Some_Tag");
    }
    // optionally use a nice transition
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    // …and to the backstack if you wish…
    ft.addToBackStack(null).commit();
    

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

    FragmentManager fm = getSupportFragmentManager();//if using support lib
    Fragment fragment = fm.findFragmentById(R.id.your_container);
    if (fragment == null) {
       fragment = YourFragment.newInstance();
       fm.beginTransaction()
       .add(R.id.your_container, fragment, "some_tag_if_you_wish_to_use_find_by_tag_later")
       .commit();
    }
    

    这只会在容器没有片段时添加片段。否则没有什么可以做的,因为容器已经有了你的片段。 :)

    【讨论】:

    • 我用 add 函数替换了 replace 函数,但它也不起作用。我需要找到旧片段并用新片段替换它,还是我做错了什么?
    【解决方案2】:

    错误消息表明活动层次结构存在问题。尝试使 MainActivity 成为 SettingsActivity 的父级。除了我提到的父关系之外,我使用的是相同的代码;它对我有用。

    你可以这样设置父级关系:

    <activity
                android:name="some.package.SettingsActivity"
                android:label="@string/activity_name_settings"
                android:parentActivityName="some.package.MainActivity" >
                <meta-data
                    android:name="android.support.PARENT_ACTIVITY"
                    android:value="some.package.MainActivity" />
    </activity>
    

    【讨论】:

      猜你喜欢
      • 2018-09-04
      • 2021-05-24
      • 2016-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多