InflateException 和 ClassNotFoundException 导致应用程序崩溃

Posted

技术标签:

【中文标题】InflateException 和 ClassNotFoundException 导致应用程序崩溃【英文标题】:InflateException and ClassNotFoundException causing the app to crash 【发布时间】:2014-04-10 00:58:39 【问题描述】:

我正在创建一个应用程序,当用户选择检查时,会调用第二个活动。但是,当调用第二个活动时,我的应用程序崩溃并且日志 cat 在我的 paycheckactivity oncreate 方法上显示错误。

第一个活动

public class TableListActivity extends Activity 
private ListView mListView;
private TableListAdapter mAdapter;

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

    mListView = (ListView)findViewById(R.id.listView);
    mAdapter = new TableListAdapter(this, DataStore.CHECKS);
    mListView.setAdapter(mAdapter);
    mListView.setOnItemClickListener(new OnItemClickListener() 

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) 
            Intent intent = new Intent (TableListActivity.this, PayCheckActivity.class);
            intent.putExtra(PayCheckActivity.Extra_check, arg2);
            startActivity(intent);
        
    );


@Override
public boolean onCreateOptionsMenu(Menu menu) 
    getMenuInflater().inflate(R.menu.table_list, menu);
    return true;


@Override
public boolean onOptionsItemSelected(MenuItem item) 
        int id = item.getItemId();

        if (id == R.id.action_refresh) 
            Toast.makeText(this, "Refresh", Toast.LENGTH_SHORT).show();
        return true;
        
        return super.onOptionsItemSelected(item);

第二个活动

public class PayCheckActivity extends Activity 
    public static final String Extra_check= "abc";

    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.paycheck);
    

Logcat

