gpt4 book ai didi

安卓.view.InflateException : Binary XML file error

转载 作者:行者123 更新时间:2023-11-30 02:13:45 26 4
gpt4 key购买 nike

我创建了一个应用程序,当它打开时会显示 map ,当我想在按下 ActionBar 中的按钮时打开 NavigationFragment。我在我的应用程序中实现了 map ,它工作得很好,但是当我添加 NavigationDrawer 时,我收到了这个错误:

04-18 13:20:49.942: E/AndroidRuntime(5408): Process: com.wunderlist.slidinglayersample, PID: 5408
04-18 13:20:49.942: E/AndroidRuntime(5408): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.wunderlist.slidinglayersample/com.wunderlist.slidinglayersample.MainActivity}: android.view.InflateException: Binary XML file line #72: Error inflating class fragment
04-18 13:20:49.942: E/AndroidRuntime(5408): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
04-18 13:20:49.942: E/AndroidRuntime(5408): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
04-18 13:20:49.942: E/AndroidRuntime(5408): at android.app.ActivityThread.access$800(ActivityThread.java:144)
04-18 13:20:49.942: E/AndroidRuntime(5408): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
04-18 13:20:49.942: E/AndroidRuntime(5408): at android.os.Handler.dispatchMessage(Handler.java:102)
04-18 13:20:49.942: E/AndroidRuntime(5408): at android.os.Looper.loop(Looper.java:135)
04-18 13:20:49.942: E/AndroidRuntime(5408): at android.app.ActivityThread.main(ActivityThread.java:5221)
04-18 13:20:49.942: E/AndroidRuntime(5408): at java.lang.reflect.Method.invoke(Native Method)
04-18 13:20:49.942: E/AndroidRuntime(5408): at java.lang.reflect.Method.invoke(Method.java:372)
04-18 13:20:49.942: E/AndroidRuntime(5408): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
04-18 13:20:49.942: E/AndroidRuntime(5408): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
04-18 13:20:49.942: E/AndroidRuntime(5408): Caused by: android.view.InflateException: Binary XML file line #72: Error inflating class fragment
04-18 13:20:49.942: E/AndroidRuntime(5408): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
04-18 13:20:49.942: E/AndroidRuntime(5408): at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
04-18 13:20:49.942: E/AndroidRuntime(5408): at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
04-18 13:20:49.942: E/AndroidRuntime(5408): at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
04-18 13:20:49.942: E/AndroidRuntime(5408): at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
04-18 13:20:49.942: E/AndroidRuntime(5408): at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
04-18 13:20:49.942: E/AndroidRuntime(5408): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377)
04-18 13:20:49.942: E/AndroidRuntime(5408): at android.app.Activity.setContentView(Activity.java:2144)
04-18 13:20:49.942: E/AndroidRuntime(5408): at com.wunderlist.slidinglayersample.MainActivity.onCreate(MainActivity.java:83)
04-18 13:20:49.942: E/AndroidRuntime(5408): at android.app.Activity.performCreate(Activity.java:5937)
04-18 13:20:49.942: E/AndroidRuntime(5408): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
04-18 13:20:49.942: E/AndroidRuntime(5408): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
04-18 13:20:49.942: E/AndroidRuntime(5408): ... 10 more
04-18 13:20:49.942: E/AndroidRuntime(5408): Caused by: java.lang.NullPointerException
04-18 13:20:49.942: E/AndroidRuntime(5408): at java.lang.VMClassLoader.findLoadedClass(Native Method)
04-18 13:20:49.942: E/AndroidRuntime(5408): at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:362)
04-18 13:20:49.942: E/AndroidRuntime(5408): at java.lang.ClassLoader.loadClass(ClassLoader.java:499)
04-18 13:20:49.942: E/AndroidRuntime(5408): at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
04-18 13:20:49.942: E/AndroidRuntime(5408): at android.app.Fragment.instantiate(Fragment.java:604)
04-18 13:20:49.942: E/AndroidRuntime(5408): at android.app.Fragment.instantiate(Fragment.java:582)
04-18 13:20:49.942: E/AndroidRuntime(5408): at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2108)
04-18 13:20:49.942: E/AndroidRuntime(5408): at android.app.Activity.onCreateView(Activity.java:5286)
04-18 13:20:49.942: E/AndroidRuntime(5408): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)

XML 文件

    <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">

<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=".MainActivity">


<com.wunderlist.slidinglayer.SlidingLayer
android:id="@+id/slidingLayer1"
android:layout_width="@dimen/layer_size"
android:layout_height="match_parent"

