2013-12-23 67 views
4

我试图添加一个设置片段到我的android-app。 所以,我增加了一个XML文件和本次活动:Android设置片段

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 [email protected] 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) 

回答

3

您正在更换一个片段临时如果发现■禁止前加入...

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

尝试

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

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

  1. 查找ID或标签
  2. 片段片段是空,然后创建一个。

你可以找到很多围绕net和stackoverflow的例子。 ;)

UPDATE好的,这里有一些示例代码供您使用作为起点。

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

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

这只会增加的片段,如果容器没有它。否则,因为容器已经有你的片段,所以还没有做任何事情要做。 :)

+0

我用add函数代替了替换功能,但它也不管用。我是否需要找到旧片段并将其替换为新片段,或者我做错了什么? – ovmcit5eoivc4

1

错误消息指示活动层次结构存在一些问题。尝试使MainActivity成为SettingsActivity的父项。除了我提到的父母关系外,我使用的是相同的代码;它适用于我。

您可以设置父relatioship这样的:

<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>