第 7 行的 InflateException 二进制 XML 文件:膨胀类片段时出错
Posted
技术标签:
【中文标题】第 7 行的 InflateException 二进制 XML 文件:膨胀类片段时出错【英文标题】:InflateException Binary XML file at line #7: Error inflating class fragment 【发布时间】:2014-07-05 17:37:33 【问题描述】:我一直在对这个问题进行一些研究,但还没有适合我的解决方案。我在片段类中添加了空构造函数并尝试了不同的片段导入,但似乎没有任何效果。希望大家帮帮我!
我的活动:
package com.example.com.example.android.rssfeed;
import android.app.Activity;
import android.os.Bundle;
public class RssfeedActivity extends Activity implements MyListFragment.OnItemSelectedListener
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rssfeed);
public void onRssItemSelected(String link)
DetailFragment fragment = (DetailFragment) getFragmentManager().findFragmentById(R.id.detailFragment);
if (fragment != null && fragment.isInLayout())
fragment.setText(link);
//end if
//end method onRssItemSelected
//end class
我的片段:
package com.example.com.example.android.rssfeed;
import android.app.Activity;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class MyListFragment extends Fragment
public MyListFragment()
private OnItemSelectedListener listener;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
View view = inflater.inflate(R.layout.fragment_rsslist_overview, container, false);
Button button = (Button) view.findViewById(R.id.btn1);
button.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
updateDetail();
//end onClick
);
return view;
//end onCreateView
public interface OnItemSelectedListener
public void onRssItemSelected(String link);
//end onItemSelectedListener
@Override
public void onAttach(Activity activity)
super.onAttach(activity);
if (activity instanceof OnItemSelectedListener)
listener = (OnItemSelectedListener) activity;
else
throw new ClassCastException(activity.toString()
+ "Must implement MyListFragment.OnItemSelectedListener");
//end else
//end onAttach
//May also be triggered from the activity
public void updateDetail()
//Create fake data
String newTime = String.valueOf(System.currentTimeMillis());
// Send data to Activity
listener.onRssItemSelected(newTime);
//end method updateDetail
//end class
我的片段 XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:baselineAligned="false"
android:layout_
android:layout_
android:orientation="horizontal" >
<fragment
android:id="@+id/listFragment"
android:layout_
android:layout_weight="1"
android:layout_
class="com.example.android.rssfeed.MyListFragment"
android:layout_marginTop="?android:attr/actionBarSize"></fragment>
<fragment
android:id="@+id/detailFragment"
android:layout_
android:layout_weight="2"
android:layout_
class="com.example.android.rssfeed.DetailFragment"></fragment>
</LinearLayout>
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.com.example.android.rssfeed"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.com.example.android.rssfeed.RssfeedActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
按钮 XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:orientation="vertical" >
<button
android:id="@+id/btn1"
android:layout_
android:layout_
android:text="Press to Update">
</button>
</LinearLayout>
LogCat 错误:
05-17 10:35:01.864: E/AndroidRuntime(6384): FATAL EXCEPTION: main
05-17 10:35:01.864: E/AndroidRuntime(6384): java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.com.example.android.rssfeed/com.example.com.example.android.rssfeed.RssfeedActivity: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.ActivityThread.access$700(ActivityThread.java:150)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.os.Handler.dispatchMessage(Handler.java:99)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.os.Looper.loop(Looper.java:137)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.ActivityThread.main(ActivityThread.java:5279)
05-17 10:35:01.864: E/AndroidRuntime(6384): at java.lang.reflect.Method.invokeNative(Native Method)
05-17 10:35:01.864: E/AndroidRuntime(6384): at java.lang.reflect.Method.invoke(Method.java:511)
05-17 10:35:01.864: E/AndroidRuntime(6384): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
05-17 10:35:01.864: E/AndroidRuntime(6384): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
05-17 10:35:01.864: E/AndroidRuntime(6384): at dalvik.system.NativeStart.main(Native Method)
05-17 10:35:01.864: E/AndroidRuntime(6384): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
05-17 10:35:01.864: E/AndroidRuntime(6384): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:342)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.Activity.setContentView(Activity.java:1928)
05-17 10:35:01.864: E/AndroidRuntime(6384): at com.example.com.example.android.rssfeed.RssfeedActivity.onCreate(RssfeedActivity.java:11)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.Activity.performCreate(Activity.java:5267)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
05-17 10:35:01.864: E/AndroidRuntime(6384): ... 11 more
05-17 10:35:01.864: E/AndroidRuntime(6384): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.android.rssfeed.MyListFragment: make sure class name exists, is public, and has an empty constructor that is public
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.Fragment.instantiate(Fragment.java:592)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.Fragment.instantiate(Fragment.java:560)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.Activity.onCreateView(Activity.java:4862)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:686)
05-17 10:35:01.864: E/AndroidRuntime(6384): ... 21 more
05-17 10:35:01.864: E/AndroidRuntime(6384): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.android.rssfeed.MyListFragment" on path: /data/app/com.example.com.example.android.rssfeed-1.apk
05-17 10:35:01.864: E/AndroidRuntime(6384): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
05-17 10:35:01.864: E/AndroidRuntime(6384): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
05-17 10:35:01.864: E/AndroidRuntime(6384): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
05-17 10:35:01.864: E/AndroidRuntime(6384): at android.app.Fragment.instantiate(Fragment.java:582)
05-17 10:35:01.864: E/AndroidRuntime(6384): ... 24 more
【问题讨论】:
【参考方案1】:原因:java.lang.ClassNotFoundException:找不到类 路径上的“com.example.android.rssfeed.MyListFragment”: /data/app/com.example.com.example.android.rssfeed-1.apk
改变这个
class="com.example.android.rssfeed.MyListFragment"
到
class="com.example.com.example.android.rssfeed.MyListFragment"
因为包名是package com.example.com.example.android.rssfeed
。
同样
class="com.example.com.example.android.rssfeed.DetailFragment"
如果包名相同
改变这个
<button
android:id="@+id/btn1"
android:layout_
android:layout_
android:text="Press to Update">
到
<Button
android:id="@+id/btn1"
android:layout_
android:layout_
android:text="Press to Update" />
【讨论】:
啊,谢谢你指出这一点,就在我的头上,哈哈。现在它给了我:错误膨胀类按钮,而不是。我还需要向它添加一个类包吗? @user3572555 不,你没有 啊好吧,我把按钮的xml添加到了粘贴中。 @user3572555 它的<Button
不是 <button
。已编辑
啊!如此愚蠢的菜鸟错误,我想我现在需要我的咖啡了。感谢您的帮助!【参考方案2】:
改变
"class= com.example.android.rssfeed.MyListFragment"
到
"com.example.com.example.android.rssfeed.MyListFragment"
And your problem will be solved out
【讨论】:
【参考方案3】:在片段中添加这一行 class="com.google.android.gms.maps.SupportMapFragment"
【讨论】:
以上是关于第 7 行的 InflateException 二进制 XML 文件:膨胀类片段时出错的主要内容,如果未能解决你的问题,请参考以下文章
Android 7.0 上的 Android WebView InflateException
android.view.InflateException: Binary XML file line #7: Error inflating class
android.view.InflateException: Binary XML file line #7: Error inflating class Toolbar
android.view.InflateException 膨胀类片段时出错,二进制 XML 文件第 1 行?
Android.view.InflateException:二进制 XML 文件第 25 行:膨胀类 ImageButton、Resource NotFoundException、XmlPullPar
android.view.InflateException:二进制 XML 文件第 15 行:二进制 XML 文件第 19 行:膨胀类片段时出错