从库中扩充类时出错

时间:2014-12-26 05:00:35

标签: android android-studio android-custom-view android-gradle

我遇到了一个我正在使用外部库的类的问题。确切地说,“futuresimple / android-floating-action-button”-library。 我将它与九个机器人结合起来,以便能够在Android SDK-14之前的版本中使用它。它似乎工作(库使用gradle构建而没有问题)。 现在我的问题是,我无法启动FloatingActionMenu。 LogCat说:

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

所以,它似乎是构造函数的问题,对吧? 这是代码(我也做了一些关于它的修改):

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);
  }

有三个构造函数,init函数是公共的。我真的没有看到我正在犯的错误。

为了确保我也在添加我的构建文件:

为图书馆;

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'
}

适用于该应用:

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是上面提到的自定义库,在setting.gradle中声明:

include ':app', ':fab'
project(':fab').projectDir = new File('libs/fab')</pre>

库目录的结构如下:

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

我希望,我没有忘记任何重要信息。

修改 看起来我在实现库时犯了一些错误:我只是尝试启动一个浮动操作按钮但得到以下构建错误消息:

error: cannot find symbol class FloatingActionButton

我当然是导入了这门课程。我的代码看起来像这样(其他东西发生并且工作正常,不包括在内):

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) {

        }
    }

    [...]
}

4 个答案:

答案 0 :(得分:1)

好的,所以我现在正确地包含了这个库,我想。 This帖子非常有帮助,虽然我已经在我自己询问之前就找到了它。

这是我如何解决我的第一个问题(Android Studio 1.0.2):

  • 我在我的项目中添加了一个库:

    file - &gt;新模块 - &gt;来自现有的 - &gt; “选择图书馆目的地” - &gt;完成

  • 现在我不得不添加模块依赖项:

    file - &gt;项目结构 - &gt; “点击appname” - &gt;依赖(右侧) - &gt; + - &gt; “:library”(因为这是我选择的目录名称)

  • 最后我不得不处理我更改的settings.gradle文件:

    include ':app'

进入

include ':app', ':library'

现在,它看起来像是应该实现的。启动应用程序时没有错误,没有压缩,我可以从该库添加浮动操作按钮。

现在我的问题是我添加的浮动操作菜单没有显示,但这是另一个与现在解决的问题无关的问题。

答案 1 :(得分:0)

尝试这样:

public FloatingActionsMenu(Context context) {
      super(context);
      init(context, null);

  }

答案 2 :(得分:0)

Binary XML file line #27: Error inflating class com.getbase.floatingactionbutton.FloatingActionsMenu

我遇到了同样的问题。此错误类似于您的gradle导入。尝试重新导入然后使您的Android工作室无效。

答案 3 :(得分:0)

有同样的错误。请清理你的xml文件。我有一些旧的tools:context属性。

你的xml应该像(注意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">

使用以下代码(来自sample会崩溃我的应用...

<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">

我猜没有“正确”的方式来提供帮助。 : - /

然而,也许它有帮助......