187

我有一个非常令人沮丧的错误,我无法解释。我创建了一个 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)

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

4

41 回答 41

145

经过长时间的调试,我已经解决了这个问题。(虽然我仍然无法解释为什么)。我将属性更改android:nameclass. (虽然在 Android Document 上,他们说这些属性是相同的,但它有效!!!)

所以,它应该从:

 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 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"
    class = "com.fragment.NavigationDrawerFragment" />

希望这有帮助:)

于 2013-11-13T11:16:23.310 回答
58

TL/DR :在创建从更高级别布局 XML 引用的片段期间发生异常。此异常导致高层布局膨胀失败,但未报告初始异常;只有更高级别的通货膨胀失败出现在堆栈跟踪中。要找到根本原因,您必须捕获并记录初始异常


错误的最初原因可能是多种多样的,这就是为什么这里有这么多不同的答案来解决每个人的问题。对于某些人来说,它与idclassname属性有关。对于其他人,这是由于权限问题或构建设置。对我来说,那些并没有解决问题。相反,有一个可绘制资源仅存在于 中drawable-ldrtl-xhdpi,而不是在像drawable.

但这些只是细节。总体问题是 logcat 中显示的错误消息没有描述启动它的异常。当更高级别的布局 XML 引用片段时,将onCreateView()调用片段的。当片段中发生异常时onCreateView()(例如,在扩充片段的布局 XML 时),它会导致更高级别的布局 XML 的扩充失败。这种更高级别的通货膨胀失败是错误日志中报告为异常的原因。但最初的例外似乎并没有很好地沿着链条向上传播,无法被报告。

鉴于这种情况,问题是如何公开初始异常,当它没有出现在错误日志中时。

解决方案非常简单:在片段的内容周围放置一个try/块,并在子句中记录异常:catchonCreateView()catch

public View onCreateView(LayoutInflater inflater, ViewGroup contnr, Bundle savedInstSt) {
    try {
        mContentView = inflater.inflate(R.layout.device_detail_frag, null);
        // ... rest of body of onCreateView() ...
    } catch (Exception e) {
        Log.e(TAG, "onCreateView", e);
        throw e;
    }
}

对哪个片段类执行此操作可能并不明显onCreateView(),在这种情况下,对导致问题的布局中使用的每个片段类执行此操作。例如,在 OP 的情况下,发生异常的应用程序代码是

at android.app.Activity.setContentView(Activity.java:1901)

这是

   setContentView(R.layout.activity_main);

因此,您需要onCreateView()在 layout 中引用的任何片段中捕获异常activity_main

就我而言,根本原因异常原来是

Caused by: android.content.res.Resources$NotFoundException: Resource
   "com.example.myapp:drawable/details_view" (7f02006f)  is not a
   Drawable (color or path): TypedValue{t=0x1/d=0x7f02006f a=-1
   r=0x7f02006f}

onCreateView()直到我将其捕获并明确记录后,此异常才会出现在错误日志中。一旦记录下来,问题就很容易诊断和修复(出于某种原因,details_view.xml仅存在于文件夹下)。ldrtl-xhdpi关键是捕获作为问题根源的异常,并将其暴露出来。

在所有片段的onCreateView()方法中将其作为样板进行这样做并没有什么坏处。如果那里有未捕获的异常,无论如何它都会使活动崩溃。唯一的区别是,如果您在 中捕获并记录异常onCreateView(),您将不会对它发生的原因一无所知。

PS我刚刚意识到这个答案与@DaveHubbard's有关,但使用不同的方法来查找根本原因(日志记录与调试器)。

于 2017-05-23T19:05:45.720 回答
35

我无法使用提供的答案解决我的问题。最后我改变了这个:

<fragment
android:id="@+id/fragment_food_image_gallery"
android:name="ir.smartrestaurant.ui.fragment.ImageGalleryFragment"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout="@layout/fragment_image_gallery"
tools:layout="@layout/fragment_image_gallery" />

对此:

<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="200dp" />

,

private void showGallery() {
    ImageGalleryFragment fragment = new ImageGalleryFragment()
    getSupportFragmentManager().beginTransaction()
                .replace(R.id.fragment_container, fragment)
                .commit();
    }

它有效。
如果您在片段中使用它,请使用getChildFragmentManager而不是getSupportFragmentManager

