添加浮动actton按钮时XML文件中的错误

时间:2016-02-12 09:58:12

标签: android floating-action-button

activity_main3.xml

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:fab="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context="demo.demo2.Main3Activity"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">

      <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout

            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/app_bar"
            android:orientation="vertical"
            tools:context="demo.demo2.Main3Activity">
            <include
                android:id="@+id/app_bar"
                layout="@layout/app_bar" />

            <tabs.SlidingTabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <android.support.v4.view.ViewPager
                android:id="@+id/pager"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"/>


            <android.support.design.widget.FloatingActionButton
                android:id="@+id/pink_icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                fab:fab_icon="@drawable/ic_fab_star"
                fab:fab_colorNormal="@color/pink"
                fab:fab_colorPressed="@color/pink_pressed"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="16dp"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_above="@id/pink_icon"
                android:text="Text below button"
                android:layout_centerHorizontal="true"
                style="@style/menu_labels_style"
                android:layout_marginBottom="48dp"/>

            <demo.demo2.AddFloatingActionButton
                android:id="@+id/semi_transparent"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_above="@id/pink_icon"
                fab:fab_plusIconColor="@color/white"
                fab:fab_colorNormal="@color/blue_semi_transparent"
                fab:fab_colorPressed="@color/blue_semi_transparent_pressed"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="16dp"/>

            <android.support.design.widget.FloatingActionButton
                android:id="@+id/setter"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_above="@id/semi_transparent"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="16dp"/>

            <demo.demo2.AddFloatingActionButton
                android:id="@+id/normal_plus"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                fab:fab_plusIconColor="@color/half_black"
                fab:fab_colorNormal="@color/white"
                fab:fab_colorPressed="@color/white_pressed"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_marginBottom="16dp"
                android:layout_marginLeft="16dp"
                android:layout_marginStart="16dp"/>

            <demo.demo2.FloatingActionsMenu
                android:id="@+id/right_labels"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_above="@id/normal_plus"
                android:layout_marginLeft="16dp"
                android:layout_marginStart="16dp"
                fab:fab_addButtonColorNormal="@color/white"
                fab:fab_addButtonColorPressed="@color/white_pressed"
                fab:fab_addButtonPlusIconColor="@color/half_black"
                fab:fab_addButtonSize="mini"
                fab:fab_labelStyle="@style/menu_labels_style"
                fab:fab_labelsPosition="right">

                <android.support.design.widget.FloatingActionButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    fab:fab_colorNormal="@color/white"
                    fab:fab_title="Label on the right"
                    fab:fab_colorPressed="@color/white_pressed"/>

                <android.support.design.widget.FloatingActionButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    fab:fab_colorNormal="@color/white"
                    fab:fab_size="mini"
                    fab:fab_title="Another one on the right"
                    fab:fab_colorPressed="@color/white_pressed"/>

            </demo.demo2.FloatingActionsMenu>

            <demo.demo2.FloatingActionsMenu
                android:id="@+id/multiple_actions"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                fab:fab_addButtonColorNormal="@color/white"
                fab:fab_addButtonColorPressed="@color/white_pressed"
                fab:fab_addButtonPlusIconColor="@color/half_black"
                fab:fab_labelStyle="@style/menu_labels_style"
                android:layout_marginBottom="16dp"
                android:layout_marginRight="16dp"
                android:layout_marginEnd="16dp">

                <demo.demo2.FloatingActionButton
                    android:id="@+id/action_a"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    fab:fab_colorNormal="@color/white"
                    fab:fab_title="Action A"
                    fab:fab_colorPressed="@color/white_pressed"/>

                <demo.demo2.FloatingActionButton
                    android:id="@+id/action_b"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    fab:fab_colorNormal="@color/white"
                    fab:fab_title="Action with a very long name that won\'t fit on the screen"
                    fab:fab_colorPressed="@color/white_pressed"/>

            </demo.demo2.FloatingActionsMenu>

            <demo.demo2.FloatingActionsMenu
                android:id="@+id/multiple_actions_down"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:layout_alignParentTop="true"
                fab:fab_addButtonColorNormal="@color/white"
                fab:fab_addButtonColorPressed="@color/white_pressed"
                fab:fab_addButtonSize="mini"
                fab:fab_addButtonPlusIconColor="@color/half_black"
                fab:fab_expandDirection="down"
                fab:fab_labelStyle="@style/menu_labels_style"
                android:layout_marginTop="16dp"
                android:layout_marginRight="16dp"
                android:layout_marginEnd="16dp">

                <demo.demo2.FloatingActionButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    fab:fab_colorNormal="@color/white"
                    fab:fab_colorPressed="@color/white_pressed"
                    fab:fab_size="mini"/>

                <demo.demo2.FloatingActionButton
                    android:id="@+id/button_remove"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    fab:fab_colorNormal="@color/white"
                    fab:fab_colorPressed="@color/white_pressed"
                    fab:fab_title="Click to remove"/>

                <demo.demo2.FloatingActionButton
                    android:id="@+id/button_gone"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    fab:fab_colorNormal="@color/white"
                    fab:fab_colorPressed="@color/white_pressed"/>

                <demo.demo2.FloatingActionButton
                    android:id="@+id/action_enable"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    fab:fab_colorNormal="@color/white"
                    fab:fab_title="Set bottom menu enabled/disabled"
                    fab:fab_colorPressed="@color/white_pressed"/>

            </demo.demo2.FloatingActionsMenu>

            <demo.demo2.FloatingActionsMenu
                android:id="@+id/multiple_actions_left"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toLeftOf="@+id/multiple_actions_down"
                android:layout_toStartOf="@+id/multiple_actions_down"
                android:layout_alignParentTop="true"
                fab:fab_addButtonColorNormal="@color/white"
                fab:fab_addButtonColorPressed="@color/white_pressed"
                fab:fab_addButtonSize="mini"
                fab:fab_addButtonPlusIconColor="@color/half_black"
                fab:fab_addButtonStrokeVisible="false"
                fab:fab_expandDirection="left"
                android:layout_marginTop="16dp"
                android:layout_marginRight="16dp"
                android:layout_marginEnd="16dp">

                <demo.demo2.FloatingActionButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    fab:fab_colorNormal="@color/white"
                    fab:fab_colorPressed="@color/white_pressed"/>

                <demo.demo2.FloatingActionButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    fab:fab_colorNormal="@color/white"
                    fab:fab_colorPressed="@color/white_pressed"
                    fab:fab_size="mini"/>

                <demo.demo2.FloatingActionButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:visibility="gone"
                    fab:fab_colorNormal="@color/white"
                    fab:fab_colorPressed="@color/white_pressed"
                    fab:fab_size="mini"/>

            </demo.demo2.FloatingActionsMenu>

            <demo.demo2.AddFloatingActionButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/setter_drawable"
                android:layout_above="@id/setter"
                android:layout_centerHorizontal="true"/>


        </LinearLayout>
        <fragment
            android:id="@+id/fragment_navigation_drawer"
            android:layout_width="@dimen/nav_drawer_width"
            android:layout_height="match_parent"
            app:layout="@layout/fragment_navigation_drawer"
            android:layout_gravity="start"
            android:name="demo.demo2.NavigationDrawerFragment"
            tools:layout="@layout/fragment_navigation_drawer" />
    </android.support.v4.widget.DrawerLayout>
