在我的项目中,我使用三个选项卡在单个屏幕中显示三个活动。
标签1 - 地图
表2 - 游戏
表3 - Imageflipper
当我选择像Map一样的系列标签--->游戏---> Imageflipper然后它工作正常。
但是当我选择回到地图--->游戏---> Imageflipper --->>游戏等任何标签时,它会显示错误 -
二进制XML文件行#10:错误膨胀类片段。
模拟器的屏幕截图:
http://s9.postimg.org/jdjsxnswf/Untitled.png
这是我的logcat。
03-26 05:48:09.876: D/AndroidRuntime(1094): Shutting down VM
03-26 05:48:09.876: W/dalvikvm(1094): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
03-26 05:48:09.936: E/AndroidRuntime(1094): FATAL EXCEPTION: main
03-26 05:48:09.936: E/AndroidRuntime(1094): android.view.InflateException: Binary XML file line #10: Error inflating class fragment
03-26 05:48:09.936: E/AndroidRuntime(1094): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
03-26 05:48:09.936: E/AndroidRuntime(1094): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
03-26 05:48:09.936: E/AndroidRuntime(1094): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
03-26 05:48:09.936: E/AndroidRuntime(1094): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
03-26 05:48:09.936: E/AndroidRuntime(1094): at com.example.image_changer.TopRatedFragment.onCreateView(TopRatedFragment.java:15)
03-26 05:48:09.936: E/AndroidRuntime(1094): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
03-26 05:48:09.936: E/AndroidRuntime(1094): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
03-26 05:48:09.936: E/AndroidRuntime(1094): at android.support.v4.app.FragmentManagerImpl.attachFragment(FragmentManager.java:1264)
03-26 05:48:09.936: E/AndroidRuntime(1094): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:672)
03-26 05:48:09.936: E/AndroidRuntime(1094): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
03-26 05:48:09.936: E/AndroidRuntime(1094): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
03-26 05:48:09.936: E/AndroidRuntime(1094): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
03-26 05:48:09.936: E/AndroidRuntime(1094): at android.support.v4.view.ViewPager.populate(ViewPager.java:1012)
03-26 05:48:09.936: E/AndroidRuntime(1094): at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:523)
03-26 05:48:09.936: E/AndroidRuntime(1094): at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:495)
03-26 05:48:09.936: E/AndroidRuntime(1094): at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:476)
03-26 05:48:09.936: E/AndroidRuntime(1094): at com.example.image_changer.MainActivity1.onTabSelected(MainActivity1.java:82)
03-26 05:48:09.936: E/AndroidRuntime(1094): at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:570)
03-26 05:48:09.936: E/AndroidRuntime(1094): at com.android.internal.app.ActionBarImpl$TabImpl.select(ActionBarImpl.java:1067)
03-26 05:48:09.936: E/AndroidRuntime(1094): at com.android.internal.widget.ScrollingTabContainerView$TabClickListener.onClick(ScrollingTabContainerView.java:519)
03-26 05:48:09.936: E/AndroidRuntime(1094): at android.view.View.performClick(View.java:4202)
03-26 05:48:09.936: E/AndroidRuntime(1094): at android.view.View$PerformClick.run(View.java:17340)
03-26 05:48:09.936: E/AndroidRuntime(1094): at android.os.Handler.handleCallback(Handler.java:725)
03-26 05:48:09.936: E/AndroidRuntime(1094): at android.os.Handler.dispatchMessage(Handler.java:92)
03-26 05:48:09.936: E/AndroidRuntime(1094): at android.os.Looper.loop(Looper.java:137)
03-26 05:48:09.936: E/AndroidRuntime(1094): at android.app.ActivityThread.main(ActivityThread.java:5039)
03-26 05:48:09.936: E/AndroidRuntime(1094): at java.lang.reflect.Method.invokeNative(Native Method)
03-26 05:48:09.936: E/AndroidRuntime(1094): at java.lang.reflect.Method.invoke(Method.java:511)
03-26 05:48:09.936: E/AndroidRuntime(1094): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-26 05:48:09.936: E/AndroidRuntime(1094): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-26 05:48:09.936: E/AndroidRuntime(1094): at dalvik.system.NativeStart.main(Native Method)
03-26 05:48:09.936: E/AndroidRuntime(1094): Caused by: java.lang.IllegalArgumentException: Binary XML file line #10: Duplicate id 0x7f050010, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
03-26 05:48:09.936: E/AndroidRuntime(1094): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:285)
03-26 05:48:09.936: E/AndroidRuntime(1094): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
03-26 05:48:09.936: E/AndroidRuntime(1094): ... 30 more
03-26 05:48:14.426: I/Process(1094): Sending signal. PID: 1094 SIG: 9
03-26 05:48:15.578: W/Trace(1203): Unexpected value from nativeGetEnabledTags: 0
03-26 05:48:15.578: W/Trace(1203): Unexpected value from nativeGetEnabledTags: 0
03-26 05:48:15.656: I/System.out(1203): you are in on create in main activity
03-26 05:48:16.306: D/dalvikvm(1203): GC_FOR_ALLOC freed 74K, 7% free 2616K/2812K, paused 51ms, total 53ms
03-26 05:48:16.576: D/dalvikvm(1203): GC_CONCURRENT freed 2K, 6% free 3025K/3200K, paused 111ms+102ms, total 262ms
03-26 05:48:16.736: W/Trace(1203): Unexpected value from nativeGetEnabledTags: 0
03-26 05:48:16.736: W/Trace(1203): Unexpected value from nativeGetEnabledTags: 0
03-26 05:48:16.746: W/Trace(1203): Unexpected value from nativeGetEnabledTags: 0
03-26 05:48:16.776: W/Trace(1203): Unexpected value from nativeGetEnabledTags: 0
03-26 05:48:16.966: D/libEGL(1203): loaded /system/lib/egl/libEGL_emulation.so
03-26 05:48:17.056: D/(1203): HostConnection::get() New Host Connection established 0x2a187918, tid 1203
03-26 05:48:17.376: D/libEGL(1203): loaded /system/lib/egl/libGLESv1_CM_emulation.so
03-26 05:48:17.407: D/libEGL(1203): loaded /system/lib/egl/libGLESv2_emulation.so
03-26 05:48:18.176: W/EGL_emulation(1203): eglSurfaceAttrib not implemented
03-26 05:48:18.997: D/OpenGLRenderer(1203): Enabling debug mode 0
03-26 05:48:18.997: W/Trace(1203): Unexpected value from nativeGetEnabledTags: 0
03-26 05:48:19.018: W/Trace(1203): Unexpected value from nativeGetEnabledTags: 0
03-26 05:48:19.026: W/Trace(1203): Unexpected value from nativeGetEnabledTags: 0
03-26 05:48:19.176: W/Trace(1203): Unexpected value from nativeGetEnabledTags: 0
03-26 05:48:19.186: W/Trace(1203): Unexpected value from nativeGetEnabledTags: 0
03-26 05:48:19.218: W/Trace(1203): Unexpected value from nativeGetEnabledTags: 0
03-26 05:48:19.218: W/Trace(1203): Unexpected value from nativeGetEnabledTags: 0
03-26 05:48:19.226: W/Trace(1203): Unexpected value from nativeGetEnabledTags: 0
03-26 05:48:19.226: W/Trace(1203): Unexpected value from nativeGetEnabledTags: 0
03-26 05:48:19.226: W/Trace(1203): Unexpected value from nativeGetEnabledTags: 0
03-26 05:48:19.436: W/Trace(1203): Unexpected value from nativeGetEnabledTags: 0
03-26 05:48:19.436: W/Trace(1203): Unexpected value from nativeGetEnabledTags: 0
03-26 05:48:19.436: W/Trace(1203): Unexpected value from nativeGetEnabledTags: 0
通过单击Logcat中的包名称相关错误,它会显示这些错误。
首先在MainActivity1.java中
View rootView = inflater.inflate(R.layout.map, container, false);
Topratedfragment.java中的第二个
viewPager.setCurrentItem(tab.getPosition());
这是MainActivity1.java文件
package com.example.image_changer;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Window;
import android.view.WindowManager;
public class MainActivity1 extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Map", "Games", "Image flipper" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main1);
/*MainActivity1.this.getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);*/
getActionBar().setBackgroundDrawable(
new ColorDrawable(Color.parseColor("#ee7b35")));
System.out.println("you are in oncreate in mainactivity1");
// 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
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.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) {
}
});
}
@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());
System.out.println("you are in onTabreselected");
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
System.out.println("you are in onTabunselected");
}
}
这是我的TopRatedfragment.java文件
package com.example.image_changer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class TopRatedFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.map, container, false);
return rootView;
}
}
我试过这个解决方案 -
当我在Topratedfragment.java中将extends
Fragment
更改为FragmentActivity
时,它会给我错误 - The method onCreateView(LayoutInflater, ViewGroup, Bundle) of type TopRatedFragment must override or implement a supertype method
。 Remove override notation
快速修复。
当我删除覆盖表示法时,它在Tabspageradapter.java类中给出错误并且错误说 - 类型不匹配:无法从TopRatedFragment转换为Fragment并将方法返回类型更改为TopRatedFragment
并且可能这对于我
package com.example.image_changer;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm){
super(fm);
}
@Override
public Fragment getItem(int index) {
// TODO Auto-generated method stub
switch (index) {
case 0:
return new TopRatedFragment();
case 1:
return new GamesFragment();
case 2:
return new MoviesFragment();
}
return null;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 3;
}
}
这是GameFragment.java类
package com.example.image_changer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class GamesFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
//return super.onCreateView(inflater, container, savedInstanceState);
View rootView = inflater.inflate(R.layout.fragment_games,container,false );
return rootView;
}
}
那么如何解决这个问题。谢谢你的帮助
答案 0 :(得分:5)
在TopRatedfragment.java中使用此代码解决了这个问题:
private static View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (view != null) {
ViewGroup parent = (ViewGroup) view.getParent();
if (parent != null)
parent.removeView(view);
}
try {
view = inflater.inflate(R.layout.map, container, false);
} catch (InflateException e) {
/* map is already there, just return view as it is */
}
return view;
}
答案 1 :(得分:0)
Caused by: java.lang.IllegalArgumentException: Binary XML file line #10: Duplicate id 0x7f050010, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
你对xml的错误是完全正确的,但这只是因为你正在创建一个android已经在堆栈中保存的新片段。
而不是创建一个新的fragemtn,你想使用FragmentManager
FragmentManager fman=getFragmentManager();
fman.popBackStack();
这将是这些方面的东西....更准确的代码....
Fragment fragment = (Fragment) getFragmentManager().
.findFragmentById(R.your id here);
if (fragment != null && fragment.isInLayout()) {
fman.beginTransaction().show(fragment)
} else {
Fragment fragment = (Fragment) getFragmentManager().beginTransaction().add(your container-the file the error is coming from that holds all you fragments, fragment)
fman.beginTransaction().show(fragment)
}