于 2016-02-16T15:18:49.943 回答
22

我有同样的问题,问题,尝试了这个线程中的所有答案都无济于事。我的解决方案是,我没有在 Activity XML 中添加 ID。我认为这无关紧要,但确实如此。

所以在我的活动 XML 中:

<fragment
    android:name="com.covle.hellocarson.SomeFragment"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

但应该有:

<fragment
    android:id="@+id/some_fragment"
    android:name="com.covle.hellocarson.SomeFragment"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

如果有人愿意评论为什么会这样,我会全神贯注于其他人,我希望这会有所帮助。

于 2015-08-04T10:54:48.170 回答
12

您可能不再需要它,但如果其他读者发现它有帮助。我有完全相同的android.view.InflateException:...Error inflating class fragment。我包含了所有正确的库。通过在文件中添加一个用户权限来解决,AndroidManifest.xml<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

顺便说一句,我正在跑步Android Studio 0.8.9 on Ubuntu 12.04.

于 2014-10-09T12:00:33.690 回答
12

我有同样的问题,因为我没有实现监听器。请参阅以下代码/*Add This!*/

public class SomeActivity extends AppCompatActivity
        implements BlankFragment.OnFragmentInteractionListener /*Add this!*/ 
{
    @Override                                                  /*Add This!*/
    public void onFragmentInteraction(Uri uri){                /*Add This!*/
    }                                                          /*Add This!*/
}

仅供参考,我的片段类如下所示:

public class SomeFragment extends Fragment {

    private OnFragmentInteractionListener mListener;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnFragmentInteractionListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    public interface OnFragmentInteractionListener {
        public void onFragmentInteraction(Uri uri);
    }
}

编辑:

当. onCreate_ Fragment我有以下内容:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container, false);

    int ID = getArguments().getInt("val");

    return rootView;
} 

因为我重用了这个片段,所以我完全忘记了设置参数。那么结果getArguments()就是null。显然,我null在这里得到一个指针异常。我会建议你留意这样的错误。

于 2015-08-22T15:29:03.280 回答
8

您的 NavigationDrawerFragment 是否扩展了 android.support.v4.app.Fragment?换句话说,您是否导入了正确的包?

import android.support.v4.app.Fragment;
于 2013-11-09T10:40:51.087 回答
6

我也有这个问题。我通过替换导入解决了MainActivityNavigationDrawerFragment

import android.app.Activity;
import android.app.ActionBar;

import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;

我更新MainActivity为扩展ActionBarActivity而不是活动

public class MainActivity extends ActionBarActivity implements NavigationDrawerFragment.NavigationDrawerCallbacks

也用于ActionBar actionBar = getSupportActionBar();获取 ActionBar

我更新了以下功能NavigationDrawerFragment

private ActionBar getActionBar()
{
    return ((ActionBarActivity)getActivity()).getSupportActionBar();
}
于 2014-11-04T15:17:45.623 回答
6

我遇到了这个问题并通过使用以下代码解决了它。我正在使用子片段管理器开始片段事务。

布局:

                  <fragment
                    class="com.google.android.youtube.player.YouTubePlayerSupportFragment"
                    android:id="@+id/youtube_fragment"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>

这就是我开始片段交易的方式:

   youTubePlayerFragment = (YouTubePlayerSupportFragment) getChildFragmentManager().findFragmentById(R.id.youtube_fragment);

以下代码解释了我如何删除使用 childfragmentmanger 添加的片段。

@Override
    public void onDestroyView() {
        super.onDestroyView();

        youTubePlayerFragment = (YouTubePlayerSupportFragment) getChildFragmentManager().findFragmentById(R.id.youtube_fragment);

        if (youTubePlayerFragment != null)
        {
            getChildFragmentManager().beginTransaction().remove(youTubePlayerFragment).commitAllowingStateLoss();
        }

        youTubePlayer = null;
    }
于 2016-12-08T09:13:50.263 回答
4

我断断续续地遇到过类似的问题。无论实际原因如何,错误消息通常提供的细节很少。但我找到了一种获取更多有用信息的方法。事实证明,内部 android 类“LayoutInflater.java”(在 android.view 包中)有一个“inflate”方法,该方法会重新引发异常,但不会获取详细信息,因此您会丢失有关原因的信息。

