空白Android活动上找不到类错误

时间:2016-04-10 16:44:00

标签: java android android-studio

当我在Android Studio 2.0中创建一个空白活动并尝试在API 16上运行它时,它编译时没有错误。当我在使用API​​ 23的模拟器上运行它时,它运行没有错误。当我在API 16上运行它时,它会在应用程序启动时给出一堆错误。见下文。奇怪的是应用运行正常。

这可能是什么原因?

在Android Studio中,我无效并清除了缓存。还清理了项目并重建了它。

参考代码:

的AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="nl.myapp.test">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

MainActivity.Java

package nl.myapp.test;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

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

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="nl.myapp.test.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
</RelativeLayout>

Gradle 2.0.0构建文件build.gradle(模块应用程序)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "nl.myapp.test"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.3.0'
}

应用在API 16上启动时的logcat错误

04-10 18:29:29.945 23899-23899/nl.myapp.test E/dalvikvm: Could not find class 'android.app.ActivityManager$TaskDescription', referenced from method nl.myapp.test.MainActivity.access$super
04-10 18:29:29.945 23899-23899/nl.myapp.test E/dalvikvm: Could not find class 'android.os.UserHandle', referenced from method nl.myapp.test.MainActivity.access$super
04-10 18:29:29.945 23899-23899/nl.myapp.test E/dalvikvm: Could not find class 'android.os.UserHandle', referenced from method nl.myapp.test.MainActivity.access$super
04-10 18:29:29.945 23899-23899/nl.myapp.test E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method nl.myapp.test.MainActivity.access$super
04-10 18:29:29.945 23899-23899/nl.myapp.test E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method nl.myapp.test.MainActivity.access$super
04-10 18:29:29.945 23899-23899/nl.myapp.test E/dalvikvm: Could not find class 'android.transition.TransitionManager', referenced from method nl.myapp.test.MainActivity.access$super
04-10 18:29:29.945 23899-23899/nl.myapp.test E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method nl.myapp.test.MainActivity.access$super
04-10 18:29:29.945 23899-23899/nl.myapp.test E/dalvikvm: Could not find class 'android.os.UserHandle', referenced from method nl.myapp.test.MainActivity.access$super
04-10 18:29:29.945 23899-23899/nl.myapp.test E/dalvikvm: Could not find class 'android.os.UserHandle', referenced from method nl.myapp.test.MainActivity.access$super
04-10 18:29:29.945 23899-23899/nl.myapp.test E/dalvikvm: Could not find class 'android.app.assist.AssistContent', referenced from method nl.myapp.test.MainActivity.access$super
04-10 18:29:29.945 23899-23899/nl.myapp.test E/dalvikvm: Could not find class 'android.view.SearchEvent', referenced from method nl.myapp.test.MainActivity.access$super
04-10 18:29:29.945 23899-23899/nl.myapp.test E/dalvikvm: Could not find class 'android.os.UserHandle', referenced from method nl.myapp.test.MainActivity.access$super
04-10 18:29:29.945 23899-23899/nl.myapp.test E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method nl.myapp.test.MainActivity.access$super

2 个答案:

答案 0 :(得分:2)

可能是您的应用达到64k方法限制。从API级别21及以下版本中可以看到该问题。 请参阅document。它提供了有关该问题的详细信息以及如何使用Multidex支持库来解决旧版Android的问题。

同样this可以快速解决问题。

答案 1 :(得分:1)

在Android Studio中,我访问了文件 - &gt;项目结构 - &gt;项目

首先,我将Gradle升级为 2.11 ,将Android插件版本升级为 2.0.0 。 接下来,我同步了项目并编译了它。错误仍然存​​在。

在此之后,我将Android插件版本降级为 1.5.0

现在,当我编译并运行项目时,错误就消失了。