我正在尝试使用支持库中的Snackbar但是当我在一个API低于21的模拟器上午餐时,该应用程序会崩溃。 我认为支持库会使小吃栏在旧版本上运行,这是正确的吗?
Gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.example.user.test"
minSdkVersion 15
targetSdkVersion 25
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(include: ['*.jar'], dir: 'libs')
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:25.2.0'
testCompile 'junit:junit:4.12'
compile 'com.android.support:design:25.2.0'
compile 'com.automationrockstars:design:1.0.3'
}
MainActivity.java
package com.example.user.test;
import android.support.design.widget.Snackbar;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onStart() {
super.onStart();
LinearLayout l = (LinearLayout) findViewById(R.id.linear1);
Snackbar snackbar = Snackbar.make(l,"Hello",Snackbar.LENGTH_LONG);
snackbar.show();
}
}
编辑(logcat)
03-04 18:54:49.827 1468-1468/? D/dalvikvm: Late-enabling CheckJNI
03-04 18:54:49.911 1468-1474/? E/jdwp: Failed sending reply to debugger: Broken pipe
03-04 18:54:49.911 1468-1474/? D/dalvikvm: Debugger has detached; object registry had 1 entries
03-04 18:54:49.939 1468-1468/? I/dalvikvm: Could not find method android.view.Window$Callback.onProvideKeyboardShortcuts, referenced from method android.support.v7.view.WindowCallbackWrapper.onProvideKeyboardShortcuts
03-04 18:54:49.951 1468-1468/? W/dalvikvm: VFY: unable to resolve interface method 20911: Landroid/view/Window$Callback;.onProvideKeyboardShortcuts (Ljava/util/List;Landroid/view/Menu;I)V
03-04 18:54:49.951 1468-1468/? D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
03-04 18:54:49.955 1468-1468/? W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
03-04 18:54:49.959 1468-1468/? I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested
03-04 18:54:49.963 1468-1468/? W/dalvikvm: VFY: unable to resolve interface method 20913: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
03-04 18:54:49.963 1468-1468/? D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
03-04 18:54:49.963 1468-1468/? I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode
03-04 18:54:49.963 1468-1468/? W/dalvikvm: VFY: unable to resolve interface method 20917: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
03-04 18:54:49.963 1468-1468/? D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
03-04 18:54:49.963 1468-1468/? I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.widget.TintTypedArray.getChangingConfigurations
03-04 18:54:49.963 1468-1468/? W/dalvikvm: VFY: unable to resolve virtual method 487: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
03-04 18:54:49.963 1468-1468/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
03-04 18:54:49.963 1468-1468/? I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.widget.TintTypedArray.getType
03-04 18:54:49.967 1468-1468/? W/dalvikvm: VFY: unable to resolve virtual method 509: Landroid/content/res/TypedArray;.getType (I)I
03-04 18:54:49.967 1468-1468/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0008
03-04 18:54:50.059 1468-1468/? I/dalvikvm: Could not find method android.widget.FrameLayout.startActionModeForChild, referenced from method android.support.v7.widget.ActionBarContainer.startActionModeForChild
03-04 18:54:50.059 1468-1468/? W/dalvikvm: VFY: unable to resolve virtual method 21394: Landroid/widget/FrameLayout;.startActionModeForChild (Landroid/view/View;Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
03-04 18:54:50.063 1468-1468/? D/dalvikvm: VFY: replacing opcode 0x6f at 0x0002
03-04 18:54:50.071 1468-1468/? I/dalvikvm: Could not find method android.content.Context.getColorStateList, referenced from method android.support.v7.content.res.AppCompatResources.getColorStateList
03-04 18:54:50.087 1468-1468/? W/dalvikvm: VFY: unable to resolve virtual method 301: Landroid/content/Context;.getColorStateList (I)Landroid/content/res/ColorStateList;
03-04 18:54:50.087 1468-1468/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0006
03-04 18:54:50.095 1468-1468/? I/dalvikvm: Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.widget.ResourcesWrapper.getDrawable
03-04 18:54:50.095 1468-1468/? W/dalvikvm: VFY: unable to resolve virtual method 450: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
03-04 18:54:50.095 1468-1468/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
03-04 18:54:50.095 1468-1468/? I/dalvikvm: Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.widget.ResourcesWrapper.getDrawableForDensity
03-04 18:54:50.095 1468-1468/? W/dalvikvm: VFY: unable to resolve virtual method 452: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable;
03-04 18:54:50.095 1468-1468/? D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
03-04 18:54:50.107 1468-1468/? E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
03-04 18:54:50.107 1468-1468/? W/dalvikvm: VFY: unable to resolve instanceof 146 (Landroid/graphics/drawable/RippleDrawable;) in Landroid/support/v7/widget/AppCompatImageHelper;
03-04 18:54:50.107 1468-1468/? D/dalvikvm: VFY: replacing opcode 0x20 at 0x000c
03-04 18:54:50.187 1468-1471/? D/dalvikvm: GC_CONCURRENT freed 188K, 10% free 2682K/2980K, paused 2ms+1ms, total 62ms
03-04 18:54:50.187 1468-1468/? D/dalvikvm: WAIT_FOR_CONCURRENT_GC blocked 57ms
03-04 18:54:50.187 1468-1468/? D/AndroidRuntime: Shutting down VM
03-04 18:54:50.187 1468-1468/? W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xa61fe908)
03-04 18:54:50.191 1468-1468/? E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.test/com.example.user.test.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
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:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.support.design.widget.Snackbar.make(Snackbar.java:136)
at com.example.user.test.MainActivity.onStart(MainActivity.java:21)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1164)
at android.app.Activity.performStart(Activity.java:5114)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
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:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
试试这段代码:
LinearLayout l;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l = (LinearLayout) findViewById(R.id.linear1);
}
@Override
protected void onStart() {
super.onStart();
Snackbar snackbar = Snackbar.make(l,"Hello",Snackbar.LENGTH_LONG);
snackbar.show();
}
如果不起作用,请使用snackbar
android.R.id.content
Snackbar.make(findViewById(android.R.id.content), "Hello", Snackbar.LENGTH_LONG).show();