Android:我删除了一个包,然后我得到了 ClassNotFoundException:在路径上找不到类

Posted

技术标签:

【中文标题】Android:我删除了一个包,然后我得到了 ClassNotFoundException:在路径上找不到类【英文标题】:Android: I deleted a package then I got ClassNotFoundException: Didn't find class on path 【发布时间】:2021-04-26 11:02:24 【问题描述】:

我删除了一个包含我的片段类的 UI 包。 The UI package I deleted

然后我在 bustracker 包中创建了同名的新类 What my project set up looks like now

当我运行应用程序时,我收到以下错误。

caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.bustracker.ui.nearbyStops.nearbyStopsFragment" on path: DexPathList[[zip file "/data/app/com.example.bustracker-cuWeKLTM_oUFXduWOdcaDA==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.bustracker-cuWeKLTM_oUFXduWOdcaDA==/lib/x86, /system/lib, /vendor/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:125)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
            ... 55 more

我的问题是,如何让它不再寻找 com.example.bustracker.ui.nearbyStops.nearbyStopsFragment,而是寻找 com.example.bustracker.nearbyStopsFragment

Project 有 4 个类,您可以在下面找到所有这些类。

MainActivity.java


import android.os.Bundle;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.view.Menu;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.navigation.NavigationView;

import androidx.annotation.NonNull;
import androidx.core.view.GravityCompat;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

public class MainActivity extends AppCompatActivity 

    private AppBarConfiguration mAppBarConfiguration;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        /*FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View view) 
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            
        );*/

        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_nearby_stops, R.id.nav_saved, R.id.nav_search)
                .setDrawerLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() 
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) 
                switch (item.getItemId())
                    case R.id.nav_nearby_stops:
                        getSupportFragmentManager().beginTransaction().replace(R.id.nav_view, new nearbyStopsFragment()).commit();
                        break;
                    case R.id.nav_saved:
                        getSupportFragmentManager().beginTransaction().replace(R.id.nav_view, new savedFragment()).commit();
                        break;
                    case R.id.nav_search:
                        getSupportFragmentManager().beginTransaction().replace(R.id.nav_view, new searchFragment()).commit();
                        break;
                
                drawer.closeDrawer(GravityCompat.START);
                return true;
            
        );
    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    

    @Override
    public boolean onSupportNavigateUp() 
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    

nearbyStopsFragment.java


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class nearbyStopsFragment extends Fragment 

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) 
        return inflater.inflate(R.layout.fragment_nearby_stops, container, false);
    

savedFragment.java


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class savedFragment extends Fragment 

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) 
        return inflater.inflate(R.layout.fragment_saved, container, false);
    

searchFragment.java


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class searchFragment extends Fragment 

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) 
        return inflater.inflate(R.layout.fragment_search, container, false);
    

【问题讨论】:

它在您的消息中说:“没有找到类“com.example.bustracker.ui.nearbyStops.nearbyStopsFragment”。因此,您可能正在尝试调用您之前删除的名为 nearStopsFragment 的片段。因为你的问题真的很糟糕,我不能肯定地说。请阅读规则,然后相应地发布问题。 同意@SlothCoding 尚不清楚发生了什么,尽管可以得出结论,您正在尝试从已删除的包中加载片段。最好阅读:***.com/help/how-to-ask 并添加更多细节以帮助他人帮助您。 请显示完整的错误(展开那 55 行) 【参考方案1】:

不看代码很难知道,但看起来 BaseDexClassLoader 中的某些内容试图在第 125 行调用片段文件中的某些内容。

【讨论】:

【参考方案2】:

在您的activity/fragment 中,除了类之外,您还将拥有大量正在使用的包的导入。应该有这样的导入:

import com.example.bustracker.ui.nearbyStops.nearbyStopsFragment;

只需将其更改为这个新的:

import com.example.bustracker.nearbyStopsFragment;

如果这不起作用,请在导入时提供您的 JAVA 代码。

【讨论】:

【参考方案3】:

我在将一些Java类转换为Kotlin时遇到了这个问题,所以我发现我需要在Module gradle中添加id "kotlin-android"插件和implementation "androidx.core:core-ktx:1.7.0"在项目gradle中添加classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10"

【讨论】:

以上是关于Android:我删除了一个包,然后我得到了 ClassNotFoundException:在路径上找不到类的主要内容,如果未能解决你的问题,请参考以下文章

Android 意图:总是得到 resultCode = 0

如何在android中删除包?

如何手动添加Android Dependencies包

获取 Android 应用程序包的所有已安装功能

解压缩包后的文件可以删吗

android ndk 开发1