【问题标题】:Android app, "Class not found exception" in Android Studio - what am I doing wrong?Android 应用程序,Android Studio 中的“类未找到异常” - 我做错了什么?
【发布时间】:2018-07-19 17:57:05
【问题描述】:

每当我尝试运行 Android Studio 调试器时,它都会给我一个 Class Not Found 异常。似乎崩溃发生在第 38 行 - setContentView(R.layout.dataman_main); 以设置主要活动布局 - 之前工作正常。我是一名业余开发人员,我在这个问题上被困了 3 天 - 谁能告诉我我做错了什么?

这是崩溃后调试器变量的副本,其中“Exception”和“detailMessage”字段已展开:

Exception = {ClassNotFoundException@5326} 
 ex = {NoClassDefFoundError@5330} "java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available"
 backtrace = {Object[38]@5331} 
 cause = null
 detailMessage = "android.widget.ViewStub"
  count = 46
  hash = 0
  shadow$_klass_ = {Class@3925} "class java.lang.String"
  shadow$_monitor_ = -1913475893
 stackTrace = {StackTraceElement[0]@5333} 
 suppressedExceptions = {Collections$EmptyList@5334}  size = 0
 shadow$_klass_ = {Class@4242} "class java.lang.ClassNotFoundException"
 shadow$_monitor_ = -2070025328
Variables debug info not available

这是我的主要活动的样子:

package norrisduncan.dataman;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.PointF;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.view.ViewStub;

import java.lang.reflect.InvocationTargetException;

import norrisduncan.dataman.adapter.MainPagerAdapter;

/**
 * Created by norri on 12/13/2017.
 */


public class DataMan_Main extends AppCompatActivity {

PointF startPoint = new PointF(0,0);
PointF endPoint = new PointF(0,0);

private norrisduncan.dataman.UsableScreenSizeLineView UsableScreenSizeLineView;

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

    //FINDING THE SIZE OF THE USABLE SCREEN SPACE SO THAT UI ELEMENTS CAN SCALE TO IT
    DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    int usableScreenWidth = displayMetrics.widthPixels;
    int usableScreenHeight = displayMetrics.heightPixels;

    //ASSOCIATES UI JAVA IMAGEVIEW FIELDS WITH THEIR XML IMAGEVIEWS
    ImageView actionDrawerPullTab = findViewById(R.id.actionDrawerPullTabImageViewXML);

    UsableScreenSizeLineView = findViewById(R.id.usable_screen_size_line_view);
    UsableScreenSizeLineView.setStartPoint(startPoint);
    endPoint.set(usableScreenWidth,usableScreenHeight);
    UsableScreenSizeLineView.setEndPoint(endPoint);
    UsableScreenSizeLineView.draw();

    //SETS THE PAINT COLOR AND SIZE FOR THE TEST LINE THAT TELLS ME HOW BIG THE USABLE SCREEN SIZE IS
    Paint screenSizeTestLinePaint = new Paint();
    screenSizeTestLinePaint.setColor(Color.BLACK);
    screenSizeTestLinePaint.setStrokeWidth(1f);

    //SETS THE PULL TAB IMAGEVIEW SIZES ACCORDING TO SCREEN SIZE
    RelativeLayout.LayoutParams actionDrawerPullTabParams = new RelativeLayout.LayoutParams(usableScreenHeight / 3, usableScreenHeight / 6);
    actionDrawerPullTabParams.leftMargin = 0 - usableScreenHeight * 3 / 18;
    actionDrawerPullTabParams.topMargin = 0;
    actionDrawerPullTab.setLayoutParams(actionDrawerPullTabParams);

    //SETS THE VIEWPAGER AND PAGERADAPTER
    ViewPager viewPager = findViewById(R.id.view_pager);
    MainPagerAdapter mainPagerAdapter = new MainPagerAdapter(getSupportFragmentManager());
    viewPager.setAdapter(mainPagerAdapter);

}