</LinearLayout>

Main3Activity.java

这是主要的活动,我没有在这里包含浮动操作按钮java文件,但在我的项目中它们都在同一个包demo.demo2

        package demo.demo2;

        import android.app.Activity;
        import android.content.Context;
        import android.support.annotation.Nullable;
        import android.support.v4.app.Fragment;
        import android.support.v4.app.FragmentManager;
        import android.support.v4.app.FragmentPagerAdapter;
        import android.support.v4.view.ViewPager;
        import android.support.v4.widget.DrawerLayout;
        import android.support.v7.app.AppCompatActivity;
        import android.os.Bundle;
        import android.support.v7.widget.Toolbar;
        import android.view.LayoutInflater;
        import android.view.TextureView;
        import android.view.View;
        import android.view.ViewGroup;
        import android.widget.TextView;
        import demo.demo2.FloatingActionButton;
        import demo.demo2.FloatingActionsMenu;
        import android.graphics.drawable.ShapeDrawable;
        import android.graphics.drawable.shapes.OvalShape;
        import android.os.Bundle;
        import android.view.View;
        import android.view.View.OnClickListener;
        import android.widget.Toast;

        import tabs.SlidingTabLayout;

        public class Main3Activity extends AppCompatActivity {
            private ViewPager mPager;
            private Toolbar toolbar;
            Activity activity;

            private SlidingTabLayout mTabs;

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main3);

                Toolbar toolbar= (Toolbar) findViewById(R.id.app_bar);
                setSupportActionBar(toolbar);
               getSupportActionBar().setHomeButtonEnabled(true);
               getSupportActionBar().setDisplayHomeAsUpEnabled(true);

                NavigationDrawerFragment drawerFragment=(NavigationDrawerFragment)getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);

                drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);


                mPager = (ViewPager) findViewById(R.id.pager);

                mPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
                mTabs = (SlidingTabLayout) findViewById(R.id.tabs);
            mTabs.setViewPager(mPager);

                findViewById(R.id.pink_icon).setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(Main3Activity.this, "Clicked pink Floating Action Button", Toast.LENGTH_SHORT).show();
                    }
                });

                FloatingActionButton button = (FloatingActionButton) findViewById(R.id.setter);
                button.setSize(FloatingActionButton.SIZE_MINI);
                button.setColorNormalResId(R.color.pink);
                button.setColorPressedResId(R.color.pink_pressed);
                button.setIcon(R.drawable.ic_fab_star);
                button.setStrokeVisible(false);

                final View actionB = findViewById(R.id.action_b);

                FloatingActionButton actionC = new FloatingActionButton(getBaseContext());
                actionC.setTitle("Hide/Show Action above");
                actionC.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        actionB.setVisibility(actionB.getVisibility() == View.GONE ? View.VISIBLE : View.GONE);
                    }
                });

                final FloatingActionsMenu menuMultipleActions = (FloatingActionsMenu) findViewById(R.id.multiple_actions);
                menuMultipleActions.addButton(actionC);

                final FloatingActionButton removeAction = (FloatingActionButton) findViewById(R.id.button_remove);
                removeAction.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ((FloatingActionsMenu) findViewById(R.id.multiple_actions_down)).removeButton(removeAction);
                    }
                });

                ShapeDrawable drawable = new ShapeDrawable(new OvalShape());
                drawable.getPaint().setColor(getResources().getColor(R.color.white));
                ((FloatingActionButton) findViewById(R.id.setter_drawable)).setIconDrawable(drawable);

                final FloatingActionButton actionA = (FloatingActionButton) findViewById(R.id.action_a);
                actionA.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        actionA.setTitle("Action A clicked");
                    }
                });

                // Test that FAMs containing FABs with visibility GONE do not cause crashes
                findViewById(R.id.button_gone).setVisibility(View.GONE);

                final FloatingActionButton actionEnable = (FloatingActionButton) findViewById(R.id.action_enable);
                actionEnable.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        menuMultipleActions.setEnabled(!menuMultipleActions.isEnabled());
                    }
                });

                FloatingActionsMenu rightLabels = (FloatingActionsMenu) findViewById(R.id.right_labels);
                FloatingActionButton addedOnce = new FloatingActionButton(this);
                addedOnce.setTitle("Added once");
                rightLabels.addButton(addedOnce);

                FloatingActionButton addedTwice = new FloatingActionButton(this);
                addedTwice.setTitle("Added twice");
                rightLabels.addButton(addedTwice);
                rightLabels.removeButton(addedTwice);
                rightLabels.addButton(addedTwice);
            }
            }

