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

Posted

技术标签:

【中文标题】Androidx java.lang.ClassNotFoundException: 和 android.view.InflateException: 同时【英文标题】:Androidx java.lang.ClassNotFoundException: and android.view.InflateException: at the same time 【发布时间】:2020-01-26 01:38:02 【问题描述】:

迁移到 androidx 库后,我在 Google Play 开发控制台中收到此错误。

来自 Google Play 的崩溃报告:

java.lang.RuntimeException: 
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3322)
  at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3418)
  at android.app.ActivityThread.handleRelaunchActivity (ActivityThread.java:5481)
  at android.app.ActivityThread.access$1200 (ActivityThread.java:231)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1829)
  at android.os.Handler.dispatchMessage (Handler.java:102)
  at android.os.Looper.loop (Looper.java:148)
  at android.app.ActivityThread.main (ActivityThread.java:7422)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1230)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1120)
Caused by: android.view.InflateException: 
  at android.view.LayoutInflater.inflate (LayoutInflater.java:551)
  at android.view.LayoutInflater.inflate (LayoutInflater.java:429)
  at android.view.LayoutInflater.inflate (LayoutInflater.java:380)
  at androidx.appcompat.app.AppCompatDelegateImpl.setContentView (AppCompatDelegateImpl.java:469)
  at androidx.appcompat.app.AppCompatActivity.setContentView (AppCompatActivity.java:140)
  at x.y.z.LoginActivity.onCreate (LoginActivity.java:24)
  at android.app.Activity.performCreate (Activity.java:6904)
  at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1136)
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3269)
Caused by: android.view.InflateException: 
  at android.view.LayoutInflater.createViewFromTag (LayoutInflater.java:788)
  at android.view.LayoutInflater.parseInclude (LayoutInflater.java:966)
  at android.view.LayoutInflater.rInflate (LayoutInflater.java:843)
  at android.view.LayoutInflater.rInflateChildren (LayoutInflater.java:810)
  at android.view.LayoutInflater.rInflate (LayoutInflater.java:855)
  at android.view.LayoutInflater.rInflateChildren (LayoutInflater.java:810)
  at android.view.LayoutInflater.inflate (LayoutInflater.java:527)
Caused by: java.lang.ClassNotFoundException: 
  at dalvik.system.BaseDexClassLoader.findClass (BaseDexClassLoader.java:56)
  at java.lang.ClassLoader.loadClass (ClassLoader.java:511)
  at java.lang.ClassLoader.loadClass (ClassLoader.java:469)
  at android.view.LayoutInflater.createView (LayoutInflater.java:595)
  at android.view.LayoutInflater.createViewFromTag (LayoutInflater.java:776)

EDIT2:

layout/toolbar.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_
    android:layout_
    android:gravity="center"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    local:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

编辑:

LoginActivity.java:

package x.y.z;

import android.content.Intent;
import android.os.Bundle;
//import androidx.core.app.Fragment;
//import androidx.core.app.FragmentManager;
//import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import x.y.z.common.ActivityBase;

public class LoginActivity extends ActivityBase 

    Toolbar mToolbar;

    Fragment fragment;

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

        mToolbar = (Toolbar) findViewById(R.id.toolbar);

        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

        if (savedInstanceState != null) 

            fragment = getSupportFragmentManager().getFragment(savedInstanceState, "currentFragment");

         else 

            fragment = new LoginFragment();
        

        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.container_body, fragment).commit();
    

    @Override
    protected void onSaveInstanceState(Bundle outState) 

        super.onSaveInstanceState(outState);

        getSupportFragmentManager().putFragment(outState, "currentFragment", fragment);
    

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 

        super.onActivityResult(requestCode, resultCode, data);

        fragment.onActivityResult(requestCode, resultCode, data);
    

    @Override
    public void onBackPressed()

        finish();
    

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        switch (item.getItemId()) 

            case android.R.id.home: 

                finish();
                return true;
            

            default: 

                return super.onOptionsItemSelected(item);
            
        
    

activity_login.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_
    android:layout_
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="x.y.z.LoginActivity" >

    <LinearLayout
        android:id="@+id/container_toolbar"
        android:layout_
        android:layout_
        android:orientation="vertical">

        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar" />

    </LinearLayout>

    <FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/container_body"
        android:name="ru.ifsoft.network.LoginFragment"
        android:layout_
        android:layout_
        android:visibility="visible" />

</LinearLayout>

MainActivity.java

//Imported libraries.    
public class MainActivity extends ActivityBase implements FragmentDrawer.FragmentDrawerListener, ImageChooseDialog.AlertPositiveListener, ProfileReportDialog.AlertPositiveListener, PopularSettingsDialog.AlertPositiveListener 

        Toolbar mToolbar;

        private FragmentDrawer drawerFragment;

        // used to store app title
    private CharSequence mTitle;

    LinearLayout mContainerAdmob;

    Fragment fragment;
    Boolean action = false;
    int page = 0;

    private Boolean restore = false;
    Boolean entered = false;

    private AdView mAdView;

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



        if (savedInstanceState != null) 

            //Restore the fragment's instance
            fragment = getSupportFragmentManager().getFragment(savedInstanceState, "currentFragment");

            restore = savedInstanceState.getBoolean("restore");
            mTitle = savedInstanceState.getString("mTitle");

         else 

            fragment = new StreamFragment();

            restore = false;
            mTitle = getString(R.string.app_name);
        

        if (fragment != null) 

            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.container_body, fragment).commit();
        

        mToolbar = (Toolbar) findViewById(R.id.toolbar);

        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setTitle(mTitle);

        drawerFragment = (FragmentDrawer) getFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
        drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar);
        drawerFragment.setDrawerListener(this);

        mContainerAdmob = (LinearLayout) findViewById(R.id.container_admob);



        mContainerAdmob.setVisibility(View.VISIBLE);

        mAdView = findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
        mAdView.bringToFront();

        if (!restore) 

            // Show default section "Stream"

            displayView(1);
        


    


    /** Called when leaving the activity */
    @Override
    public void onPause() 
        if (mAdView != null) 
            mAdView.pause();
        
        super.onPause();
    

    /** Called when returning to the activity */
    @Override
    public void onResume() 
        super.onResume();
        if (mAdView != null) 
            mAdView.resume();
        
    

    /** Called before the activity is destroyed */
    @Override
    public void onDestroy() 
        if (mAdView != null) 
            mAdView.destroy();
        
        super.onDestroy();
    

    @Override
    protected void onSaveInstanceState(Bundle outState) 

        super.onSaveInstanceState(outState);

        outState.putBoolean("restore", true);
        outState.putString("mTitle", getSupportActionBar().getTitle().toString());
        getSupportFragmentManager().putFragment(outState, "currentFragment", fragment);
    

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) 

        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        fragment.onRequestPermissionsResult(requestCode, permissions, grantResults);
    

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) 

        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == ACTION_LOGIN && resultCode == RESULT_OK && null != data) 

            String pageId = data.getStringExtra("pageId");

            switch (pageId) 

                case "favorites": 

                    displayView(5);

                    break;
                

                case "notifications": 

                    displayView(6);

                    break;
                

                case "profile": 

                    displayView(7);

                    break;
                


                case "settings": 


                    Intent i = new Intent(MainActivity.this, SettingsActivity.class);
                    startActivity(i);

                    break;
                

                default: 

                    break;
                
            
        
    

    @Override
    public void onChangePopularCategory(int position) 

        PopularFragment p = (PopularFragment) fragment;
        p.onChangeCategory(position);
    

    @Override
    public void onImageFromGallery() 

        ProfileFragment p = (ProfileFragment) fragment;
        p.imageFromGallery();
    

    @Override
    public void onImageFromCamera() 

        ProfileFragment p = (ProfileFragment) fragment;
        p.imageFromCamera();
    

    @Override
    public void onProfileReport(int position) 

        ProfileFragment p = (ProfileFragment) fragment;
        p.onProfileReport(position);
    


    @Override
    public void onDrawerItemSelected(View view, int position) 

        displayView(position);
    

    private void displayView(int position) 

        action = false;

        switch (position) 

            case 0: 

                break;
            

            case 1: 

                page = 1;

                fragment = new StreamFragment();
                getSupportActionBar().setTitle(R.string.page_1);

                action = true;

                break;
            

            case 2: 

                page = 2;

                fragment = new CategoriesFragment();
                getSupportActionBar().setTitle(R.string.page_2);

                action = true;

                break;
            


            case 3: 

                page = 3;

                fragment = new SearchFragment();
                getSupportActionBar().setTitle("");

                action = true;

                break;
            

            case 4: 

                page = 4;

                fragment = new PopularFragment();
                getSupportActionBar().setTitle(R.string.page_4);

                action = true;

                break;
            

            case 5: 

                if (App.getInstance().getId() != 0)

                    page = 5;

                    fragment = new FavoritesFragment();
                    getSupportActionBar().setTitle(R.string.page_5);

                    action = true;

                 else 

                    Intent i = new Intent(MainActivity.this, LoginActivity.class);
                    i.putExtra("pageId", "favorites");
                    startActivityForResult(i, ACTION_LOGIN);
                

                break;
            


            case 6: 

                if (App.getInstance().getId() != 0)

                    page = 6;

                    fragment = new NotificationsFragment();
                    getSupportActionBar().setTitle(R.string.page_6);

                    action = true;

                 else 

                    Intent i = new Intent(MainActivity.this, LoginActivity.class);
                    i.putExtra("pageId", "notifications");
                    startActivityForResult(i, ACTION_LOGIN);
                

                break;
            


            case 7: 

                if (App.getInstance().getId() != 0)

                    page = 7;

                    fragment = new ProfileFragment();
                    getSupportActionBar().setTitle(R.string.page_7);

                    action = true;

                 else 

                    Intent i = new Intent(MainActivity.this, LoginActivity.class);
                    i.putExtra("pageId", "profile");
                    startActivityForResult(i, ACTION_LOGIN);
                

                break;
            
            case 8: 
                //Log.d("case8","case8");

                Uri uri = Uri.parse("market://details?id=" + "x.y.z");
                Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
                // To count with Play market backstack, After pressing back button,
                // to taken back to our application, we need to add following flags to intent.
                goToMarket.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY |
                        Intent.FLAG_ACTIVITY_NEW_DOCUMENT |
                        Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
                try 
                    startActivity(goToMarket);
                 catch (ActivityNotFoundException e) 
                    startActivity(new Intent(Intent.ACTION_VIEW,
                            Uri.parse("http://play.google.com/store/apps/details?id=" + "x.y.z")));
                
                break;
            

            case 9: 

                //Log.d("case8","son");

                if (App.getInstance().getId() != 0) 

                    Intent i = new Intent(MainActivity.this, SettingsActivity.class);
                    startActivity(i);

                 else 

                    Intent i = new Intent(MainActivity.this, LoginActivity.class);
                    i.putExtra("pageId", "settings");
                    startActivityForResult(i, ACTION_LOGIN);
                

                break;
            
        

        if (action) 

            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.container_body, fragment)
                    .commit();
        
    

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) 
        // If the nav drawer is open, hide action items related to the content view
        return super.onPrepareOptionsMenu(menu);
    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 

        return super.onCreateOptionsMenu(menu);
    

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        switch (item.getItemId()) 

            case android.R.id.home: 

                return true;
            

            default: 

                return super.onOptionsItemSelected(item);
            
        
    

    @Override
    public void onBackPressed() 

        if (drawerFragment.isDrawerOpen()) 

            drawerFragment.closeDrawer();

         else 

            super.onBackPressed();
        
    

    @Override
    public void setTitle(CharSequence title) 

        mTitle = title;
        getSupportActionBar().setTitle(mTitle);
    

    public void hideAds() 

        if (App.getInstance().getAdmob() == ADMOB_DISABLED) 

            mContainerAdmob.setVisibility(View.GONE);
        
    