//FINDS THE FULL SCREEN SIZE USING EITHER display.getRealSize() OR ELSE IT DOES SOME ?MAGIC? TO ?.INVOKE?
//(NEVER HEARD OF THIS) getRawHeight()/getRawWidth() ON THE DEFAULT DISPLAY
public static Point getScreenRealSize(Context context) {
    WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = windowManager.getDefaultDisplay();
    Point size = new Point();

    if (Build.VERSION.SDK_INT >= 17) {
        display.getRealSize(size);
    } else if (Build.VERSION.SDK_INT >= 14) {
        try {
            size.x = (Integer) Display.class.getMethod("getRawWidth").invoke(display);
            size.y = (Integer) Display.class.getMethod("getRawHeight").invoke(display);
        } catch (IllegalAccessException e) {
        } catch (InvocationTargetException e) {
        } catch (NoSuchMethodException e) {
        }
    }
    return size;
}

public static Point getUsableScreenSize(Context context) {
    WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = windowManager.getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    return size;
}

}

setContentView() 方法应该加载的布局文件 dataman_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >

    <View
        android:id="@+id/baselayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/paper" />

    <norrisduncan.doubledrawers.UsableScreenSizeLineView
        android:id="@+id/usable_screen_size_line_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <ImageView
    android:id="@+id/actionDrawerPullTabImageViewXML"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/actions_drawer_pull_tab_textured_662x331x441ppi"
    />

    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="48dp"
        android:text="@string/layout_fragment_1_title" />

</RelativeLayout>

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="norrisduncan.dataman">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name="norrisduncan.dataman.DataMan_Main"
            android:screenOrientation="portrait"
            android:theme="@style/FullScreenTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

如果有帮助,我也可以提供我的其他课程 - 其中有 6 个,但我认为在崩溃发生之前它们中的任何一个都不会被调用。我到底对这个糟糕的程序做错了什么???

【问题讨论】:

    标签: java android android-studio classnotfoundexception setcontentview


    【解决方案1】:

    我猜你对包含 UsableScreenSizeLineView 类的库以及许多其他库有 gradle 依赖关系。由于设计限制,dex 文件(类存储在 apk 中)不能包含超过 65k 的方法。当应用程序有更多的方法或使用即时运行等一些开发功能时,类会被拆分为不同的 dex 文件,并且可以使用不同的类加载器来加载 dex 文件。有一个类加载器树层次结构,顶部是引导类加载器。这些类加载器用于从 dex 文件中获取类,然后才能在应用程序中使用它们,并且当类加载器找不到类时,它会将搜索委托给其父类。问题是父级没有询问它的子级,所以如果引导类加载器没有找到它,应用程序就会停止。

    首先,您应该检查 UsableScreenSizeLineView 是否是导致问题的类,方法是将其从布局中删除。如果错误消失,您可以尝试以下可能的修复方法:

    • 如果已启用,请在 android studio 设置中禁用即时运行 或
    • 在列表中向上移动对库的依赖项,以便在类是库的一部分时使用引导类加载器加载它 或
    • 如果当前在库中,则在项目中包含视图的源代码(作为单独的模块)

    【讨论】:

      【解决方案2】:

      将您的 xml 文件中的norrisduncan.doubledrawers.UsableScreenSizeLineView 替换为norrisduncan.dataman.UsableScreenSizeLineView。

      【讨论】:

      • 啊,谢谢你的理解,你说得对,这是一个问题,但似乎这不是我要找的问题。我只能在重新安装 SDK 后解决这个问题,我最初下载它时空间不足,而且我一定会得到一些损坏的 Android 文件。
      【解决方案3】:

      问题在于您的 xml 文件。

      在xml 文件中,您使用了&lt;norrisduncan.doubledrawers.UsableScreenSizeLineView&gt; 标签,而在DataMan_Main.java 中,您使用了norrisduncan.dataman.UsableScreenSizeLineView。您需要用正确的包名称替换它。

      【讨论】:

        【解决方案4】:

        更改java中的值

        私人 norrisduncan.dataman.UsableScreenSizeLineView

        到

        norrisduncan.双抽屉.UsableScreenSizeLineView

        【讨论】:

        • Yyyup 那是我的错。虽然直到我重新安装了 Android SDK 后问题才真正消失,但我最近的硬盘空间不足,我想我可能只下载了部分 Android 类,但感谢您发现!
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-09-09
        • 2022-12-01
        • 2016-06-10
        • 1970-01-01
        • 1970-01-01
        • 2012-05-16
        • 2021-11-11
        相关资源
        最近更新 更多