我需要在fragment内部使用操作栏按钮。所以我试图通过使用选项菜单项来实现,但它显示以下错误.............
- 02-22 13:40:27.560: E/AndroidRuntime(2852): FATAL EXCEPTION: main
02-22 13:40:27.560: E/AndroidRuntime(2852): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.t3chtr3ndz.sportz/info.tech.slidermenu.slider_main}: android.view.InflateException: Binary XML file line #52: Error inflating class fragment
02-22 13:40:27.560: E/AndroidRuntime(2852): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
02-22 13:40:27.560: E/AndroidRuntime(2852): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
02-22 13:40:27.560: E/AndroidRuntime(2852): at android.app.ActivityThread.access$600(ActivityThread.java:130)
02-22 13:40:27.560: E/AndroidRuntime(2852): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
02-22 13:40:27.560: E/AndroidRuntime(2852): at android.os.Handler.dispatchMessage(Handler.java:99)
02-22 13:40:27.560: E/AndroidRuntime(2852): at android.os.Looper.loop(Looper.java:137)
02-22 13:40:27.560: E/AndroidRuntime(2852): at android.app.ActivityThread.main(ActivityThread.java:4745)
02-22 13:40:27.560: E/AndroidRuntime(2852): at java.lang.reflect.Method.invokeNative(Native Method)
02-22 13:40:27.560: E/AndroidRuntime(2852): at java.lang.reflect.Method.invoke(Method.java:511)
02-22 13:40:27.560: E/AndroidRuntime(2852): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-22 13:40:27.560: E/AndroidRuntime(2852): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-22 13:40:27.560: E/AndroidRuntime(2852): at dalvik.system.NativeStart.main(Native Method)
02-22 13:40:27.560: E/AndroidRuntime(2852): Caused by: android.view.InflateException: Binary XML file line #52: Error inflating class fragment
02-22 13:40:27.560: E/AndroidRuntime(2852): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
02-22 13:40:27.560: E/AndroidRuntime(2852): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
02-22 13:40:27.560: E/AndroidRuntime(2852): at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
02-22 13:40:27.560: E/AndroidRuntime(2852): at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
02-22 13:40:27.560: E/AndroidRuntime(2852): at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
02-22 13:40:27.560: E/AndroidRuntime(2852): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
02-22 13:40:27.560: E/AndroidRuntime(2852): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
02-22 13:40:27.560: E/AndroidRuntime(2852): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
02-22 13:40:27.560: E/AndroidRuntime(2852): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
02-22 13:40:27.560: E/AndroidRuntime(2852): at android.app.Activity.setContentView(Activity.java:1867)
02-22 13:40:27.560: E/AndroidRuntime(2852): at info.tech.slidermenu.slider_main.onCreate(slider_main.java:61)
02-22 13:40:27.560: E/AndroidRuntime(2852): at android.app.Activity.performCreate(Activity.java:5008)
02-22 13:40:27.560: E/AndroidRuntime(2852): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
02-22 13:40:27.560: E/AndroidRuntime(2852): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
02-22 13:40:27.560: E/AndroidRuntime(2852): ... 11 more
02-22 13:40:27.560: E/AndroidRuntime(2852): Caused by: java.lang.NullPointerException
02-22 13:40:27.560: E/AndroidRuntime(2852): at info.tech.slidermenu.slider_main.onPrepareOptionsMenu(slider_main.java:219)
02-22 13:40:27.560: E/AndroidRuntime(2852): at android.app.Activity.onPreparePanel(Activity.java:2494)
02-22 13:40:27.560: E/AndroidRuntime(2852): at android.support.v4.app.FragmentActivity.onPrepareOptionsPanel(FragmentActivity.java:491)
02-22 13:40:27.560: E/AndroidRuntime(2852): at android.support.v4.app.FragmentActivity.onPreparePanel(FragmentActivity.java:480)
02-22 13:40:27.560: E/AndroidRuntime(2852): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:421)
02-22 13:40:27.560: E/AndroidRuntime(2852): at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:747)
02-22 13:40:27.560: E/AndroidRuntime(2852): at android.app.Activity.invalidateOptionsMenu(Activity.java:2595)
02-22 13:40:27.560: E/AndroidRuntime(2852): at android.support.v4.app.ActivityCompatHoneycomb.invalidateOptionsMenu(ActivityCompatHoneycomb.java:29)
02-22 13:40:27.560: E/AndroidRuntime(2852): at android.support.v4.app.FragmentActivity.supportInvalidateOptionsMenu(FragmentActivity.java:648)
02-22 13:40:27.560: E/AndroidRuntime(2852): at android.support.v4.app.Fragment.setHasOptionsMenu(Fragment.java:803)
02-22 13:40:27.560: E/AndroidRuntime(2852): at info.tech.slidermenu.Home_feed.onCreateView(Home_feed.java:93)
02-22 13:40:27.560: E/AndroidRuntime(2852): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
02-22 13:40:27.560: E/AndroidRuntime(2852): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:900)
02-22 13:40:27.560: E/AndroidRuntime(2852): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
02-22 13:40:27.560: E/AndroidRuntime(2852): at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1184)
02-22 13:40:27.560: E/AndroidRuntime(2852): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:291)
02-22 13:40:27.560: E/AndroidRuntime(2852): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
02-22 13:40:27.560: E/AndroidRuntime(2852): ... 24 more
02-22 13:42:02.621: E/Trace(2902): error opening trace file: No such file or directory (2)
public class Home_feed extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mTitle = mDrawerTitle = "title";
ActionBar bar = getActivity().getActionBar();
View rootView = inflater.inflate(
com.t3chtr3ndz.sportz.R.layout.home_feed, container, false);
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
navMenuIcons = getResources()
.obtainTypedArray(R.array.nav_drawer_icons);
mDrawerLayout = (DrawerLayout) rootView
.findViewById(R.id.drawer_layout);
mDrawerList = (ListView) rootView
.findViewById(R.id.list_slidermenu_home);
navDrawerItems = new ArrayList<NavDrawerItem>();
navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons
.getResourceId(0, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons
.getResourceId(1, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons
.getResourceId(2, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons
.getResourceId(3, -1)));
navMenuIcons.recycle();
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
adapter = new NavDrawerListAdapter(getActivity(), navDrawerItems);
mDrawerList.setAdapter(adapter);
getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
getActivity().getActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(getActivity(), mDrawerLayout,
R.drawable.ic_drawer, R.string.app_name, R.string.app_name) {
public void onDrawerClosed(View view) {
getActivity().getActionBar().setTitle(mTitle);
}
public void onDrawerOpened(View drawerView) {
getActivity().getActionBar().setTitle(mDrawerTitle);
}
};
ListView listview = (ListView) rootView
.findViewById(com.t3chtr3ndz.sportz.R.id.Home_feed_list);
listview.setAdapter(new adapter(getActivity()));
setHasOptionsMenu(true);
return rootView;
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
super.onPrepareOptionsMenu(menu);
}
private class SlideMenuClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// display view for selected nav drawer item
Log.i("click", "unsuccess");
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.i("item", "" + item.getItemId());
if (mDrawerToggle.onOptionsItemSelected(item)) {
Log.i("item", "action icon");
return true;
}
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
这是我的xml文件。
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<!-- Listview to display slider menu -->
<ListView
android:id="@+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/list_background"
android:choiceMode="singleChoice"
android:divider="@color/list_divider"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector" />
<RelativeLayout
android:id="@+id/rl3"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TabHost
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="@drawable/gray" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" >
<fragment
android:id="@+id/fragment1"
android:name="info.tech.slidermenu.Upcoming_tennise_match"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="50dp" />
<fragment
android:id="@+id/fragment2"
android:name="info.tech.slidermenu.Upcoming_tennise_match"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="50dp" />
<fragment
android:id="@+id/fragment3"
android:name="info.tech.slidermenu.Upcoming_tennise_match"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="50dp" />
</FrameLayout>
</TabHost>
</RelativeLayout>
答案 0 :(得分:1)
您的问题是Null Pointer错误,在此函数中:
@Override
public void onPrepareOptionsMenu(Menu menu) {
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
super.onPrepareOptionsMenu(menu);
}
我的猜测是mDrawerLayout为null。为什么它可以为空?
底线,我会看到mDrawLayout
等于什么,如果为null,找出它从未设置的原因。如果它不是空值,请确定设置的内容。