我第一次運行onClickMap
功能(點擊按鈕),它的工作原理,並出現mapFragment
,然後我點擊spotList
按鈕,它也能工作和SpotListFragment
出現,然後我再次運行onClickMap
和應用程序崩潰!安卓:有問題fragmentManager(錯誤XML inflateExeption)
屏幕圖像:
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
顯示您的完整MainActivity類...所以減少到骨頭是無用的。 –