在我的 Lg G2 4.4.2 和 Nexus Emulators(8.0 - 9.0) 中,没有问题。

activity_main.xml:

<androidx.drawerlayout.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_
    android:layout_>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_
        android:layout_
        tools:context=".MainActivity"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/container_toolbar"
            android:layout_
            android:layout_
            android:orientation="vertical">

            <include
                android:id="@+id/toolbar"
                layout="@layout/toolbar" />

        </LinearLayout>

        <FrameLayout
            android:id="@+id/container_body"
            android:layout_
            android:layout_
            android:layout_weight="1"
            android:visibility="visible"
            android:layout_marginBottom="5dp"/>

        <LinearLayout
            android:orientation="vertical"
            android:layout_
            android:layout_
            android:background="#00ffffff"
            android:gravity="center"
            android:id="@+id/container_admob"
            android:visibility="gone">


            <com.google.android.gms.ads.AdView
                android:id="@+id/adView"
                android:layout_
                android:layout_
                android:layout_centerHorizontal="true"
                android:layout_alignParentBottom="true"
                ads:adSize="BANNER"
                ads:adUnitId="@string/banner_ad_unit_id"
                android:layout_marginBottom="3dp">
            </com.google.android.gms.ads.AdView>

        </LinearLayout>

    </LinearLayout>


    <fragment
        android:id="@+id/fragment_navigation_drawer"
        android:name="x.y.z.FragmentDrawer"
        android:layout_
        android:layout_
        android:layout_gravity="start"
        app:layout="@layout/fragment_navigation_drawer"
        tools:layout="@layout/fragment_navigation_drawer" />

</androidx.drawerlayout.widget.DrawerLayout>

最后,我该如何解决这个问题?什么是(或可能是)主要原因?

感谢您的回复。

【问题讨论】:

我认为问题出在 LoginActivity @SalmanAziz 嗯,让我编辑代码。 问题已编辑。 什么是异常信息?找不到哪个类? 向我们展示您的包含工具栏。正如它所说的android.view.LayoutInflater.parseInclude (LayoutInflater.java:966) 我认为问题出在你的@layout/toolbar 【参考方案1】:

我认为问题出在您的@layout/toolbar 上,正如您的堆栈跟踪中的android.view.LayoutInflater.parseInclude (LayoutInflater.java:966) 所说。尝试检查您的工具栏视图是否正确。

【讨论】:

问题出在land/toolbar.xml。这就是为什么我的 LG G2 和模拟器没有问题的原因。然后,你节省了我的时间!再次感谢

以上是关于Androidx java.lang.ClassNotFoundException: 和 android.view.InflateException: 同时的主要内容,如果未能解决你的问题,请参考以下文章

如何在非 AndroidX 应用程序中集成任何 AndroidX SDK

带有 --androidx 的新 Flutter 项目出现 AndroidX 不兼容错误

AndroidX适配与升级指南

AndroidX适配与升级指南

AndroidX适配与升级指南

AndroidX适配与升级指南