问题
I got a problem with a class I'm using from an external library. the "futuresimple/android-floating-action-button"-library to be exact. I combined it with nineoldandroids to be able to use it in Android SDK-Versions prior to 14. It seems to work (library gets built with gradle without problems). Now my problem is, that I can't initiate a FloatingActionMenu. LogCat says:
Process: com.packagename, PID: 1531
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.packagename/com.packagename.MainActivity}: android.view.InflateException: Binary XML file line #27: Error inflating class com.getbase.floatingactionbutton.FloatingActionsMenu
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #27: Error inflating class com.getbase.floatingactionbutton.FloatingActionsMenu
at android.view.LayoutInflater.createView(LayoutInflater.java:603)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
at com.packagename.MainActivity.onCreate(MainActivity.java:48)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
at java.lang.Class.getConstructorOrMethod(Class.java:472)
at java.lang.Class.getConstructor(Class.java:446)
at android.view.LayoutInflater.createView(LayoutInflater.java:568)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
at com.packagename.MainActivity.onCreate(MainActivity.java:48)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
12-26 05:06:35.7
1531-1531/com.packagename I/Process﹕ Sending signal. PID: 1531 SIG: 9
So, it seems to be a problem with the constructor, right? Here is the code (I had do do a little rework on that, too):
public FloatingActionsMenu(Context context) {
super(context, null);
init(context, null);
}
public FloatingActionsMenu(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public FloatingActionsMenu(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs);
}
public void init(Context context, AttributeSet attributeSet) {
mButtonSpacing = (int) (getResources().getDimension(R.dimen.fab_actions_spacing) - getResources().getDimension(R.dimen.fab_shadow_radius) - getResources().getDimension(R.dimen.fab_shadow_offset));
mLabelsMargin = getResources().getDimensionPixelSize(R.dimen.fab_labels_margin);
mLabelsVerticalOffset = getResources().getDimensionPixelSize(R.dimen.fab_shadow_offset);
TypedArray attr = context.obtainStyledAttributes(attributeSet, R.styleable.FloatingActionsMenu, 0, 0);
mAddButtonPlusColor = attr.getColor(R.styleable.FloatingActionsMenu_fab_addButtonPlusIconColor, getColor(android.R.color.white));
if (Build.VERSION.SDK_INT >= 14 ) {
mAddButtonColorNormal = attr.getColor(R.styleable.FloatingActionsMenu_fab_addButtonColorNormal, getColor(android.R.color.holo_blue_dark));
mAddButtonColorPressed = attr.getColor(R.styleable.FloatingActionsMenu_fab_addButtonColorPressed, getColor(android.R.color.holo_blue_light));
}else{
mAddButtonColorNormal = attr.getColor(R.styleable.FloatingActionsMenu_fab_addButtonColorNormal, getColor(R.color.holo_blue_dark));
mAddButtonColorPressed = attr.getColor(R.styleable.FloatingActionsMenu_fab_addButtonColorPressed, getColor(R.color.holo_blue_light));
}
mExpandDirection = attr.getInt(R.styleable.FloatingActionsMenu_fab_expandDirection, EXPAND_UP);
mLabelsStyle = attr.getResourceId(R.styleable.FloatingActionsMenu_fab_labelStyle, 0);
attr.recycle();
if (mLabelsStyle != 0 && expandsHorizontally()) {
throw new IllegalStateException("Action labels in horizontal expand orientation is not supported.");
}
createAddButton(context);
}
There are all three Constructors and the init-function is public. I really don't see the mistake I'm making.
Just to be sure I'm adding my build files, too:
for the library;
apply plugin: 'com.android.library'
android {
signingConfigs {
debug {
}
release {
keyAlias 'AppName'
keyPassword 'android'
storeFile file('path/to/keystore')
storePassword 'android'
}
}
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
minSdkVersion 7
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
signingConfig signingConfigs.release
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
minifyEnabled false
signingConfig signingConfigs.debug
}
}
productFlavors {
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}
dependencies {
compile files('libs/nineoldandroids-2.4.0.jar')
compile 'com.android.support:support-annotations:21.0.3'
}
for the app:
apply plugin: 'com.android.application'
android {
signingConfigs {
debug {
}
release {
keyAlias 'AppName'
keyPassword 'android'
storeFile file('path/to/keystore')
storePassword 'android'
}
}
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.packagename"
minSdkVersion 9
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
signingConfig signingConfigs.release
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
minifyEnabled false
signingConfig signingConfigs.debug
}
}
productFlavors {
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-annotations:21.0.3'
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.android.support:cardview-v7:21.0.0'
compile 'com.android.support:gridlayout-v7:21.0.0'
compile 'com.android.support:support-v4:21.0.3'
compile 'com.google.android.gms:play-services:6.5.87'
compile project(':fab')
}
...with :fab being the customized library mentioned above, declared in setting.gradle:
include ':app', ':fab'
project(':fab').projectDir = new File('libs/fab')</pre>
The structure of the library directory is like this:
fab
build
generated
intermediates
libs
nineoldandroids-2.4.0.jar
src
main
java
com.getbase.floatingactionbutton
AddFloatingActionButton
FloatingActionButton
FloatinActionsMenu
res
drawables and stuff...
AndroidManifest.xml
I hope, I didn't forget any important information.
EDIT It looks like I made something wrong with implementing the library: I just tried to initiate a single Floating Action Button but got the following build error message:
error: cannot find symbol class FloatingActionButton
I, of course, imported the class. My code looks something like this (other stuff happening and working fine not included):
package com.packagename;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.View;
import android.widget.ExpandableListView;
import com.getbase.floatingactionbutton.FloatingActionButton;
public class MainActivity extends android.support.v7.app.ActionBarActivity
{
private android.support.v7.app.ActionBarDrawerToggle mDrawerToggle;
// used to store app title
private CharSequence mTitle;
[...]
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = mDrawerTitle = getTitle();
Toolbar mToolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
[...]
FloatingActionButton button = (FloatingActionButton) findViewById(R.id.messages_fab);
button.setSize(FloatingActionButton.SIZE_MINI);
button.setColorNormalResId(R.color.half_black);
button.setColorPressedResId(R.color.half_black);
button.setIcon(R.drawable.ic_action_email);
if (savedInstanceState == null) {
}
}
[...]
}
回答1:
Okay, so I now included that library correctly, I guess. This post was very helpful, although I already found it before asking here on my own.
Here is, how I solved my first problem (Android Studio 1.0.2):
I added a library to my prject with:
file -> new module -> from existing -> "choosing the library-destination" -> finish
now I had to add the module dependency:
file -> project structure -> "click on appname" -> dependencies (on the right) -> + -> ":library" (as this was the directories name I chose)
at last I had to work on the settings.gradle file where i changed:
include ':app'
into
include ':app', ':library'
Now, it looks like the library is implemented as it should be. No errors when starting the app, no crushes and I am able to add a Floating Action Button from that library.
Now my problem is that my added Floating Action Menu doesn't show up, but that's another problem having nothing to do with that now solved problem.
回答2:
try like this:
public FloatingActionsMenu(Context context) {
super(context);
init(context, null);
}
回答3:
Binary XML file line #27: Error inflating class com.getbase.floatingactionbutton.FloatingActionsMenu
I got same problem. This error looks like from your gradle import. Try re-import then invalidate your Android studio.
回答4:
Had the same error. Please clean up you xml-file. I had some old tools:context
attributes in it.
Your xml should start like (note xmlns:fab="http://schemas.android.com/tools"
)...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:fab="http://schemas.android.com/tools">
Using the following code (from the sample) will crash my app...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:background="@color/background"
android:layout_width="match_parent"
android:layout_height="match_parent">
I guess there is no "proper" way to provide help. :-/
However, maybe it helps...
来源:https://stackoverflow.com/questions/27652990/error-inflating-class-from-library