03-06 21:51:11.275: D/dalvikvm(1108): GC_FOR_ALLOC freed 36K, 4% free 3135K/3244K, paused 50ms, total 50ms
03-06 21:51:11.285: I/dalvikvm-heap(1108): Grow heap (frag case) to 4.200MB for 1127536-byte allocation
03-06 21:51:11.325: D/dalvikvm(1108): GC_FOR_ALLOC freed 1K, 3% free 4234K/4348K, paused 36ms, total 36ms
03-06 21:51:11.695: D/(1108): HostConnection::get() New Host Connection established 0xb7fc9f38, tid 1108
03-06 21:51:11.775: W/EGL_emulation(1108): eglSurfaceAttrib not implemented
03-06 21:51:11.805: D/OpenGLRenderer(1108): Enabling debug mode 0
03-06 21:51:14.175: D/androidRuntime(1108): Shutting down VM
03-06 21:51:14.175: W/dalvikvm(1108): threadid=1: thread exiting with uncaught exception (group=0xb1a5fba8)
03-06 21:51:14.225: E/AndroidRuntime(1108): FATAL EXCEPTION: main
03-06 21:51:14.225: E/AndroidRuntime(1108): Process: edu.udallas.tableservice, PID: 1108
03-06 21:51:14.225: E/AndroidRuntime(1108): java.lang.RuntimeException: Unable to start activity ComponentInfoedu.udallas.tableservice/edu.tableservice.PayCheckActivity: android.view.InflateException: Binary XML file line #15: Error inflating class br
03-06 21:51:14.225: E/AndroidRuntime(1108):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-06 21:51:14.225: E/AndroidRuntime(1108):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-06 21:51:14.225: E/AndroidRuntime(1108):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-06 21:51:14.225: E/AndroidRuntime(1108):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-06 21:51:14.225: E/AndroidRuntime(1108):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-06 21:51:14.225: E/AndroidRuntime(1108):     at android.os.Looper.loop(Looper.java:136)
03-06 21:51:14.225: E/AndroidRuntime(1108):     at android.app.ActivityThread.main(ActivityThread.java:5017)
03-06 21:51:14.225: E/AndroidRuntime(1108):     at java.lang.reflect.Method.invokeNative(Native Method)
03-06 21:51:14.225: E/AndroidRuntime(1108):     at java.lang.reflect.Method.invoke(Method.java:515)
03-06 21:51:14.225: E/AndroidRuntime(1108):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-06 21:51:14.225: E/AndroidRuntime(1108):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-06 21:51:14.225: E/AndroidRuntime(1108):     at dalvik.system.NativeStart.main(Native Method)
03-06 21:51:14.225: E/AndroidRuntime(1108): Caused by: android.view.InflateException: Binary XML file line #15: Error inflating class br
03-06 21:51:14.225: E/AndroidRuntime(1108):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:707)
03-06 21:51:14.225: E/AndroidRuntime(1108):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
03-06 21:51:14.225: E/AndroidRuntime(1108):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
03-06 21:51:14.225: E/AndroidRuntime(1108):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
03-06 21:51:14.225: E/AndroidRuntime(1108):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
03-06 21:51:14.225: E/AndroidRuntime(1108):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
03-06 21:51:14.225: E/AndroidRuntime(1108):     at android.app.Activity.setContentView(Activity.java:1929)
03-06 21:51:14.225: E/AndroidRuntime(1108):     at edu.tableservice.PayCheckActivity.onCreate(PayCheckActivity.java:22)
03-06 21:51:14.225: E/AndroidRuntime(1108):     at android.app.Activity.performCreate(Activity.java:5231)
03-06 21:51:14.225: E/AndroidRuntime(1108):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-06 21:51:14.225: E/AndroidRuntime(1108):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-06 21:51:14.225: E/AndroidRuntime(1108):     ... 11 more
03-06 21:51:14.225: E/AndroidRuntime(1108): Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.br" on path: DexPathList[[zip file "/data/app/edu.udallas.tableservice-1.apk"],nativeLibraryDirectories=[/data/app-lib/edu.udallas.tableservice-1, /system/lib]]
03-06 21:51:14.225: E/AndroidRuntime(1108):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
03-06 21:51:14.225: E/AndroidRuntime(1108):     at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
03-06 21:51:14.225: E/AndroidRuntime(1108):     at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
03-06 21:51:14.225: E/AndroidRuntime(1108):     at android.view.LayoutInflater.createView(LayoutInflater.java:559)
03-06 21:51:14.225: E/AndroidRuntime(1108):     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:652)
03-06 21:51:14.225: E/AndroidRuntime(1108):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
03-06 21:51:14.225: E/AndroidRuntime(1108):     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
03-06 21:51:14.225: E/AndroidRuntime(1108):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
03-06 21:51:14.225: E/AndroidRuntime(1108):     ... 21 more
03-06 21:51:17.095: I/Process(1108): Sending signal. PID: 1108 SIG: 9
03-06 22:03:15.835: D/dalvikvm(1134): GC_FOR_ALLOC freed 39K, 4% free 3135K/3248K, paused 89ms, total 91ms
03-06 22:03:15.845: I/dalvikvm-heap(1134): Grow heap (frag case) to 4.200MB for 1127536-byte allocation
03-06 22:03:15.885: D/dalvikvm(1134): GC_FOR_ALLOC freed 2K, 3% free 4233K/4352K, paused 35ms, total 35ms
03-06 22:03:16.215: D/(1134): HostConnection::get() New Host Connection established 0xb7edd508, tid 1134
03-06 22:03:16.315: W/EGL_emulation(1134): eglSurfaceAttrib not implemented
03-06 22:03:16.335: D/OpenGLRenderer(1134): Enabling debug mode 0
03-06 22:03:17.795: D/AndroidRuntime(1134): Shutting down VM
03-06 22:03:17.795: W/dalvikvm(1134): threadid=1: thread exiting with uncaught exception (group=0xb1a5fba8)
03-06 22:03:17.845: E/AndroidRuntime(1134): FATAL EXCEPTION: main
03-06 22:03:17.845: E/AndroidRuntime(1134): Process: edu.udallas.tableservice, PID: 1134
03-06 22:03:17.845: E/AndroidRuntime(1134): java.lang.RuntimeException: Unable to start activity ComponentInfoedu.udallas.tableservice/edu.tableservice.PayCheckActivity: android.view.InflateException: Binary XML file line #15: Error inflating class br
03-06 22:03:17.845: E/AndroidRuntime(1134):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-06 22:03:17.845: E/AndroidRuntime(1134):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-06 22:03:17.845: E/AndroidRuntime(1134):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-06 22:03:17.845: E/AndroidRuntime(1134):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-06 22:03:17.845: E/AndroidRuntime(1134):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-06 22:03:17.845: E/AndroidRuntime(1134):     at android.os.Looper.loop(Looper.java:136)
03-06 22:03:17.845: E/AndroidRuntime(1134):     at android.app.ActivityThread.main(ActivityThread.java:5017)
03-06 22:03:17.845: E/AndroidRuntime(1134):     at java.lang.reflect.Method.invokeNative(Native Method)
03-06 22:03:17.845: E/AndroidRuntime(1134):     at java.lang.reflect.Method.invoke(Method.java:515)
03-06 22:03:17.845: E/AndroidRuntime(1134):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-06 22:03:17.845: E/AndroidRuntime(1134):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-06 22:03:17.845: E/AndroidRuntime(1134):     at dalvik.system.NativeStart.main(Native Method)
03-06 22:03:17.845: E/AndroidRuntime(1134): Caused by: android.view.InflateException: Binary XML file line #15: Error inflating class br
03-06 22:03:17.845: E/AndroidRuntime(1134):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:707)
03-06 22:03:17.845: E/AndroidRuntime(1134):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
03-06 22:03:17.845: E/AndroidRuntime(1134):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
03-06 22:03:17.845: E/AndroidRuntime(1134):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
03-06 22:03:17.845: E/AndroidRuntime(1134):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
03-06 22:03:17.845: E/AndroidRuntime(1134):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
03-06 22:03:17.845: E/AndroidRuntime(1134):     at android.app.Activity.setContentView(Activity.java:1929)
03-06 22:03:17.845: E/AndroidRuntime(1134):     at edu.tableservice.PayCheckActivity.onCreate(PayCheckActivity.java:22)
03-06 22:03:17.845: E/AndroidRuntime(1134):     at android.app.Activity.performCreate(Activity.java:5231)
03-06 22:03:17.845: E/AndroidRuntime(1134):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-06 22:03:17.845: E/AndroidRuntime(1134):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-06 22:03:17.845: E/AndroidRuntime(1134):     ... 11 more
03-06 22:03:17.845: E/AndroidRuntime(1134): Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.br" on path: DexPathList[[zip file "/data/app/edu.udallas.tableservice-1.apk"],nativeLibraryDirectories=[/data/app-lib/edu.udallas.tableservice-1, /system/lib]]
03-06 22:03:17.845: E/AndroidRuntime(1134):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
03-06 22:03:17.845: E/AndroidRuntime(1134):     at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
03-06 22:03:17.845: E/AndroidRuntime(1134):     at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
03-06 22:03:17.845: E/AndroidRuntime(1134):     at android.view.LayoutInflater.createView(LayoutInflater.java:559)
03-06 22:03:17.845: E/AndroidRuntime(1134):     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:652)
03-06 22:03:17.845: E/AndroidRuntime(1134):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
03-06 22:03:17.845: E/AndroidRuntime(1134):     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
03-06 22:03:17.845: E/AndroidRuntime(1134):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
03-06 22:03:17.845: E/AndroidRuntime(1134):     ... 21 more

