0

照着第一行代码实例敲的,在第8章使用通知的实例代码中卡住了。具体代码

MainActivity

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button sendNotice = (Button) findViewById(R.id.send_notice);
        sendNotice.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.send_notice:
                NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
                builder.setContentTitle("BasicNotifications Sample");
                builder.setContentText("Time to learn about notifications!");
                builder.setSubText("Tap to view documentation about notifications.");
                builder.setAutoCancel(true);
                //.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher_round));
                manager.notify(1, builder.build());

                break;
            default:
                break;

        }
    }
}

Gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.2"
    defaultConfig {
        applicationId "com.example.administrator.mediatest"
        minSdkVersion 21
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
}

报错log

01-09 15:10:58.225 27760-27760/com.example.administrator.mediatest E/Typeface: SANS_LOC file not found.
01-09 15:10:58.695 27760-27760/com.example.administrator.mediatest W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
01-09 15:10:58.765 27760-27760/com.example.administrator.mediatest E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                     Process: com.example.administrator.mediatest, PID: 27760
                                                                                     java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v7/appcompat/R$drawable;
                                                                                         at android.support.v7.widget.AppCompatDrawableManager.<clinit>(AppCompatDrawableManager.java:110)
                                                                                         at android.support.v7.widget.AppCompatDrawableManager.get(AppCompatDrawableManager.java:84)
                                                                                         at android.support.v7.widget.TintTypedArray.getDrawableIfKnown(TintTypedArray.java:81)
                                                                                         at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:129)
                                                                                         at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:152)
                                                                                         at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:29)
                                                                                         at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:53)
                                                                                         at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:204)
                                                                                         at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:184)
                                                                                         at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:518)
                                                                                         at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:70)
                                                                                         at com.example.administrator.mediatest.MainActivity.onCreate(MainActivity.java:18)
                                                                                         at android.app.Activity.performCreate(Activity.java:5958)
                                                                                         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
                                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
                                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)
                                                                                         at android.app.ActivityThread.access$800(ActivityThread.java:144)
                                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
                                                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                         at android.os.Looper.loop(Looper.java:155)
                                                                                         at android.app.ActivityThread.main(ActivityThread.java:5702)
                                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                                         at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
                                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)
                                                                                      Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v7.appcompat.R$drawable" on path: DexPathList[[zip file "/data/app/com.example.administrator.mediatest-2/base.apk", zip file "/data/app/com.example.administrator.mediatest-2/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.administrator.mediatest-2/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.administrator.mediatest-2/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.administrator.mediatest-2/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.administrator.mediatest-2/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.administrator.mediatest-2/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.administrator.mediatest-2/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.administrator.mediatest-2/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.administrator.mediatest-2/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.administrator.mediatest-2/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/vendor/lib64, /system/lib64]]
                                                                                         at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
                                                                                         at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
                                                                                         at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
                                                                                         at android.support.v7.widget.AppCompatDrawableManager.<clinit>(AppCompatDrawableManager.java:110) 
                                                                                         at android.support.v7.widget.AppCompatDrawableManager.get(AppCompatDrawableManager.java:84) 
                                                                                         at android.support.v7.widget.TintTypedArray.getDrawableIfKnown(TintTypedArray.java:81) 
                                                                                         at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:129) 
                                                                                         at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:152) 
                                                                                         at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:29) 
                                                                                         at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:53) 
                                                                                         at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:204) 
                                                                                         at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:184) 
                                                                                         at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:518) 
                                                                                         at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:70) 
                                                                                         at com.example.administrator.mediatest.MainActivity.onCreate(MainActivity.java:18) 
                                                                                         at android.app.Activity.performCreate(Activity.java:5958) 
                                                                                         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129) 
                                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364) 
                                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474) 
                                                                                         at android.app.ActivityThread.access$800(ActivityThread.java:144) 
                                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359) 
                                                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                         at android.os.Looper.loop(Looper.java:155) 
                                                                                         at android.app.ActivityThread.main(ActivityThread.java:5702) 
                                                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                                                         at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029) 
                                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824) 
                                                                                         Suppressed: java.lang.ClassNotFoundException: android.support.v7.appcompat.R$drawable
                                                                                         at java.lang.Class.classForName(Native Method)
                                                                                         at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
                                                                                         at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
                                                                                         at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
                                                                                                 ... 26 more
                                                                                      Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

关键在这几句,希望得到大家指点
java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v7/appcompat/R$drawable;

Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v7.appcompat.R$drawable" on path:

Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

1个回答

0

请从1到5依次尝试:

  1. 尝试在File | Settings | Build, Execution, Deployment | Instant Run中关闭Instant Run,重新安装APK。
  2. 使用Build->Clean ProjectBuild->Build Project
  3. 还不行就卸载测试机上的app,重装一次。
  4. 换用其他版本的appcompat-v7,如compile 'com.android.support:appcompat-v7:25.+'
  5. 重装Android SDK。
  6. 最后大招:重装Android Studio。

希望能帮助到你

撰写答案

相似问题