Android设置片段

时间:2013-12-23 18:18:38

标签: java android android-activity settings preferences

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

2 个答案:

答案 0 :(得分:4)

您正在替换之前从未添加的片段...

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

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

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

  1. 按ID或标记查找片段
  2. 如果找到的片段为空,则创建一个。
  3. 您可以在网络和堆栈溢出周围找到大量示例。 ;)

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

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

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

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

答案 1 :(得分:1)

错误消息表明活动层次结构存在一些问题。尝试将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>