gpt4 book ai didi

java - 调用 fragment 时 fragment 选项菜单出现问题?

转载 作者:行者123 更新时间:2023-12-01 10:50:22 27 4
gpt4 key购买 nike

主要 Activity

public class MainActivity extends AppCompatActivity {

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, new LayOutOne()).commit();
}

}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}

public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.settings:
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, new PrefFragment())
.addToBackStack(null)
.commit();
break;
}
return super.onOptionsItemSelected(item);
}

PrefFragmentList 扩展 PreferenceFragment

public class PrefFragmentList extends PreferenceFragment {

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);

}

PrefFragment 扩展 Fragment

public class PrefFragment extends Fragment {
private View v;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
v = inflater.inflate(R.layout.layout_settings, null);
setHasOptionsMenu(true);
return v;
}
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
menu.findItem(R.id.settings).setVisible(true).setEnabled(false).setChecked(true).setChecked(true);
super.onCreateOptionsMenu(menu, inflater);
}

问题:

MainActivity 从 OptionMenu 调用 PrefFragment ,当我返回 MainActivity 并重新调用 PrefFragment 时,应用程序崩溃。

LOGCAT:

 11-27 12:12:50.857 1387-1387/xx.xxx.myapplication D/dalvikvm: GC_FOR_ALLOC freed 228K, 3% free 9211K/9476K, paused 25ms, total 25ms
11-27 12:13:02.584 1387-1387/xx.xxxx.myapplication D/AndroidRuntime: Shutting down VM
11-27 12:13:02.584 1387-1387/xx.xxxx.myapplication W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x4178b700)
11-27 12:13:02.592 1387-1387/xx.xxxx.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
android.view.InflateException: Binary XML file line #6: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at xx.xxx.myapplication.PrefActivity.onCreateView(PrefActivity.java:23)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: Binary XML file line #6: Duplicate id 0x7f0e0052, tag null, or parent id 0xffffffff with another fragment for xx.xxxxx.myapplication.PrefFragment
at android.app.Activity.onCreateView(Activity.java:4751)
at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:34)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:79)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
at xxx.xxxx.myapplication.PrefFragment.onCreateView(PrefFragment.java:23) 
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962) 
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) 
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248) 
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738) 
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613) 
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517) 
at android.os.Handler.handleCallback(Handler.java:730) 
at android.os.Handler.dispatchMessage(Handler.java:92) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:5103) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:525) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
at dalvik.system.NativeStart.main(Native Method) 

最佳答案

我尝试在OnCreateView中替换:

final View rootView = inflater.inflate(R.layout.fragment_profile, container, false);

与:

if (rootView != null) {
ViewGroup parent = (ViewGroup) rootView.getParent();
if (parent != null)
parent.removeView(rootView);
}
try {
rootView = inflater.inflate(R.layout.layout_settings, container, false);
} catch (InflateException e) {
}
return rootView;

工作正常并解决此错误:

LOGCAT

From the logcat: Caused by: java.lang.IllegalArgumentException: Binary XML file line #6: Duplicate id 0x7f0e0052, tag null, or parent id 0xffffffff with another fragment for xx.xxxxx.myapplication.PrefFragment

关于java - 调用 fragment 时 fragment 选项菜单出现问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33955944/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com