我一直在为这个程序工作,感觉就像是永远的,我觉得我很接近它实际上工作,但是当我关闭抽屉时,它关闭一半会改变页面标题然后崩溃应用程序。我想我已经指出实际发生错误的地方,但对于我的生活,我看不到它。
我已经浏览了developer.android.com上的导航抽屉信息,我已经搜索了问题,但我似乎无法看到它是什么。
所以也许一双更有经验的新眼睛可以找到它。
这是我认为错误发生的代码部分。
private void selectItem(int position) {
// Create a new fragment and specify the planet to show based on position
Fragment fragment = new PageFragment();
Bundle args = new Bundle();
args.putInt(PageFragment.ARG_PAGE_NUMBER, position);
fragment.setArguments(args);
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment)
.commit();
// Highlight the selected item, update the title, and close the drawer
drawerList.setItemChecked(position, true);
setTitle(colerainePages[position]);
drawerLayout.closeDrawer(drawerList);
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
selectItem(position);
}
}
public static class PageFragment extends Fragment {
public static final String ARG_PAGE_NUMBER = "page_number";
public PageFragment() {
// Empty constructor required for fragment subclasses
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
logcat错误代码显示它是onCreateView方法中的“View rootView”行,似乎是说我的HomePage(我点击的按钮)没有设置为片段,但是在fragment_main.xml中,我把它设置为一个片段并拥有HomeScreen设置的类,所以它扩展了Fragment,无论如何都是错误堆栈:
android.view.InflateException: Binary XML file line #9: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
com.project.ColeraineTownApp.MainActivity$PageFragment.onCreateView(MainActivity.java:146)
at android.app.Fragment.performCreateView(Fragment.java:1699)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1075)
at android.app.BackStackRecord.run(BackStackRecord.java:682)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1455)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5306)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class com.project.ColeraineTownApp.HomePage that is not a Fragment
at android.app.Fragment.instantiate(Fragment.java:584)
at android.app.Fragment.instantiate(Fragment.java:560)
at android.app.Activity.onCreateView(Activity.java:4850)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:686)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at com.project.ColeraineTownApp.MainActivity$PageFragment.onCreateView(MainActivity.java:146)
at android.app.Fragment.performCreateView(Fragment.java:1699)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1075)
at android.app.BackStackRecord.run(BackStackRecord.java:682)
编辑:
我已经完成了代码并确保所有内容都调用片段,但是我仍然无法弄明白。
下面是HomePage类和fragment_main xml文件。
主页类:
package com.project.ColeraineTownApp;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class HomePage extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.home_fragment, container, false);
return view;
}
}
fragment_main xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FragmentExampleActivity" >
<fragment
android:id="@+id/home_fragment"
android:name="com.project.ColeraineTownApp.HomePage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
tools:layout="@layout/home_fragment" />
<fragment
android:id="@+id/shopping_fragment"
android:name="com.project.ColeraineTownApp.Shopping"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
tools:layout="@layout/shopping_fragment" />
</RelativeLayout>
这是PageFragment类:
public static class PageFragment extends Fragment {
public static final String ARG_PAGE_NUMBER = "page_number";
public PageFragment() {
// Empty constructor required for fragment subclasses
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
请参阅上文,了解在inflater中调用的fragment_main xml
答案 0 :(得分:0)
堆栈跟踪似乎表明您尝试将com.project.ColeraineTownApp.HomePage
作为片段附加到您的活动的某个位置。此对象不会扩展android.app.Fragment
,而FragmentManager会跳过它。
我无法根据您提供的代码找到原因,因此您可能希望在项目中搜索对HomePage
的引用。它可能位于<fragment>
标记中,特别是通过FragmentTransaction添加,或者可能被添加到TaskStackBuilder。