【发布时间】: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