gpt4 book ai didi

java - 自定义 View 适用于 API21,但在较低的 API 上会出现 ClassNotFound 异常

转载 作者:行者123 更新时间:2023-12-01 11:44:05 26 4
gpt4 key购买 nike

当我在带有 API21 的 Android 模拟器上运行我的应用程序时,它工作正常,但是当我在 API16 模拟器上运行我的应用程序时,我在 fragment 膨胀时捕获错误。错误示例

>  java.lang.RuntimeException: Unable to resume activity {ajax.ir.com.ajax/com.ir.ajax.activity.AuthentificationActivity}: android.view.InflateException: Binary XML file line **#57: Error inflating class com.ir.ajax.views.AJTextView**
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2575)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2603)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2089)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #57: Error inflating class com.ir.ajax.views.AJTextView . . . .
Caused by: java.lang.ClassNotFoundException: com.ir.ajax.views.AJTextView
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
at android.view.LayoutInflater.createView(LayoutInflater.java:552)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at com.ir.ajax.fragments.AuthentificationFragment.onCreateView(AuthentificationFragment.java:26)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
at android.support.v4.app.FragmentActivity.onPostResume(FragmentActivity.java:434)
at android.app.Activity.performResume(Activity.java:5095)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2565)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2603)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2089)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)

此 View 位于我的 fragment XML 中,其中包含自定义 View :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aj="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">

<LinearLayout
android:id="@+id/bttn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:paddingBottom="28dp"
android:paddingTop="28dp">

<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@drawable/s_d_blue"
android:duplicateParentState="true" />

<!--<com.ir.ajax.views.AJButton
android:id="@+id/bttn_enter"
style="@style/ButtonBlueText"
aj:typeface="os_light"
android:text="@string/entry"
android:onClick="onClick" />-->

<Button
android:id="@+id/bttn_enter"
style="@style/ButtonBlueText"
android:text="@string/entry"
android:onClick="onClick" />

<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@drawable/s_d_blue"
android:duplicateParentState="true" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:paddingBottom="28dp"
android:paddingTop="28dp">

<View
android:layout_width="48dp"
android:layout_height="1dp"
android:background="@color/grey_500" />

<com.ir.ajax.views.AJTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
aj:typeface="os_light"
android:padding="8dp"
android:text="@string/or"
android:textColor="@color/grey_500"
android:textSize="@dimen/text_size_36" />

<View
android:layout_width="48dp"
android:layout_height="1dp"
android:background="@color/grey_500" />
</LinearLayout>

<LinearLayout
android:id="@+id/btnRegistration"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:paddingBottom="28dp"
android:paddingTop="28dp">

<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@drawable/s_d_blue"
android:duplicateParentState="true" />

<com.ir.ajax.views.AJButton
android:id="@+id/bttn_register"
style="@style/ButtonBlueText"
aj:typeface="os_light"
android:text="@string/registration"
android:onClick="onClick" />

<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@drawable/s_d_blue"
android:duplicateParentState="true" />
</LinearLayout>

<Button
android:id="@+id/btnMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="all_fragments"
android:visibility="gone" />

<Button
android:id="@+id/btnActivitis"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="all_activity"
android:visibility="gone" />

和 fragment 代码:

    package com.ir.ajax.fragments;

import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

import com.ir.ajax.R;
import com.ir.ajax.activity.AuthentificationActivity;

public class AuthentificationFragment extends Fragment {


public void onCreate(Bundle savedInstanceState) {
setHasOptionsMenu(false);
super.onCreate(savedInstanceState);
}

//TODO
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_authentication, null);

Typeface os_light = Typeface.createFromAsset(getActivity().getAssets(), "fonts/os_light.ttf");
Button bttn_enter = (Button) view.findViewById(R.id.bttn_enter);
bttn_enter.setTypeface(os_light);

//TODO
final AuthentificationActivity owner = (AuthentificationActivity) getActivity();
if (owner != null) {
view.findViewById(R.id.btnMenu).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
owner.showDialog();
}
});
}
if (owner != null) {
view.findViewById(R.id.btnActivitis).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
owner.showDialogActivities();
}
});
}

return view;
}

}

我的自定义 View 示例:

package com.ir.ajax.views;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;

import com.ir.ajax.utils.TextViewUtils;

public class AJTextView extends TextView {

public AJTextView(Context context) {
this(context, null);
TextViewUtils.initTypeface(this, context);
}

public AJTextView(Context context, AttributeSet attrs) {
super(context, attrs);

if (!isInEditMode()) {
TextViewUtils.initTypeface(this, context, attrs);
}
}

public AJTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);

if (!isInEditMode()) {
TextViewUtils.initTypeface(this, context, attrs);
}
}

}

还有我的构建文件:

apply plugin: 'com.android.application'
apply plugin: 'sonar-runner'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "ajax.ir.com.ajax"
minSdkVersion 16
targetSdkVersion 19
versionCode 1
versionName "1.0"
multiDexEnabled = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
debuggable true
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
productFlavors {
}
lintOptions {
abortOnError false
}
}
allprojects {
gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.google.android.gms:play-services:6.5.87'
compile files('libs/nineoldandroids-2.4.0.jar')
compile files('libs/universal-image-loader-1.9.3.jar')
compile project(':qrscanner')
compile files('libs/NettyClient-1.0-SNAPSHOT-jar-with-dependencies.jar')
//compile 'com.android.support:support-v4:22.0.0'
compile files('libs/support-v4-22.0.0-javadoc.jar')
}
sonarRunner {
sonarProperties {
property "sonar.host.url", "http://127.0.0.1:9000"
property "sonar.jdbc.url", "jdbc:h2:tcp://localhost:9092/sonar"
property "sonar.projectName", "Ajax"
property "sonar.language", "java"
property "sonar.sources", "src"
property "sonar.binaries", "build"
}
}

最佳答案

您的项目是否已达到 65k+ 方法(dex 限制)?您似乎已在 build.gradle 中启用了 multidex。但这样做还不足以支持 multidex,您需要在 Application 类中扩展 MultidexApplication

Android 5.0 及更高版本使用名为 ART 的运行时,它本身支持从应用程序 APK 文件加载多个 dex 文件。因此,这可能就是您的应用程序在 API 5.0 + 中正常工作的原因

public class YourApplication extends MultiDexApplication {

.

.

您还需要在 build.gradle 中指定对 multidex 库的依赖

compile 'com.android.support:multidex:1.0.0'

你做到了吗?

有关更多信息,您可以引用此文档 https://developer.android.com/tools/building/multidex.html

关于java - 自定义 View 适用于 API21,但在较低的 API 上会出现 ClassNotFound 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29301784/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com