MyPagerAdapter

class MyPagerAdapter extends FragmentPagerAdapter {
        String[] tabs{"ALL REMINDER","TO DO","BILLS"};
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);

        }

        @Override
        public Fragment getItem(int position) {
            MyFragment myFragment=MyFragment.getInstance(position);
            return myFragment;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return tabs[position];
        }

        @Override
        public int getCount() {
            return 6;
        }
    }

     class MyFragment extends Fragment {
        private TextView textView;
        public static MyFragment getInstance(int position) {

            MyFragment myFragment = new MyFragment();
            Bundle args=new Bundle();
            args.putInt("position",position);
            myFragment.setArguments(args);
            return myFragment;

        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View layout=inflater.inflate(R.layout.fragment_my,container,false);
            textView=(TextView)layout.findViewById(R.id.position);
            Bundle bundle=getArguments();
            if (bundle!=null)
            {
                textView.setText("The Page Selected is"  +bundle.getInt("position"));
            }
            return layout;
        }
        }

这是提到的堆栈跟踪

The following classes could not be instantiated:
    - demo.demo2.AddFloatingActionButton (Open Class, Show Exception, Clear Cache)
    - android.support.design.widget.FloatingActionButton (Open Class, Show Exception, Clear Cache)
    - demo.demo2.FloatingActionsMenu (Open Class, Show Exception, Clear Cache)
    - demo.demo2.FloatingActionButton (Open Class, Show Exception, Clear Cache)

