我正在尝试在TabListener中加载谷歌地图和图库。但是当我第二次点击地图标签时,我收到以下错误
04-19 18:35:29.821: E/AndroidRuntime(26783): FATAL EXCEPTION: main
04-19 18:35:29.821: E/AndroidRuntime(26783): Process: hibrise.hispark2014.pixnfix, PID: 26783
04-19 18:35:29.821: E/AndroidRuntime(26783): android.view.InflateException: Binary XML file line #7: Error inflating class fragment
04-19 18:35:29.821: E/AndroidRuntime(26783): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
04-19 18:35:29.821: E/AndroidRuntime(26783): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
04-19 18:35:29.821: E/AndroidRuntime(26783): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
04-19 18:35:29.821: E/AndroidRuntime(26783): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
04-19 18:35:29.821: E/AndroidRuntime(26783): at hibrise.hispark2014.pixnfix.MapActivity.onCreateView(MapActivity.java:29)
04-19 18:35:29.821: E/AndroidRuntime(26783): at android.app.Fragment.performCreateView(Fragment.java:1700)
04-19 18:35:29.821: E/AndroidRuntime(26783): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
04-19 18:35:29.821: E/AndroidRuntime(26783): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
04-19 18:35:29.821: E/AndroidRuntime(26783): at android.app.BackStackRecord.run(BackStackRecord.java:684)
04-19 18:35:29.821: E/AndroidRuntime(26783): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
04-19 18:35:29.821: E/AndroidRuntime(26783): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
04-19 18:35:29.821: E/AndroidRuntime(26783): at android.os.Handler.handleCallback(Handler.java:733)
04-19 18:35:29.821: E/AndroidRuntime(26783): at android.os.Handler.dispatchMessage(Handler.java:95)
04-19 18:35:29.821: E/AndroidRuntime(26783): at android.os.Looper.loop(Looper.java:136)
04-19 18:35:29.821: E/AndroidRuntime(26783): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-19 18:35:29.821: E/AndroidRuntime(26783): at java.lang.reflect.Method.invokeNative(Native Method)
04-19 18:35:29.821: E/AndroidRuntime(26783): at java.lang.reflect.Method.invoke(Method.java:515)
04-19 18:35:29.821: E/AndroidRuntime(26783): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-19 18:35:29.821: E/AndroidRuntime(26783): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-19 18:35:29.821: E/AndroidRuntime(26783): at dalvik.system.NativeStart.main(Native Method)
04-19 18:35:29.821: E/AndroidRuntime(26783): Caused by: java.lang.IllegalArgumentException: Binary XML file line #7: Duplicate id 0x7f050009, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.MapFragment
04-19 18:35:29.821: E/AndroidRuntime(26783): at android.app.Activity.onCreateView(Activity.java:4791)
04-19 18:35:29.821: E/AndroidRuntime(26783): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
04-19 18:35:29.821: E/AndroidRuntime(26783): ... 19 more
我的编码是这样的
HomeActivity.Java
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.app.ActionBar.Tab;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.app.Fragment;
import android.widget.TabHost;
public class HomeActivity extends FragmentActivity
{
private static final String MAP_VIEW = "MAP VIEW";
private static final String GALLERY_VIEW = "GALLERY";
TabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_activity);
final ActionBar tabBar = getActionBar();
tabBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
MapActivity map = new MapActivity();
GaleryActivity gallery = new GaleryActivity();
tabBar.removeAllTabs();
tabBar.addTab(tabBar.newTab().setText(MAP_VIEW).setTabListener(new TabListener(this,map)));
tabBar.addTab(tabBar.newTab().setText(GALLERY_VIEW).setTabListener(new TabListener(this,gallery)));
}
public static class TabListener implements ActionBar.TabListener {
private final Fragment mFragment;
private final FragmentActivity mActivity;
public TabListener(FragmentActivity activity, Fragment fragment) {
mActivity = activity;
mFragment = fragment;
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if (null != mFragment) {
mActivity.getFragmentManager().beginTransaction()
.replace(R.id.fragment_container, mFragment).commit();
}
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (null != mFragment)
mActivity.getFragmentManager().beginTransaction().remove(mFragment).commit();
}
}
}
MapActivity.java
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
public class MapActivity extends Fragment
{
private GoogleMap googleMap;
View rootView;
Context context;
double latitude = 21.0000;
double longitude = 78.0000;
public MapActivity(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.map_activity, container, false);
context = rootView.getContext();
try {
// Loading map
initilizeMap();
} catch (Exception e) {
e.printStackTrace();
}
// create marker
MarkerOptions marker = new MarkerOptions().position(new LatLng(latitude, longitude)).title("Hello Maps ");
// adding marker
googleMap.addMarker(marker);
return rootView;
}
/**
* function to load map. If map is not created it will create it for you
* */
private void initilizeMap()
{
if (googleMap == null)
{
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(context,
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
@Override
public void onResume() {
super.onResume();
initilizeMap();
}
}
GalleryActivity.java
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class GaleryActivity extends Fragment
{
public GaleryActivity(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.galery_activity, container, false);
return rootView;
}
}
我第二次点击地图时收到错误。
像
应用程序打开 - > gallery - >地图然后应用程序正在崩溃。
我在这里坚持帮助我。
提前致谢。