>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#14ad8f"
>

<Button
android:id="@+id/buttonClose"
style="@style/ButtonRed"
android:onClick="buttonClicked"
android:layout_marginBottom="60dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="@string/close_button_container"/>


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Nosaukums"
android:id="@+id/fragNosaukums" />
</RelativeLayout>



</com.wunderlist.slidinglayer.SlidingLayer>

<FrameLayout android:id="@+id/container" android:layout_width="match_parent"
android:layout_height="match_parent" />


<fragment android:id="@+id/navigation_drawer"
android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent"
android:layout_gravity="start"

tools:layout="@layout/fragment_navigation_drawer" />

<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>


</RelativeLayout>
</android.support.v4.widget.DrawerLayout>

MainActivity 类

public class MainActivity extends Activity {


//VARIABLES DEFINED HERE


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

fragNosaukums = (TextView) findViewById(R.id.fragNosaukums);

googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
googleMap.getUiSettings();
googleMap.getUiSettings().setZoomControlsEnabled(true);
googleMap.setMyLocationEnabled(true);

bindViews();
initState();

mSlidingLayer.bringToFront();


}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);

return true;
}

@SuppressLint("NewApi")
@Override
protected void onResume() {
super.onResume();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(true);

}
}

/**
* View binding
*/
private void bindViews() {
mSlidingLayer = (SlidingLayer) findViewById(R.id.slidingLayer1);
// swipeText = (TextView) findViewById(R.id.swipeText);
}

/**
* Initializes the origin state of the layer
*/
private void initState() {

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

setupSlidingLayerPosition(prefs.getString("layer_location", "right"));

setupShadow(prefs.getBoolean("layer_has_shadow", false));
setupLayerOffset(prefs.getBoolean("layer_has_offset", false));
setupPreviewMode(prefs.getBoolean("preview_mode_enabled", false));
}

private void setupSlidingLayerPosition(String layerPosition) {

LayoutParams rlp = (LayoutParams) mSlidingLayer.getLayoutParams();
int textResource;
Drawable d;

// if (layerPosition.equals("right")) {
textResource = R.string.swipe_right_label;
d = getResources().getDrawable(R.drawable.container_rocket_right);

mSlidingLayer.setStickTo(SlidingLayer.STICK_TO_RIGHT);


d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
// swipeText.setCompoundDrawables(null, d, null, null);
// swipeText.setText(getResources().getString(textResource));
mSlidingLayer.setLayoutParams(rlp);
}

private void setupShadow(boolean enabled) {
if (enabled) {
mSlidingLayer.setShadowSizeRes(R.dimen.shadow_size);
mSlidingLayer.setShadowDrawable(R.drawable.sidebar_shadow);
} else {
mSlidingLayer.setShadowSize(0);
mSlidingLayer.setShadowDrawable(null);
}
}

private void setupLayerOffset(boolean enabled) {
int offsetDistance = enabled ? getResources().getDimensionPixelOffset(R.dimen.offset_distance) : 0;
mSlidingLayer.setOffsetDistance(offsetDistance);
}

private void setupPreviewMode(boolean enabled) {
int previewOffset = enabled ? getResources().getDimensionPixelOffset(R.dimen.preview_offset_distance) : -1;
mSlidingLayer.setPreviewOffsetDistance(previewOffset);
}

public void buttonClicked(View v) {
switch (v.getId()) {
// case R.id.buttonOpen:
// mSlidingLayer.openLayer(true);
// break;
case R.id.buttonClose:
mSlidingLayer.closeLayer(true);
break;
}
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (mSlidingLayer.isOpened()) {
mSlidingLayer.closeLayer(true);
return true;
}

default:
return super.onKeyDown(keyCode, event);
}
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {


case R.id.addNewEvent:

Intent intent = new Intent(this, AddEvent.class);
startActivity(intent);
;

case android.R.id.home:
//Do stuff
//mDrawerLayout.openDrawer(Gravity.LEFT);
return true;
}


return true;
}


}

有人能帮忙吗?

更新

我添加了这一行:android:name="com.google.android.gms.maps.MapFragment"正如 kamil zych 所建议的,我现在遇到了这个错误:

