我目前正在开始使用Android Studio开发新的Android应用程序。我已经更新了Gradle构建文件,并且我已成功添加了Crouton库。我有一个抽屉导航设置,当我点击一个项目时,我想通过显示它来测试Crouton,所以我使用了下面的代码:
@Override
public void onNavigationDrawerItemSelected(int position) {
Crouton.makeText(this, "Test", Style.ALERT).show();
}
当我运行此代码时,它构建正常,但应用程序崩溃,我得到下面的堆栈跟踪:
11-07 19:49:10.474 1212-1212/com.oss.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NoClassDefFoundError: de.keyboardsurfer.android.widget.crouton.Style
at com.oss.app.HomeActivity.onNavigationDrawerItemSelected(HomeActivity.java:59)
at com.oss.app.NavigationDrawerFragment.selectItem(NavigationDrawerFragment.java:204)
at com.oss.app.NavigationDrawerFragment.onCreate(NavigationDrawerFragment.java:79)
at android.support.v4.app.Fragment.performCreate(Fragment.java:1477)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:893)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1184)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:291)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
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.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
at android.app.Activity.setContentView(Activity.java:1867)
at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:216)
at android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDelegateICS.java:111)
at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:76)
at com.oss.app.HomeActivity.onCreate(HomeActivity.java:40)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
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)
据我所知,问题是在运行时没有找到Style类,但是我已经将它作为导入添加到页面上,所以我不知道是什么原因造成的。我真的很感激你的帮助。
提前致谢
修改
我的 build.gradle 文件如下:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
android {
compileSdkVersion 18
buildToolsVersion "18.1.1"
defaultConfig {
minSdkVersion 11
targetSdkVersion 18
}
}
dependencies {
compile files('libs/android-support-v4.jar')
compile files('libs/com.haarman.listviewanimations-2.5.2.jar')
compile files('libs/nineoldandroids-2.4.0.jar')
compile 'com.android.support:appcompat-v7:+'
compile ('de.keyboardsurfer.android.widget:crouton:1.8.1') {
exclude module: 'support-v4'
compile 'com.android.support:support-v4:18.0.+'
}
}
答案 0 :(得分:1)
在通过Gradle在Android Studio中添加库时,我经常会遇到同样的问题。对我有用的是运行" gradlew.bat clean assembleDebug"从项目文件夹中的控制台。