我有一个非常令人沮丧的错误,我无法解释.我创建了一个Android应用程序,使用Android AppCompat使其与旧版本兼容.以下是我的主要活动布局文件:

<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=".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"
        android:layout_width="@dimen/navigation_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:name="com.fragment.NavigationDrawerFragment" />

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

以下是我活动的主要代码:

public class MainActivity extends ActionBarActivity {
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
   }
}

这里的主要问题是:上述代码在几乎所有设备(受刺激设备或一些真实设备)上都能顺利运行.但当我在三星S3上运行它时.它注意到这个错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{view.MainActivity}: android.view.InflateException: Binary XML file line #25: Error inflating class fragment
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2081)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2106)
            at android.app.ActivityThread.access$700(ActivityThread.java:134)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1217)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4856)
            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:1007)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.view.InflateException: Binary XML file line #25: Error inflating class fragment
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:316)
            at android.app.Activity.setContentView(Activity.java:1901)
            at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:208)
            at android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDelegateICS.java:111)
            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:76)

请告诉我如何修复错误,谢谢:)

推荐答案

经过长时间的调试,我已经解决了这个问题.(尽管我仍然无法解释原因).我把android:name号楼改成class号楼.(虽然在Android文档中,他们说这些属性是相同的,但它是有效的!!!)

因此,它应该从:

 android:name="com.fragment.NavigationDrawerFragment"

class = "com.fragment.NavigationDrawerFragment"

因此,新的布局应该是:

<!-- 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 至 treat
     this as a sliding drawer on the left side for left-至-right
     languages and on the right side for right-至-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"
    android:layout_width="@dimen/navigation_drawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    class = "com.fragment.NavigationDrawerFragment" />

希望这有帮助:)

Android相关问答推荐

Android编写动画在发布版本中崩溃

Jetpack编写Lazy列滑动删除动画不顺利结束

如何消除脚手架和顶杆(material 3)更改 colored颜色 时的延迟?

Android:微调:在代码中设置ArrayAdapter不希望调用On ItemSelected,仅当用户单击微调时调用

使用Android Jetpack Compose,为子Composable定义ViewModel是不是一种糟糕的做法?

Android Kotlin ImageView内置于Kotlin ImageView中.适配器未按预期更新

我怎样才能画一条线在喷气背包组成和有一个自定义的角落?

在更改MuableState的值后,Android API 26(Oreo)上的Composable不能重新组合

尽管我在onCreate()期间已经初始化,但仍出现未初始化的late init变量错误

如何删除 Jetpack Compose 中按钮的左边框?

Compose Accompaniist Pager 中的 TabRow/Tab 重组问题

在compose中,为什么修改List元素的属性,LazyColumn不刷新

ionic - capacitor - Android 风味 - 无法在模拟器或真实设备中运行应用程序

如何在屏幕旋转或系统主题更改后将光标移动到 TextField 的末尾并保持键盘显示?

如何从日期 Select 器计算年龄?

有什么方法可以确定正在使用哪个 Android 手机的麦克风进行录音?

无法解析依赖项'com.github.smarteist:autoimageslider:1.4.0-appcompat'

Android Compose:LazyColumn 和 Column with verticalScroll 的区别

如何在stroke android drawable中设置渐变

使用 Jetpack Compose 的深层链接导航到可组合项