04-18 13:45:01.999: E/AndroidRuntime(5473): Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class com.wunderlist.slidinglayersample.NavigationDrawerFragment that is not a Fragment
04-18 13:45:01.999: E/AndroidRuntime(5473): at android.app.Fragment.instantiate(Fragment.java:606)
04-18 13:45:01.999: E/AndroidRuntime(5473): at android.app.Fragment.instantiate(Fragment.java:582)
04-18 13:45:01.999: E/AndroidRuntime(5473): at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2108)
04-18 13:45:01.999: E/AndroidRuntime(5473): at android.app.Activity.onCreateView(Activity.java:5286)
04-18 13:45:01.999: E/AndroidRuntime(5473): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
04-18 13:45:01.999: E/AndroidRuntime(5473): Caused by: java.lang.ClassCastException

这是fragment_navigation_drawer:

<ListView 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" android:choiceMode="singleChoice"
android:divider="@android:color/transparent" android:dividerHeight="0dp"
android:background="#cccc" tools:context=".NavigationDrawerFragment" />

主要 fragment :

<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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity$PlaceholderFragment">

<TextView android:id="@+id/section_label" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="" />

</RelativeLayout>

更新

抽屉导航 fragment :

 */
public class NavigationDrawerFragment extends Fragment {

/**
* Remember the position of the selected item.
*/
private static final String STATE_SELECTED_POSITION = "selected_navigation_drawer_position";

/**
* Per the design guidelines, you should show the drawer on launch until the user manually
* expands it. This shared preference tracks this.
*/
private static final String PREF_USER_LEARNED_DRAWER = "navigation_drawer_learned";

/**
* A pointer to the current callbacks instance (the Activity).
*/
private NavigationDrawerCallbacks mCallbacks;

/**
* Helper component that ties the action bar to the navigation drawer.
*/
private ActionBarDrawerToggle mDrawerToggle;

private DrawerLayout mDrawerLayout;
private ListView mDrawerListView;
private View mFragmentContainerView;

private int mCurrentSelectedPosition = 0;
private boolean mFromSavedInstanceState;
private boolean mUserLearnedDrawer;

public NavigationDrawerFragment() {
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Read in the flag indicating whether or not the user has demonstrated awareness of the
// drawer. See PREF_USER_LEARNED_DRAWER for details.
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
mUserLearnedDrawer = sp.getBoolean(PREF_USER_LEARNED_DRAWER, false);

if (savedInstanceState != null) {
mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION);
mFromSavedInstanceState = true;
}

// 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_activated_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

ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);

// 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;
}

if (!mUserLearnedDrawer) {
// The user manually opened the drawer; store this flag to prevent auto-showing
// the navigation drawer automatically in the future.
mUserLearnedDrawer = true;
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(getActivity());
sp.edit().putBoolean(PREF_USER_LEARNED_DRAWER, true).apply();
}

getActivity().supportInvalidateOptionsMenu(); // calls onPrepareOptionsMenu()
}
};

// If the user hasn't 'learned' about the drawer, open it to introduce them to the drawer,
// per the navigation drawer design guidelines.
if (!mUserLearnedDrawer && !mFromSavedInstanceState) {
mDrawerLayout.openDrawer(mFragmentContainerView);
}

// 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.onNavigationDrawerItemSelected(position);
}
}

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mCallbacks = (NavigationDrawerCallbacks) 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);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// If the drawer is open, show the global app actions in the action bar. See also
// showGlobalContextActionBar, which controls the top-left area of the action bar.
if (mDrawerLayout != null && isDrawerOpen()) {
inflater.inflate(R.menu.menu, menu);
showGlobalContextActionBar();
}
super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}

if (item.getItemId() == R.id.action_example) {
Toast.makeText(getActivity(), "Example action.", Toast.LENGTH_SHORT).show();
return true;
}

return super.onOptionsItemSelected(item);
}

/**
* Per the navigation drawer design guidelines, updates the action bar to show the global app
* 'context', rather than just what's in the current screen.
*/
private void showGlobalContextActionBar() {
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setTitle(R.string.app_name);
}

private ActionBar getActionBar() {
return ((ActionBarActivity) getActivity()).getSupportActionBar();
}

/**
* Callbacks interface that all activities using this fragment must implement.
*/
public static interface NavigationDrawerCallbacks {
/**
* Called when an item in the navigation drawer is selected.
*/
void onNavigationDrawerItemSelected(int position);
}
}

最佳答案

您必须以与开始标记匹配的结束结束您的 xml。

因此,如果您的 xml 开头为

<android.support.v4.widget.DrawerLayout
..
.. >

你也必须关闭它,所以在文件底部添加它

</android.support.v4.widget.DrawerLayout>

关于安卓.view.InflateException : Binary XML file error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29718023/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com