我有点陷入这个任务。我有一个应用程序显示嵌入谷歌地图的主要片段,现在我想用另一个片段替换它,当用户点击抽屉菜单列表中的发送按钮时显示一些图表。
当我点击发送应用程序卡住时,给我讨厌的“应用程序无响应”。但是,如果我将android:name字段更改为相同的地图片段,它将正常工作并显示地图。
我启动地图片段
的主要活动代码段 protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.activity_main);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
mapsFragment = new MapsFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.maincontainer,mapsFragment)
.commit();
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
主要活动代码段,我在其中启动图表片段
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
Snackbar snackbar;
Fragment fragment = null;
switch(item.getItemId()){
case R.id.nav_manage:
snackbar = Snackbar
.make(coordinatorLayout, "manage", Snackbar.LENGTH_LONG);
snackbar.show();
break;
case R.id.nav_share:
snackbar = Snackbar
.make(coordinatorLayout, "share", Snackbar.LENGTH_LONG);
snackbar.show();
break;
case R.id.nav_send:
snackbar = Snackbar
.make(coordinatorLayout, "send", Snackbar.LENGTH_LONG);
snackbar.show();
fragment = new ChartsFragment();
break;
case R.id.nav_view:
snackbar = Snackbar
.make(coordinatorLayout, "view", Snackbar.LENGTH_LONG);
snackbar.show();
break;
}
if(fragment != null){
getSupportFragmentManager().beginTransaction()
.replace(R.id.maincontainer,fragment)
.commit();
}
// Highlight the selected item has been done by NavigationView
item.setChecked(true);
// Set action bar title
//setTitle(item.getTitle());
DrawerLayout drawer = (DrawerLayout)
findViewById(R.id.activity_main);
drawer.closeDrawer(GravityCompat.START);
return true;
}
MapsFragment onCreate
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_maps, container, false);
SupportMapFragment mMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
mMapFragment.getMapAsync(this);
mContext = getActivity();
return rootView;
}
ChartsFragment onCreate
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_charts, container, false);
return rootView;
}
主appbar xml
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:id="@+id/coordinator_layout_app_bar_main"
tools:context="com.fabio.gis.geotag.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_activity" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/place_marker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="80dp"
android:layout_gravity="bottom|end"
android:layout_marginRight="16dp"
app:backgroundTint="@color/secondaryColorAccent"
app:elevation="6dp"
app:pressedTranslationZ="12dp"
android:src="@drawable/ic_marker" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/send_positions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
app:elevation="6dp"
app:backgroundTint="@color/colorAccent"
app:pressedTranslationZ="12dp"
android:layout_margin="16dp"
android:src="@drawable/ic_play_arrow_white_24dp" />
</android.support.design.widget.CoordinatorLayout>
地图片段
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map_ui"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent">
</fragment>
</RelativeLayout>
Charts Fragment
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/charts_ui"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/charts"
android:name="com.fabio.gis.geotag.ChartsFragment"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</fragment>
</RelativeLayout>
我已经尝试替换
机器人:名称= “com.fabio.gis.geotag.ChartsFragment”
与
类= “com.fabio.gis.geotag.ChartsFragment”
没有任何改变。
提前感谢您的支持。我意识到我是Android的初学者,也许我正在为一个愚蠢的新手错误而苦苦挣扎。
过了一会儿,它仍留在ANR中,它给了我以下的堆栈跟踪错误
03-06 17:11:43.322 7033-7033/? E/AndroidRuntime: at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:189)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:769)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at com.fabio.gis.geotag.ChartsFragment.onCreateView(ChartsFragment.java:20)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2080)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1065)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1268)
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1370)
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2415)
at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:189)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:769)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at com.fabio.gis.geotag.ChartsFragment.onCreateView(ChartsFragment.java:20)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2080)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1065)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1268)
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1370)
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2415)
at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:189)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:769)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at com.fabio.gis.geotag.ChartsFragment.onCreateView(ChartsFragment.java:20)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2080)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1065)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1268)
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1370)
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2415)
at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:189)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:769)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
read: unexpected EOF!
答案 0 :(得分:0)