gpt4 book ai didi

java - Android:fragmentManager 有问题(错误 XML inflateExeption)

转载 作者:行者123 更新时间:2023-11-30 11:23:25 25 4
gpt4 key购买 nike

我第一次运行 onClickMap 函数(点击按钮)时它起作用了并且 mapFragment 出现了,然后我点击 spotList 按钮它也起作用了SpotListFragment 出现,然后我再次运行 onClickMap,应用程序崩溃了!

屏幕图像:
screenshot

MainActivity.class

package com.pap.myspots;

import com.pap.myspots.adapter.TabsPagerAdapter;
import com.pap.myspots.fragments.MapFragment;
import com.pap.myspots.fragments.NotificationsFragment;
import com.pap.myspots.fragments.SpotListFragment;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {

private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
String local;
// Tab titles

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

// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

// Adding Tabs

ImageView mapTab = new ImageView(this);
mapTab.setBackgroundColor(Color.parseColor("#ffbb33"));
//mapTab.setImageResource(R.drawable.notificationsicon);
actionBar.addTab(actionBar.newTab().setIcon(R.drawable.nicon)
.setTabListener(this));
actionBar.addTab(actionBar.newTab().setIcon(R.drawable.micon)
.setTabListener(this));
actionBar.addTab(actionBar.newTab().setIcon(R.drawable.sicon)
.setTabListener(this));

/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

@Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}

@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}

public void clickMap(View v){
MapFragment mapfragment = new MapFragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.frame, mapfragment).commit();
}

public void clickList(View v){
SpotListFragment listfragment = new SpotListFragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.frame, listfragment).commit();
}



@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}

}

fragment_home.xml:

<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal"
android:layout_alignParentBottom="true">

<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ef4748"
android:text="Map"
android:textColor="#ffffff"
android:textStyle="bold"
android:onClick="clickMap"/>

<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="List"
android:textColor="#ffffff"
android:textStyle="bold"
android:background="#c43c3c"
android:onClick="clickList"/>

</LinearLayout>

错误:

E/AndroidRuntime(2971): FATAL EXCEPTION: main
E/AndroidRuntime(2971): android.view.InflateException: Binary XML file line #46: Error inflating class fragment
E/AndroidRuntime(2971): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
E/AndroidRuntime(2971): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
E/AndroidRuntime(2971): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
E/AndroidRuntime(2971): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
E/AndroidRuntime(2971): at com.pap.myspots.fragments.MapFragment.onCreateView(MapFragment.java:29)
E/AndroidRuntime(2971): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
E/AndroidRuntime(2971): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
E/AndroidRuntime(2971): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
E/AndroidRuntime(2971): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
E/AndroidRuntime(2971): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
E/AndroidRuntime(2971): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
E/AndroidRuntime(2971): at android.os.Handler.handleCallback(Handler.java:730)
E/AndroidRuntime(2971): at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(2971): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(2971): at android.app.ActivityThread.main(ActivityThread.java:5103)
E/AndroidRuntime(2971): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(2971): at java.lang.reflect.Method.invoke(Method.java:525)
E/AndroidRuntime(2971): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
E/AndroidRuntime(2971): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
E/AndroidRuntime(2971): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(2971): Caused by: java.lang.IllegalArgumentException: Binary XML file line #46: Duplicate id 0x7f05000c, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
E/AndroidRuntime(2971): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:296)
E/AndroidRuntime(2971): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
E/AndroidRuntime(2971): ... 19 more

最佳答案

将其添加到您的 mapFragment 中。

@Override
public void onDestroyView() {
super.onDestroyView();
try {
SupportMapFragment fragment = (SupportMapFragment) getActivity()
.getSupportFragmentManager().findFragmentById(R.id.map);
if (fragment != null)
getFragmentManager().beginTransaction().remove(fragment)
.commit();

} catch (IllegalStateException e) {
}
}

您需要正确销毁 map fragment !

关于java - Android:fragmentManager 有问题(错误 XML inflateExeption),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21230555/

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