我使用了 AndroidStudio,并在 LayoutInflator 第 539 行(在我正在使用的版本中)设置了一个断点,这是该“inflate”方法中通用异常的 catch 块的第一行:

        } catch (Exception e) {
            InflateException ex = new InflateException(
                    parser.getPositionDescription()
                            + ": " + e.getMessage());
            ex.initCause(e);
            throw ex;

如果您在调试器中查看“e”,您将看到一个“原因”字段。它可以为您提供有关真实情况的提示非常有帮助。例如,我发现包含片段的父级必须有一个 id,即使在您的代码中没有使用也是如此。或者 TextView 的尺寸有问题。

于 2016-10-18T16:00:09.213 回答
3

以防万一有人需要这个。假设:设备电话已连接到 USB 电缆并且您的 IDE 读取以启动应用程序。转到命令提示符以确定问题:输入 adb logcat

然后从 IDE 启动您的应用程序。你会例外。

就我而言:我正在将 2.3 版的 Android 应用程序部署到不支持小部件“Space”的移动设备上

于 2015-03-10T18:51:36.640 回答
3

当您有一个自定义类扩展了不同的类(在本例中为视图)并且未导入该类所需的所有构造函数时,就会出现此问题。

例如:public class CustomTextView extends TextView{}

这个类将有 4 个构造函数,如果你错过任何一个,它就会崩溃。事实上,我错过了 Lollipop 使用的最后一个,添加了该构造函数并且工作正常。

于 2016-03-15T07:52:27.847 回答
3

在导航中添加此名称字段

android:name="androidx.navigation.fragment.NavHostFragment"

   <fragment
             android:id="@+id/container"
             android:layout_width="match_parent"
             android:layout_height="0dp"
             android:name="androidx.navigation.fragment.NavHostFragment"
             app:navGraph="@navigation/navigation"
             app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
             app:layout_constraintTop_toTopOf="parent">
于 2019-11-15T05:14:08.683 回答
2
android.view.InflateException: Binary XML file line #16: Error inflating class com.google.android.material.bottomappbar.BottomAppBar

视图可以是任何无法膨胀的东西,当在解析 XML 文件中引用的视图的类名或名称属性时发生冲突时,就会出现这种错误。

当我遇到同样的错误时,我只是在 UI-XML 文件中得到了一切干净和安全的东西,我正在使用的视图,

   <com.google.android.material.bottomappbar.BottomAppBar
    android:id="@+id/bottomAppBar"
    style="@style/Widget.MaterialComponents.BottomAppBar.Colored"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    app:hideOnScroll="true"
    app:menu="@menu/bottom_app_bar"
    app:navigationIcon="@drawable/ic__menu_24"/>

我使用的是引用 Material components 属性的样式属性。但是我的styles.xml 有...

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
....
</style>

类解决面临冲突的地方。我的视图属性引用了一个未在我的应用主题中定义的属性。材料组件的正确父主题帮助了我。所以我将父属性更改为...

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
...
</style>

这解决了这个问题。

于 2020-06-22T14:20:14.290 回答
2

在此处没有任何答案帮助我之后,我选择在调试模式下运行应用程序,在我的片段中的每一行 onCreateView 移动(在您的情况下为 NavigationDrawerFragment)。并注意到由于 NullPointerException,该片段难以膨胀。例如

mySeekBar = (SeekBar) getActivity().findViewById(R.id.mySeekBar);
mySeekBar.setOnSeekBarChangeListener(this);

这里 mySeekBar 设置为 null(因为我错过了在适当的布局中添加控件),下一行进入 NPE,它作为 InflateException 出现。另外,如上所述,将 android:name 重命名为 class。

由于上述各种原因,可能会出现此问题。我会推荐逐行调试流程以了解问题所在。

于 2017-02-24T07:19:27.517 回答
2

经过长时间的尝试,这就是我在上述答案都无法解决的问题后解决问题的方法。

  1. 扩展AppCompatActivity您的Main活动而不是Activity.
  2. 添加android:theme="@style/Theme.AppCompat.Light"到您<Activity..../>AndroidManifest.xml
  3. 在您的NavigationDrawerFragment班级中,将您的ActionBar实例更改为

    ActionBar mActionBar=((AppCompatActivity)getActivity()).getSupportActionBar();
    

编辑

它应该是ActivityLayout之间的一致性。如果LayoutAppCompat Theme之一,例如Theme.AppCompat.Light,您的Activity应该extends AppCompatActivity

我想要一个汉堡图标和一个看起来像Android Gmail 应用程序的Navigation Drawer,但我最终得到了一个丑陋的Navigation Drawer。这一切都是因为我所有的课程而不是.extends ActivityAppCompatActivity

我将整个项目重新分解为 extend AppCompatActivity,然后右键单击Layout Folder,选择new-> ActivitythenNavigation Drawer Activity和 Boom,一切都为我完成了!

于 2018-02-18T19:03:19.857 回答
2

我们还必须在 build.gradle(app) 中添加以下内容

compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'

每当我们使用新布局或新设计功能时。希望这可以帮助你。

于 2016-06-24T05:52:31.870 回答
2

如上一篇文章所述,
重命名

android:name="com.fragment.NavigationDrawerFragment"

class = "com.fragment.NavigationDrawerFragment"

不过,它对我不起作用。然后我只使用了没有 com.fragment 部分的类名,瞧,它起作用了。所以最后改成

class = "NavigationDrawerFragment"
于 2017-01-06T22:44:53.290 回答
1

对于一些仍然没有找到解决方案的人来说,在我的情况下它正在发生,因为我遇到了 OOM(内存不足)问题。例如,当您的应用程序在长时间使用时出现内存泄漏时,就会发生这种情况。在我的堆栈跟踪中,这是主要原因。

于 2015-12-08T15:58:36.620 回答
1

我在 4.4.2 设备上有这个,但 5+ 很好。原因:在自定义视图的初始化中,我正在创建一个TextView(Context context, AttributeSet attrs, @AttrRes int defStyleAttr, @StyleRes int defStyleRes)API 21+。

Android Studio 2.1 不会抱怨它,即使它被注释了TargetApi(21)。显然,Android Studio 2.2 将更正此问题并将其正确显示为错误。

希望这可以帮助某人。

于 2016-06-07T02:23:46.587 回答
1

我参加聚会有点晚了,但这些答案都没有帮助我。我在我的片段中都使用 Google 地图作为SupportMapFragmentPlaceAutocompleteFragment 。正如所有答案都指出问题在于 SupportMapFragment 是要重新创建和重绘的地图。

但我也遇到了 PlaceAutocompleteFragment 的问题。因此,对于那些因SupportMapFragmentSupportMapFragment而面临此问题的人来说,这是可行的解决方案

 mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.mapFragment);
    FragmentManager fm = getChildFragmentManager();

    if (mapFragment == null) {
        mapFragment = SupportMapFragment.newInstance();
        fm.beginTransaction().replace(R.id.mapFragment, mapFragment).commit();
        fm.executePendingTransactions();
    }

    mapFragment.getMapAsync(this);

    //Global PlaceAutocompleteFragment autocompleteFragment;


    if (autocompleteFragment == null) {
        autocompleteFragment = (PlaceAutocompleteFragment) getActivity().getFragmentManager().findFragmentById(R.id.place_autoCompleteFragment);

    }

