簡體   English   中英

Android應用程序,Android Studio中的“未找到類異常”-我在做什么錯?

[英]Android app, “Class not found exception” in Android Studio - what am I doing wrong?

每當我嘗試運行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個類,但是我認為它們中的任何一個都不會在崩潰發生之前被調用。 對於這個可憐的程序,我到底在做什么錯了???

我猜想您對包含UsableScreenSizeLineView類的庫以及許多其他庫都具有gradle依賴性。 由於設計限制,dex文件(其中的類存儲在apk中)不能包含超過65k個方法。 當應用程序具有更多方法或使用某些開發功能(例如即時運行)時,類將拆分為不同的dex文件,並且可以使用不同的類加載器加載dex文件。 有一個類加載器樹層次結構,其中引導類加載器位於頂部。 這些類加載器用於從dex文件中獲取類,然后才能在應用程序中使用它們;當類加載器找不到類時,它將搜索委托給其父級。 問題是父級不詢問其子級,因此如果引導類加載器找不到它,則應用程序停止。

首先,您應該通過從布局中刪除UsableScreenSizeLineLineView來確定是否是導致問題的類。 如果錯誤消失,則可以嘗試以下可能的解決方法:

  • 如果已啟用,則在android studio設置中禁用即時運行,或者
  • 在列表中將依賴項移至您的庫中,以便在類屬於庫的情況下將其與引導類加載器一起加載,或者
  • 如果視圖當前位於庫中,則將視圖的源代碼包含在您的項目中(作為單獨的模塊)

更換norrisduncan.doubledrawers.UsableScreenSizeLineViewnorrisduncan.dataman.UsableScreenSizeLineView在XML文件中。

問題出在您的xml文件上。

xml文件中,您已使用<norrisduncan.doubledrawers.UsableScreenSizeLineView>標記,而在DataMan_Main.java ,您已使用norrisduncan.dataman.UsableScreenSizeLineView 您需要用正確的軟件包名稱替換它。

更改java中的值

私人諾里斯登坎。 dataman .UsableScreenSizeLineView

諾里斯敦坎。 doubledrawers .UsableScreenSizeLineView

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM