来自支持库的FloatingActionButton会导致异常。为什么?

时间:2017-01-27 19:26:03

标签: android android-support-library floating-action-button

我有以下布局activity_fab.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:elevation="20dp"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <android.support.design.widget.CoordinatorLayout
    android:id="@+id/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


        <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_marginBottom="10dp"
        android:layout_marginRight="10dp"
        android:src="@drawable/ic_info_outline_white_36dp" />

</android.support.design.widget.CoordinatorLayout>

</LinearLayout>

我的ActivityFab.java

package com.example.testandroidsupportv7;

import com.example.testandroidsupportv7.R;

import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class ActivityFab extends AppCompatActivity
{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setTheme(R.style.MyTheme_NoActionBar);
        setContentView(R.layout.activity_fab);
        Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        // method not found toolbar.setElevation(25);
        getSupportActionBar().setElevation(40);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

这是一个非常简单的例子。但是,它仍然与logcat的以下输出崩溃:

01-27 20:09:52.184: E/AndroidRuntime(4067): FATAL EXCEPTION: main
01-27 20:09:52.184: E/AndroidRuntime(4067): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testandroidsupportv7/com.example.testandroidsupportv7.ActivityFab}: android.view.InflateException: Binary XML file line #28: Error inflating class android.support.design.widget.FloatingActionButton
01-27 20:09:52.184: E/AndroidRuntime(4067):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
01-27 20:09:52.184: E/AndroidRuntime(4067):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
01-27 20:09:52.184: E/AndroidRuntime(4067):     at android.app.ActivityThread.access$700(ActivityThread.java:140)
01-27 20:09:52.184: E/AndroidRuntime(4067):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
01-27 20:09:52.184: E/AndroidRuntime(4067):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-27 20:09:52.184: E/AndroidRuntime(4067):     at android.os.Looper.loop(Looper.java:137)
01-27 20:09:52.184: E/AndroidRuntime(4067):     at android.app.ActivityThread.main(ActivityThread.java:4921)
01-27 20:09:52.184: E/AndroidRuntime(4067):     at java.lang.reflect.Method.invokeNative(Native Method)
01-27 20:09:52.184: E/AndroidRuntime(4067):     at java.lang.reflect.Method.invoke(Method.java:511)
01-27 20:09:52.184: E/AndroidRuntime(4067):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
01-27 20:09:52.184: E/AndroidRuntime(4067):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
01-27 20:09:52.184: E/AndroidRuntime(4067):     at dalvik.system.NativeStart.main(Native Method)
01-27 20:09:52.184: E/AndroidRuntime(4067): Caused by: android.view.InflateException: Binary XML file line #28: Error inflating class android.support.design.widget.FloatingActionButton
01-27 20:09:52.184: E/AndroidRuntime(4067):     at android.view.LayoutInflater.createView(LayoutInflater.java:613)
01-27 20:09:52.184: E/AndroidRuntime(4067):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
01-27 20:09:52.184: E/AndroidRuntime(4067):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
01-27 20:09:52.184: E/AndroidRuntime(4067):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
01-27 20:09:52.184: E/AndroidRuntime(4067):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
01-27 20:09:52.184: E/AndroidRuntime(4067):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-27 20:09:52.184: E/AndroidRuntime(4067):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
01-27 20:09:52.184: E/AndroidRuntime(4067):     at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
01-27 20:09:52.184: E/AndroidRuntime(4067):     at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
01-27 20:09:52.184: E/AndroidRuntime(4067):     at com.example.testandroidsupportv7.ActivityFab.onCreate(ActivityFab.java:18)
01-27 20:09:52.184: E/AndroidRuntime(4067):     at android.app.Activity.performCreate(Activity.java:5206)
01-27 20:09:52.184: E/AndroidRuntime(4067):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
01-27 20:09:52.184: E/AndroidRuntime(4067):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
01-27 20:09:52.184: E/AndroidRuntime(4067):     ... 11 more
01-27 20:09:52.184: E/AndroidRuntime(4067): Caused by: java.lang.reflect.InvocationTargetException
01-27 20:09:52.184: E/AndroidRuntime(4067):     at java.lang.reflect.Constructor.constructNative(Native Method)
01-27 20:09:52.184: E/AndroidRuntime(4067):     at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
01-27 20:09:52.184: E/AndroidRuntime(4067):     at android.view.LayoutInflater.createView(LayoutInflater.java:587)
01-27 20:09:52.184: E/AndroidRuntime(4067):     ... 23 more
01-27 20:09:52.184: E/AndroidRuntime(4067): Caused by: java.lang.IllegalAccessError: tried to access class android.support.v7.widget.AppCompatImageHelper from class android.support.design.widget.FloatingActionButton
01-27 20:09:52.184: E/AndroidRuntime(4067):     at android.support.design.widget.FloatingActionButton.<init>(FloatingActionButton.java:133)
01-27 20:09:52.184: E/AndroidRuntime(4067):     at android.support.design.widget.FloatingActionButton.<init>(FloatingActionButton.java:110)
01-27 20:09:52.184: E/AndroidRuntime(4067):     ... 26 more

从我所看到的问题出现在:

Caused by: java.lang.IllegalAccessError: tried to access class android.support.v7.widget.AppCompatImageHelper from class android.support.design.widget.FloatingActionButton
01-27 20:09:52.184: E/AndroidRuntime(4067):     at android.support.design.widget.FloatingActionButton.<init>(FloatingActionButton.java:133)
01-27 20:09:52.184: E/AndroidRuntime(4067):     at android.support.design.widget.FloatingActionButton.<init>(FloatingActionButton.java:110)

如果我尝试使用

将此类导入代码中
import android.support.v7.widget.AppCompatImageHelper;

我不能,因为类型不可见。这是否意味着我的suuport库(23.0.1)版本的访问修饰符有问题? 为什么会发生这种错误?怎么解决?

编辑: 在流行的需求上,我也发布了我的风格和主题。

<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.

    -->
    <style name="MyThemeActionBar" parent="Theme.AppCompat.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.

        -->
    </style>

    <style name="MyTheme.NoActionBar" parent="Theme.AppCompat.Light.NoActionBar">

        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.

        -->
        <item name="listChoiceBackgroundIndicator">@drawable/selector_selectable_background</item>
        <item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:spinnerItemStyle">@style/SpinnerItem</item>
        <item name="android:spinnerDropDownItemStyle">@style/SpinnerItem</item>
    </style>

    <!-- styles -->


    <!-- textColor, textColorHint za EditText NE GRE NASTAVIT v android:textAppearance -->
    <style name="EditText" parent="">
        <item name="android:textAppearance">@style/TextAppearance.EditText</item>
        <item name="android:textColor">@color/selector_input_text_light</item>
        <item name="android:textColorHint">@color/selector_input_hint_light</item>
    </style>

    <style name="ProgressBar" parent="Widget.AppCompat.ProgressBar">
        <item name="android:minWidth">10dp</item>
        <item name="android:maxWidth">80dp</item>
        <item name="android:minHeight">10dp</item>
        <item name="android:maxHeight">80dp</item>
    </style>

    <style name="SpinnerItem" parent="Widget.AppCompat.TextView.SpinnerItem">
        <item name="android:textAppearance">@style/TextAppearance.SpinnerItem</item>
    </style>

    <style name="TextInputLayout" parent="">
        <item name="android:textColorHint">@color/selector_input_hint_light</item>
    </style>

    <style name="Test" parent="TextAppearance.AppCompat">
        <item name="android:textColorHint">?colorAccent</item>
        <item name="android:textSize">14dp</item>
        <item name="colorAccent">?colorAccent</item>
        <item name="colorControlNormal">?colorAccent</item>
        <item name="colorControlActivated">?colorAccent</item>
    </style>

    <!-- Text Appearance -->
    <!-- textColor, textColorHint za EditText cannot be set in android:textAppearance -->
    <style name="TextAppearance.EditText" parent="">
        <item name="android:textSize">18sp</item>
    </style>

    <style name="TextAppearance.SimpleColor" parent="">
        <item name="android:textColor">#aa0000</item>
    </style>

    <style name="TextAppearance.SpinnerItem" parent="">
        <item name="android:textColor">#aa0000</item>
        <item name="android:textSize">40sp</item>
    </style>

</resources>

更新:

我在android.googlesource.com搜索了AppCompatImage助手类的源代码。类定义以:

开头
    .
    .
    .
import static android.support.annotation.RestrictTo.Scope.GROUP_ID;
/**
 * @hide
 */
@RestrictTo(GROUP_ID)
public class AppCompatImageHelper {
    private final ImageView mView;
    . 
    .
    .

这个@RestrictTo(GROUP_ID)注释是否有可能限制类的可见性? 一个子问题出现了吗?如何查找特定版本的支持库的源代码?

0 个答案:

没有答案