Tip: Use View.isInEditMode() in your custom views to skip code or show sample data when shown in the IDE  Exception Details java.lang.NoClassDefFoundError: demo/demo2/R$styleable   
 at demo.demo2.AddFloatingActionButton.init(AddFloatingActionButton.java:35)     
 at demo.demo2.FloatingActionButton.<init>(FloatingActionButton.java:69)      
 at demo.demo2.AddFloatingActionButton.<init>(AddFloatingActionButton.java:26)      
 at java.lang.reflect.Constructor.newInstance(Constructor.java:526)   
 at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)     
 at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:835)     
 at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:70)      
 at android.view.LayoutInflater.rInflate(LayoutInflater.java:811)   
 at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)     
 at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:838)     
 at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:70)     
 at android.view.LayoutInflater.rInflate(LayoutInflater.java:811)   
 at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)     
 at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:838)     
 at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:70)     
 at android.view.LayoutInflater.rInflate(LayoutInflater.java:811)   
 at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)     
 at android.view.LayoutInflater.inflate(LayoutInflater.java:515)   
 at android.view.LayoutInflater.inflate(LayoutInflater.java:394)

我是android编程的新手。请帮我检测错误,以及如何解决错误。

AddFloatingActionButton.java

    import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.Shape;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.util.AttributeSet;

public class AddFloatingActionButton extends FloatingActionButton {
    int mPlusColor;

    public AddFloatingActionButton(Context context) {
        this(context, null);
    }

    public AddFloatingActionButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AddFloatingActionButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    void init(Context context, AttributeSet attributeSet) {
        TypedArray attr = context.obtainStyledAttributes(attributeSet, R.styleable.AddFloatingActionButton, 0, 0);
        mPlusColor = attr.getColor(R.styleable.AddFloatingActionButton_fab_plusIconColor, getColor(android.R.color.white));
        attr.recycle();

        super.init(context, attributeSet);
    }

    /**
     * @return the current Color of plus icon.
     */
    public int getPlusColor() {
        return mPlusColor;
    }

    public void setPlusColorResId(@ColorRes int plusColor) {
        setPlusColor(getColor(plusColor));
    }

    public void setPlusColor(int color) {
        if (mPlusColor != color) {
            mPlusColor = color;
            updateBackground();
        }
    }

    @Override
    public void setIcon(@DrawableRes int icon) {
        throw new UnsupportedOperationException("Use FloatingActionButton if you want to use custom icon");
    }

    @Override
    Drawable getIconDrawable() {
        final float iconSize = getDimension(R.dimen.fab_icon_size);
        final float iconHalfSize = iconSize / 2f;

        final float plusSize = getDimension(R.dimen.fab_plus_icon_size);
        final float plusHalfStroke = getDimension(R.dimen.fab_plus_icon_stroke) / 2f;
        final float plusOffset = (iconSize - plusSize) / 2f;

        final Shape shape = new Shape() {
            @Override
            public void draw(Canvas canvas, Paint paint) {
                canvas.drawRect(plusOffset, iconHalfSize - plusHalfStroke, iconSize - plusOffset, iconHalfSize + plusHalfStroke, paint);
                canvas.drawRect(iconHalfSize - plusHalfStroke, plusOffset, iconHalfSize + plusHalfStroke, iconSize - plusOffset, paint);
            }
        };

        ShapeDrawable drawable = new ShapeDrawable(shape);

        final Paint paint = drawable.getPaint();
        paint.setColor(mPlusColor);
        paint.setStyle(Style.FILL);
        paint.setAntiAlias(true);

        return drawable;
    }
}

Screenshot of the prevailing error

2 个答案:

答案 0 :(得分:0)

错误说

  

异常详细信息java.lang.NoClassDefFoundError:demo / demo2 / R $ styleable

at

  

demo.demo2.AddFloatingActionButton.init(AddFloatingActionButton.java:35)

这意味着AddFloatingActionButton.java类无法找到任何类名R.styleable

如果您在代码中定义了属性R.styleable.AddFloatingActionButtonR.styleable.AddFloatingActionButton_fab_plusIconColor,那么请导入R.java

import demo.demo2.R; // user your package name

但如果您没有定义,那么您应该导入此属性所在的正确包名。

答案 1 :(得分:0)

尝试使用gradle.build。

您显示的堆栈跟踪在设计视图中是否正确? 尝试构建并运行代码。

编辑: 对于上下文,您需要从MainActivity传递上下文,如

mPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager(),this)); 

并且在FragmentAdapter类中,您必须向参数添加上下文,然后将该上下文与getResources一起使用:

 String[] tabs;
 Context context;
    public MyPagerAdapter(FragmentManager fm,Context context) {
        super(fm);
        this.context = context; //(only if you are going to use context further)
         tabs = context.getResources().....

    }

EDIT2:

解决新错误

在具有id&#34; @ + id / right_labels&#34;的FloatingActionsMenu中,您已从设计库中提供了两个FloatingActionButtons。将它们更改为您的自定义demo.demo2.FloatingActionButton