我尝试了以下解决方案,没有人为我工作。
1)When inflating fragment which was previously inflated app crashes
2)android.view.InflateException: Binary XML file line #20: Error inflating class fragment我认为没有嵌套片段
我在第一次完成后运行应用程序时遇到崩溃Error inflating class fragment
。
当Open App第一次一切都很好。当打开应用程序第二次崩溃。当第三次打开然后app工作正常。等等。
以下是我的Activity
片段
<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.taazi.Activities.MainActivity" >
<!--
As the main content view, the view below consumes the entire
space available using match_parent in both dimensions.
-->
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!--
android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
If you're not building against API 17 or higher, use
android:layout_gravity="left" instead.
-->
<!--
The drawer is given a fixed width in dp and extends the full height of
the container.
-->
<fragment
android:id="@+id/navigation_drawer_left"
android:name="com.taazi.Fragments.NavigationDrawerFragmentLeft"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="left" />
<fragment
android:id="@+id/navigation_drawer_right"
android:name="com.taazi.Fragments.NavigationDrawerFragmentRight"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="right" />
</android.support.v4.widget.DrawerLayout>
主要代码Activity
package com.taazi.Activities;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import com.taazi.Fragments.NavigationDrawerFragmentLeft;
import com.taazi.Fragments.NavigationDrawerFragmentRight;
import com.taazi.Fragments.PlaceholderFragment;
import com.taazi.Fragments.PlayerFragment;
import com.taazi.android.R;
public class MainActivity extends BaseActivity implements
NavigationDrawerFragmentLeft.NavigationDrawerCallbacksLeft,NavigationDrawerFragmentRight.NavigationDrawerCallbacksRight{
/**
* Fragment managing the behaviors, interactions and presentation of the
* navigation drawer.
*/
private NavigationDrawerFragmentLeft mNavigationDrawerFragmentLeft;
private NavigationDrawerFragmentRight mNavigationDrawerFragmentRight;
/**
* Used to store the last screen title. For use in
* {@link #restoreActionBar()}.
*/
private CharSequence mTitle;
public static Context context;
public static ActionBar actionBar;
Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
mNavigationDrawerFragmentLeft = (NavigationDrawerFragmentLeft) getSupportFragmentManager()
.findFragmentById(R.id.navigation_drawer_left);
mNavigationDrawerFragmentRight = (NavigationDrawerFragmentRight) getSupportFragmentManager()
.findFragmentById(R.id.navigation_drawer_right);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragmentLeft.setUp(R.id.navigation_drawer_left,
(DrawerLayout) findViewById(R.id.drawer_layout));
mNavigationDrawerFragmentRight.setUp(R.id.navigation_drawer_right,
(DrawerLayout) findViewById(R.id.drawer_layout));
}
@Override
public void finish() {
super.finish();
}
public void onSectionAttached(int number) {
switch (number) {
case 1:
mTitle = getString(R.string.title_section1);
break;
case 2:
mTitle = getString(R.string.title_section2);
break;
case 3:
mTitle = getString(R.string.title_section3);
break;
}
}
public void restoreActionBar() {
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragmentLeft.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.main, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch (id) {
case R.id.action_right_drawer:
toggleRightDrawer();
return true;
case android.R.id.home:
toggleLeftDrawer();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
public static void toggleLeftDrawer(){
if(NavigationDrawerFragmentRight.mDrawerLayout.isDrawerOpen(Gravity.RIGHT)){
NavigationDrawerFragmentRight.mDrawerLayout.closeDrawer(Gravity.RIGHT);
}
if(NavigationDrawerFragmentLeft.mDrawerLayout.isDrawerOpen(Gravity.LEFT)){
NavigationDrawerFragmentLeft.mDrawerLayout.closeDrawer(Gravity.LEFT);
}else{
NavigationDrawerFragmentLeft.mDrawerLayout.openDrawer(Gravity.LEFT);
}
}
public static void toggleRightDrawer(){
if(NavigationDrawerFragmentLeft.mDrawerLayout.isDrawerOpen(Gravity.START)){
NavigationDrawerFragmentLeft.mDrawerLayout.closeDrawer(Gravity.LEFT);
}
if(NavigationDrawerFragmentRight.mDrawerLayout.isDrawerOpen(Gravity.RIGHT)){
NavigationDrawerFragmentRight.mDrawerLayout.closeDrawer(Gravity.RIGHT);
}else{
NavigationDrawerFragmentRight.mDrawerLayout.openDrawer(Gravity.RIGHT);
}
}
@Override
public void onNavigationDrawerItemSelectedRight(int position) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager
.beginTransaction()
.replace(R.id.container,
PlayerFragment.newInstance(position + 1)).commit();
}
@Override
public void onNavigationDrawerItemSelectedLeft(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager
.beginTransaction()
.replace(R.id.container,
PlaceholderFragment.newInstance(position + 1)).commit();
}
}
日志。
05-28 11:28:58.417: E/AndroidRuntime(22774): FATAL EXCEPTION: main
05-28 11:28:58.417: E/AndroidRuntime(22774): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.taazi.android/com.taazi.Activities.MainActivity}: android.view.InflateException: Binary XML file line #31: Error inflating class fragment
05-28 11:28:58.417: E/AndroidRuntime(22774): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
05-28 11:28:58.417: E/AndroidRuntime(22774): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
05-28 11:28:58.417: E/AndroidRuntime(22774): at android.app.ActivityThread.access$600(ActivityThread.java:156)
05-28 11:28:58.417: E/AndroidRuntime(22774): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
05-28 11:28:58.417: E/AndroidRuntime(22774): at android.os.Handler.dispatchMessage(Handler.java:99)
05-28 11:28:58.417: E/AndroidRuntime(22774): at android.os.Looper.loop(Looper.java:153)
05-28 11:28:58.417: E/AndroidRuntime(22774): at android.app.ActivityThread.main(ActivityThread.java:5297)
05-28 11:28:58.417: E/AndroidRuntime(22774): at java.lang.reflect.Method.invokeNative(Native Method)
05-28 11:28:58.417: E/AndroidRuntime(22774): at java.lang.reflect.Method.invoke(Method.java:511)
05-28 11:28:58.417: E/AndroidRuntime(22774): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
05-28 11:28:58.417: E/AndroidRuntime(22774): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
05-28 11:28:58.417: E/AndroidRuntime(22774): at dalvik.system.NativeStart.main(Native Method)
05-28 11:28:58.417: E/AndroidRuntime(22774): Caused by: android.view.InflateException: Binary XML file line #31: Error inflating class fragment
05-28 11:28:58.417: E/AndroidRuntime(22774): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
05-28 11:28:58.417: E/AndroidRuntime(22774): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
05-28 11:28:58.417: E/AndroidRuntime(22774): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
05-28 11:28:58.417: E/AndroidRuntime(22774): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
05-28 11:28:58.417: E/AndroidRuntime(22774): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
05-28 11:28:58.417: E/AndroidRuntime(22774): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:274)
05-28 11:28:58.417: E/AndroidRuntime(22774): at android.app.Activity.setContentView(Activity.java:1881)
05-28 11:28:58.417: E/AndroidRuntime(22774): at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:216)
05-28 11:28:58.417: E/AndroidRuntime(22774): at android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDelegateICS.java:110)
05-28 11:28:58.417: E/AndroidRuntime(22774): at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:76)
05-28 11:28:58.417: E/AndroidRuntime(22774): at com.taazi.Activities.MainActivity.onCreate(MainActivity.java:42)
05-28 11:28:58.417: E/AndroidRuntime(22774): at android.app.Activity.performCreate(Activity.java:5122)
05-28 11:28:58.417: E/AndroidRuntime(22774): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
05-28 11:28:58.417: E/AndroidRuntime(22774): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
05-28 11:28:58.417: E/AndroidRuntime(22774): ... 11 more
05-28 11:28:58.417: E/AndroidRuntime(22774): Caused by: java.lang.NullPointerException
05-28 11:28:58.417: E/AndroidRuntime(22774): at android.support.v4.widget.DrawerLayout.isDrawerView(DrawerLayout.java:910)
05-28 11:28:58.417: E/AndroidRuntime(22774): at android.support.v4.widget.DrawerLayout.closeDrawer(DrawerLayout.java:1111)
05-28 11:28:58.417: E/AndroidRuntime(22774): at com.taazi.Fragments.NavigationDrawerFragmentLeft.selectItem(NavigationDrawerFragmentLeft.java:159)
05-28 11:28:58.417: E/AndroidRuntime(22774): at com.taazi.Fragments.NavigationDrawerFragmentLeft.onCreate(NavigationDrawerFragmentLeft.java:61)
05-28 11:28:58.417: E/AndroidRuntime(22774): at android.support.v4.app.Fragment.performCreate(Fragment.java:1477)
05-28 11:28:58.417: E/AndroidRuntime(22774): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:904)
05-28 11:28:58.417: E/AndroidRuntime(22774): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1093)
05-28 11:28:58.417: E/AndroidRuntime(22774): at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1195)
05-28 11:28:58.417: E/AndroidRuntime(22774): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:291)
05-28 11:28:58.417: E/AndroidRuntime(22774): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
05-28 11:28:58.417: E/AndroidRuntime(22774): ... 24 more
代码NavigationDrawerFragmentLeft.java
package com.taazi.Fragments;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.taazi.android.R;
/**
* Fragment used for managing interactions for and presentation of a navigation drawer.
* See the <a href="https://developer.android.com/design/patterns/navigation-drawer.html#Interaction">
* design guidelines</a> for a complete explanation of the behaviors implemented here.
*/
public class NavigationDrawerFragmentLeft extends Fragment {
/**
* Remember the position of the selected item.
*/
private static final String STATE_SELECTED_POSITION = "selected_navigation_drawer_position";
/**
* A pointer to the current callbacks instance (the Activity).
*/
private NavigationDrawerCallbacksLeft mCallbacks;
/**
* Helper component that ties the action bar to the navigation drawer.
*/
private ActionBarDrawerToggle mDrawerToggle;
public static DrawerLayout mDrawerLayout;
private ListView mDrawerListView;
private View mFragmentContainerView;
private int mCurrentSelectedPosition = 0;
public NavigationDrawerFragmentLeft() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION);
}
// Select either the default item (0) or the last selected item.
selectItem(mCurrentSelectedPosition);
}
@Override
public void onActivityCreated (Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Indicate that this fragment would like to influence the set of actions in the action bar.
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mDrawerListView = (ListView) inflater.inflate(
R.layout.fragment_navigation_drawer, container, false);
mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
});
mDrawerListView.setAdapter(new ArrayAdapter<String>(
getActionBar().getThemedContext(),
android.R.layout.simple_list_item_1,
android.R.id.text1,
new String[]{
getString(R.string.title_section1),
getString(R.string.title_section2),
getString(R.string.title_section3),
}));
mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
return mDrawerListView;
}
public boolean isDrawerOpen() {
return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(mFragmentContainerView);
}
/**
* Users of this fragment must call this method to set up the navigation drawer interactions.
*
* @param fragmentId The android:id of this fragment in its activity's layout.
* @param drawerLayout The DrawerLayout containing this fragment's UI.
*/
public void setUp(int fragmentId, DrawerLayout drawerLayout) {
mFragmentContainerView = getActivity().findViewById(fragmentId);
mDrawerLayout = drawerLayout;
// set a custom shadow that overlays the main content when the drawer opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
// set up the drawer's list view with items and click listener
// ActionBarDrawerToggle ties together the the proper interactions
// between the navigation drawer and the action bar app icon.
mDrawerToggle = new ActionBarDrawerToggle(
getActivity(), /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.navigation_drawer_open, /* "open drawer" description for accessibility */
R.string.navigation_drawer_close /* "close drawer" description for accessibility */
) {
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
if (!isAdded()) {
return;
}
getActivity().supportInvalidateOptionsMenu(); // calls onPrepareOptionsMenu()
}
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
if (!isAdded()) {
return;
}
getActivity().supportInvalidateOptionsMenu(); // calls onPrepareOptionsMenu()
}
};
// Defer code dependent on restoration of previous instance state.
mDrawerLayout.post(new Runnable() {
@Override
public void run() {
mDrawerToggle.syncState();
}
});
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
private void selectItem(int position) {
mCurrentSelectedPosition = position;
if (mDrawerListView != null) {
mDrawerListView.setItemChecked(position, true);
}
if (mDrawerLayout != null) {
mDrawerLayout.closeDrawer(mFragmentContainerView);
}
if (mCallbacks != null) {
mCallbacks.onNavigationDrawerItemSelectedLeft(position);
}
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mCallbacks = (NavigationDrawerCallbacksLeft) activity;
} catch (ClassCastException e) {
throw new ClassCastException("Activity must implement NavigationDrawerCallbacks.");
}
}
@Override
public void onDetach() {
super.onDetach();
mCallbacks = null;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(STATE_SELECTED_POSITION, mCurrentSelectedPosition);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Forward the new configuration the drawer toggle component.
mDrawerToggle.onConfigurationChanged(newConfig);
}
private ActionBar getActionBar() {
return ((ActionBarActivity) getActivity()).getSupportActionBar();
}
/**
* Callbacks interface that all activities using this fragment must implement.
*/
public static interface NavigationDrawerCallbacksLeft {
/**
* Called when an item in the navigation drawer is selected.
*/
void onNavigationDrawerItemSelectedLeft(int position);
}
}
答案 0 :(得分:-1)
您可以尝试替换此行:
`android:name="com.taazi.Fragments.NavigationDrawerFragmentLeft"`
与
class = "com.fragment.NavigationDrawerFragment"
答案 1 :(得分:-2)
从抽屉布局中删除静电......我有同样的问题..