android.view.InflateException:二进制XML文件:膨胀类片段时出错
我有一个非常令人沮丧的错误,我无法解释。我创建了一个Android应用程序,它使用android.view.InflateException:二进制XML文件:膨胀类片段时出错,android,Android,我有一个非常令人沮丧的错误,我无法解释。我创建了一个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:
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>
这里的主要问题是:上述代码在几乎所有设备(模拟设备或一些真实设备)上都能顺利运行。但当我在三星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)
请告诉我如何修复错误,谢谢:)您的NavigationDrawerFragment是否扩展了android.support.v4.app.Fragment?换句话说,您是否导入了正确的包
import android.support.v4.app.Fragment;
经过长时间的调试,我已经解决了这个问题。(尽管我仍然无法解释原因)。我将属性
android:name
更改为class
。(虽然在Android文档中,他们说这些属性是相同的,但它是有效的!!!)
因此,它应该从:
android:name="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" />
希望这有帮助:)你可能不再需要它了,但如果更多的读者发现它有帮助的话。我有完全相同的
android.view.InflateException:…膨胀类片段时出错
。我有所有合适的图书馆。通过在AndroidManifest.xml
文件中再添加一个用户权限来解决,即
顺便说一句,我在Ubuntu 12.04上运行的是Android Studio 0.8.9。我也遇到了这个问题。我通过在
MainActivity
和NavigationDrawerFragment
private ActionBar getActionBar()
{
return ((ActionBarActivity)getActivity()).getSupportActionBar();
}
从
到
我更新了MainActivity
以扩展ActionBarActivity
而不是Activity
public class MainActivity extends ActionBarActivity implements NavigationDrawerFragment.NavigationDrawerCallbacks
还可以使用ActionBar ActionBar=getSupportActionBar()代码>以获取操作栏
我在NavigationDrawerFragment
private ActionBar getActionBar()
{
return ((ActionBarActivity)getActivity()).getSupportActionBar();
}
以防万一有人需要这个。
假设:连接到USB电缆的设备电话和要启动的IDE读数
应用程序。
转到命令提示符以确定问题:
输入adb logcat
然后从IDE启动应用程序。
你会成为一个例外
在我的例子中:我在移动设备上部署了一个2.3版的Android应用程序
这不支持小部件“Space”我遇到了同样的问题,问题,在这个线程中尝试了所有答案,但都没有用。我的解决方案是,我没有在活动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"/>
如果有人愿意评论这是为什么,我洗耳恭听,我希望这会有所帮助。我也有同样的问题,因为我没有实现侦听器。使用/*查看以下代码添加此项*/代码>
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
函数出现异常时,我也注意到了同样的错误消息。我有以下几点:
@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
指针异常。我建议你也注意这样的错误 我认为基本问题在于AndroidManifest.xml中定义的“android:targetSdkVersion”。在我的情况下,我定义的初始值为:
android:targetSdkVersion=16
我把它改成:
android:targetSdkVersion=22
这解决了我所有的错误。因此,在构建android应用程序之前,设置正确的“targetSdkVersion”也很重要。如果其他人来这里,而答案无助于解决问题,还有一件事要尝试
正如其他人提到的,这通常是由嵌套在XML本身中的问题引起的,而不是您在Java中所做的错误。就我而言,这是一个非常容易(而且愚蠢)纠正的错误
我有这样的代码:
<view
android:layout_width="fill_parent"
android:layout_height="1dip"
android:id="@+id/view44"
android:background="@color/gray"
/>
当我所要做的就是在“视图”中大写v,以便系统对其进行重新设定。检查您的自定义视图(或片段、recyclerviews等)是否预先都有正确的大写声明,以便XML自动完成将其与适当的视图相匹配。对于一些仍然没有找到解决方案的人,在我的例子中,这是因为我遇到了OOM(内存不足)问题。例如,长时间使用应用程序时,如果应用程序内存泄漏,就会发生这种情况。在我的堆栈跟踪中,这是主要原因。我不知道这是否有帮助
我在试图膨胀的布局中的TextView出现了这个问题(android.view.InflateException:Binary XML文件行#45:膨胀类TextView时出错)
我已经设置了以下XML属性android:textSize=“?android:attr/textpearancelarge”,这不允许布局膨胀
不知道确切的原因,(我对Android还是有点陌生-不到一年的经验),可能与调用系统属性有关,idk,我只知道只要我使用纯旧的@dimen/md\u text\u 16sp(这是我的习惯),问题就解决了:)
希望这有帮助…我无法使用提供的答案解决我的问题。最后我改变了这一点:
<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" />
而且很有效。
如果您在fragment内部使用它,请使用getChildFragmentManager而不是getSupportFragmentManager当您有一个扩展不同类(在本例中是视图)的自定义类,并且没有导入该类所需的所有构造函数时,就会出现此问题
例如:公共类CustomTextView扩展了TextView{}
这个类将有4个构造函数,如果错过任何一个,它将崩溃。为了m
@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;
}
<view
android:layout_width="fill_parent"
android:layout_height="1dip"
android:id="@+id/view44"
android:background="@color/gray"
/>
<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();
}
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
} catch (Exception e) {
InflateException ex = new InflateException(
parser.getPositionDescription()
+ ": " + e.getMessage());
ex.initCause(e);
throw ex;
<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);
@Override
public void onDestroyView() {
super.onDestroyView();
youTubePlayerFragment = (YouTubePlayerSupportFragment) getChildFragmentManager().findFragmentById(R.id.youtube_fragment);
if (youTubePlayerFragment != null)
{
getChildFragmentManager().beginTransaction().remove(youTubePlayerFragment).commitAllowingStateLoss();
}
youTubePlayer = null;
}
android:name="com.fragment.NavigationDrawerFragment"
class = "com.fragment.NavigationDrawerFragment"
class = "NavigationDrawerFragment"
mySeekBar = (SeekBar) getActivity().findViewById(R.id.mySeekBar);
mySeekBar.setOnSeekBarChangeListener(this);
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;
}
}
at android.app.Activity.setContentView(Activity.java:1901)
setContentView(R.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}
<include
layout = "...."
/>
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();
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);
}
@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;
}
}
ActionBar mActionBar=((AppCompatActivity)getActivity()).getSupportActionBar();
android.enableAapt2=false
<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">
android.view.InflateException: Binary XML file line #16: Error inflating class com.google.android.material.bottomappbar.BottomAppBar
<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"/>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
....
</style>
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
...
</style>