并在 onDestroyView 中清除 SupportMapFragment 和 SupportMapFragment

@Override
public void onDestroyView() {
    super.onDestroyView();


    if (getActivity() != null) {
        Log.e("res","place dlted");
        android.app.FragmentManager fragmentManager = getActivity().getFragmentManager();
        android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.remove(autocompleteFragment);
        fragmentTransaction.commit();

        autocompleteFragment = null;
    }


}
于 2018-01-19T20:44:13.280 回答
1

我不知道这是否会有所帮助。

我在尝试膨胀的布局中的 TextView 遇到了这个问题(android.view.InflateException: Binary XML file line #45: Error inflating class TextView)。

我设置了以下 XML 属性android:textSize="?android:attr/textAppearanceLarge"不允许布局膨胀。

不知道为什么,(我对 Android 还是有点陌生​​ - 不到一年的经验),可能与调用系统属性有关,idk,我所知道的只是我使用普通的旧@ dimen/md_text_16sp这是我的习惯),问题解决了:)

希望这可以帮助...

于 2016-01-19T17:48:57.710 回答
1

在我的特殊情况下,问题是我将此行添加到 TextView :

android:background="?attr/selectableItemBackground"

删除它后,一切都开始正常工作。

于 2020-06-03T19:07:31.677 回答
0