【问题讨论】:

请发布您的 logcat 错误 目前我没有看到您的onCreate() sn-p 有任何问题,我想确认:您是否在清单上注册了活动?编辑:没关系,您的 XML 布局文件有问题。也发布您的paycheck.xml 布局。 是的。这是我的清单 activity_table_list.xml 是什么样的?任何 XML 错误? 请发布您的 xml 代码,因为它在您的 activity_table_list.xml 文件的第 15 行显示错误 【参考方案1】:

这里,你得到了两个Exceptions...

#android.view.InflateException :当您尝试 inflate 您的视图/XML 时,它会导致。由于您使用的是CustomAdapter,因此您在onCreateView() 方法中膨胀了列表项的XML,并且导致问题的XML...LogCat 直接将其指示为...

Caused by: android.view.InflateException: Binary XML file line #15: Error inflating class br

#java.lang.ClassNotFoundException :这是因为您试图将html 标签&lt;br&gt;&lt;/br&gt; 放入您的XML 中,而Android XML 不支持该标签。 LogCat 通过这条消息清楚地表明您正在使用 &lt;br&gt; 标记...

Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.br"

android 中没有名为 br 的视图...所以从 XML 中删除它...您的问题将得到解决。

【讨论】:

您的生活更安全!谢谢 很高兴收到您的来信……那么请接受答案。 不知道没有xml你是怎么知道这么多东西的。不错的作品。 :) +1 我有同样的问题,找不到对象,但是对象在正确的路径上..【参考方案2】:

在“application”标签下的AndroidManifest.xml文件中提及活动名称,如下所述:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.TableListActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
     <activity
        android:name="com.example.PayCheckActivity"
        android:label="@string/app_name" >
        </activity>

</application>

如果仍然显示错误,请告知错误。

【讨论】:

【参考方案3】:

看起来您已经复制粘贴了(我也是这样做的:))PayCheckActivity 的 onCreate,因此您在那里缺少 @Override 关键字。

这个 onCreate() 不再是 Activity 被覆盖的方法,因此是错误。

就这样吧,

public class PayCheckActivity extends Activity
    public static final String Extra_check= "abc";

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

希望这会有所帮助。 :)

【讨论】:

尝试完全覆盖它。删除当前的onCreate,然后执行。还要清理你的项目并运行。同时发布 PayCheckActivity 的布局文件。 既然onCreate()方法调用super.onCreate()那么它会自动继承超类的所有功能...所以,@Override注解不会做任何改变。

以上是关于InflateException 和 ClassNotFoundException 导致应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章

Androidx java.lang.ClassNotFoundException: 和 android.view.InflateException: 同时

InflateException 和 ClassNotFoundException 导致应用程序崩溃

如何处理 android.view.InflateException?当我尝试打开地图时出现此错误

InflateException - 浮动操作按钮

android.view.InflateException:总是错误

使用 PNG 图像的 SplashScreen 导致 Android.Views.InflateException 然后是 OutOfMemory