在我的例子中,我使用的是 Hilt Dagger Library,在我的 ViewModel 类中,我没有在类的顶部提到 @HiltViewModel 注释。

因此,只需添加该注释即可解决问题。

于 2021-09-27T01:57:30.803 回答
0

当我将 RecyclerView 放在 fragment.xml 的根目录时,我遇到了这个错误,

但是当我用 ViewGroup 包装它时,该应用程序运行良好。

于 2018-10-21T20:38:48.333 回答
0

FWIW:我收到“Android.Views.InflateException Message=Binary XML file line #1: Binary XML file line #1: Error inflating class android.view.TextureView”

我是 android 表单设计的新手。在尝试在某些按钮上设置边框时,我发现了一个 SO 帖子,它启发我创建了 buttonborder.xml,如下所示:

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="@android:color/transparent" />
<stroke android:width="1dp" android:color="@android:color/white"/>

然后对于我添加的每个按钮

android:background="@drawable/buttonborder"

然而,在我的热情中,我还将它添加到 TextureView - 这是我的问题。当我从 TextureView 中删除该行时,一切正常。

于 2020-01-23T17:20:30.163 回答
0

这个错误真的很令人困惑。在我的情况下,我的片段缺少一个默认的空参数少构造函数。在我的片段中添加空构造函数后,问题解决了。

于 2020-04-17T13:52:30.650 回答
0

我的错误是由另一个问题引起的。

我正在将一个包从一个 Activity 传递到它的片段。当我评论在片段中接收包的代码时,错误消失了。事实证明,我的错误是由于下面的“getArguments();” 返回 null 的部分。

Bundle args = getArguments();

检查活动发送代码后,我意识到我在下面犯了一个愚蠢的错误;

Bundle bundle =new Bundle();
 bundle.putInt("recipeID", recipe_position);
 Fragment mainFragment = new MainActivityFragment();
 mainFragment.setArguments(bundle);
 FragmentManager fragmentManager = getSupportFragmentManager();
 --> fragmentManager.beginTransaction()
                        .replace(R.id.container, new  MainActivityFragment(),
                                DetailRecipeActivityFragment.TAG)
                        .commit();

我正在用箭头创建一个新片段。而我应该使用已经拥有我的捆绑包的预实例化片段。所以它应该是:

Bundle bundle =new Bundle();
 bundle.putInt("recipeID", recipe_position);
 Fragment mainFragment = new MainActivityFragment();
 mainFragment.setArguments(bundle);
 Fragment mainFragment = new MainActivityFragment();
 FragmentManager fragmentManager = getSupportFragmentManager();
 -->fragmentManager.beginTransaction()
                        .replace(R.id.container, mainFragment,
                                DetailRecipeActivityFragment.TAG)
                        .commit();

我不知道为什么它会抛出这个错误而不是 NPE,但这解决了我的错误,以防有人遇到相同的情况

于 2017-07-22T20:11:41.783 回答
0

android:name 和 class: 可以互换吗?

大概,是的。我只使用了类,这似乎是谷歌的大多数示例使用的,但我确实看到他们在某些示例中使用 android:name 的位置。不幸的是,片段没有正式和完整的文档。

利用

class = "com.fragment.NavigationDrawerFragment"

安装的

android:name="com.fragment.NavigationDrawerFragment"

这似乎意味着程序首先看起来“类”属性。并且在失败时看起来“名称”属性。因此,如果更有效的话,使用“类”是真的

于 2020-01-15T05:14:44.400 回答
0

我遇到了同样的问题,在我的情况下,包名称错误,修复它解决了问题。

于 2018-01-19T16:12:21.320 回答
0

如果其他人来到这里并且答案无助于解决问题,请尝试另一件事。

正如其他人所提到的,这通常是由嵌套在 XML 本身中的问题引起的,而不是您在 Java 中做错的事情。就我而言,这是一个非常容易(而且愚蠢)的错误来解决。

我有这样的代码:

<view
    android:layout_width="fill_parent"
    android:layout_height="1dip"
    android:id="@+id/view44"
    android:background="@color/gray"
    />

当我所要做的就是将“视图”中的 v 大写以便系统识别它时。检查您的自定义视图(或 Fragments、recyclerviews 等)是否都预先具有正确的大写声明,以便 XML 自动完成将其与适当的视图相匹配。

于 2015-11-30T22:39:17.117 回答
0

打开 gradle.properties 并添加以下行:

android.enableAapt2=false

参考:https ://github.com/chrisjenx/Calligraphy/issues/417#issuecomment-365177808

于 2018-03-12T02:51:42.913 回答
0

就我而言。

我试图膨胀的布局有

<include 
 layout  = "...."
/>

标签,删除它修复它。

我试图将以前为 Activity 设计的布局膨胀到视图寻呼机适配器中。

于 2017-06-30T09:58:08.697 回答
0

当管理您的 Fragment 的活动停止并且您尝试再次打开它时,可能会发生此异常,因此在这种情况下,NavHostFragment 将不会被丢失状态的活动重新调整。要修复它,请尝试完成活动的循环寿命,以便再次由 FragmentManager 重新加载 NavHostFragment。

在您管理片段的活动中放置以下内容:

@Override
protected void onStop() {
    super.onStop();
    this.finishActivity(0);
}
于 2020-05-20T00:02:40.493 回答
0

检查 Proguard

我将这个错误与ClassNotFoundException我的一个课程结合在一起,最后只需要检查我的proguard-rules.pro. 该文件app位于您YourAppName目录中的目录下。我需要附加以下两行:

-keep class com.gbros.tabslite.data.** { *; }
-keepattributes Exceptions, Signature, InnerClasses 

我的应用程序名称在哪里tabslite,我得到的文件ClassNotFoundExceptiondata文件夹中。

于 2020-03-25T02:51:22.380 回答
0

我也有这个错误,经过很长时间的调试,问题似乎是我的 MainClass 扩展了 Activity 而不是 FrameActivity,在我的情况下,xml 不是问题。希望能帮到你。

于 2016-09-05T15:37:53.063 回答
0

我不知道发生了什么,但是经过数小时的努力,将 Fragment 更改为 FrameLayout 解决了我的问题。

<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
于 2021-04-05T12:07:18.053 回答
0

我认为基本问题在于 AndroidManifest.xml 中定义的“android:targetSdkVersion”。就我而言,我定义为的初始值:

android:targetSdkVersion=16

我将其更改为:

android:targetSdkVersion=22

这解决了我的所有错误。因此,在构建 Android 应用程序之前,设置正确的“targetSdkVersion”也很重要。

于 2015-09-16T10:23:55.257 回答
0

经过 2 小时的努力,我发现我的一个可绘制文件是 v24,但我正在 Api 23 设备上测试应用程序。要解决该崩溃请按照下列步骤操作:

  1. 确保您的文件位于 res>drawable 文件夹中。而不是 res>drawable-v24。
  2. 有时你的 android studio 没有显示 drawable-24 文件夹,而你的文件在 res>drawable 文件夹中带有 v24 标签。
  3. 删除该文件并重新添加它。
于 2022-02-11T12:26:16.237 回答
0

对我来说,我复制粘贴了一个活动布局,但我没有更改其中的值

<androidx.constraintlayout.widget.ConstraintLayout>
...
tools:context=""  <!-- Check this value -->
</androidx.constraintlayout.widget.ConstraintLayout>

用新的活动名称更新它后,它对我有用。

于 2021-04-03T16:29:40.137 回答
0

我也遇到了这个错误。

尝试捕获引发错误的方法,我找到了解决我的具体问题的方法。

就我而言,我使用了两个标准主题:明暗。

因此,我必须(在必要时)以这种方式将颜色资源引用应用于颜色、可绘制对象和视图:

android:color="?accentColor"

以便系统根据当前主题知道使用哪种颜色。

在我的 DrawerLayout 中,我有一个 ListView 项目(菜单)的自定义选择器:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <solid android:color="?colorAccent" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <solid android:color="?colorAccent" />
        </shape>
    </item>
</selector>

到目前为止,较低的 API 已经很好地解析了这些颜色引用。但是,在这个选择器中,它不起作用。

为每个主题制作一个选择器,将其中的任何一个更改?colorAccentt1colorAccentt2colorAccent分别解决了我的问题。

于 2021-03-11T14:13:14.443 回答