如何将包装类附加到“视图”类型以外的视图

时间:2014-07-16 21:57:33

标签: java android

我想将一个新类附加到我在XML中定义的ImageView。以下代码有效,但仅适用于视图类型。当我将类型更改为ImageView,如下所示,我得到了ClassCastException。

这是我的article_view.xml文件:

<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/bg_color"
        >
<LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="600">


    <ImageView
        android:id="@+id/mybutton1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="75"
        class="com.example.customui2.MainActivity$ArticleFragment$MyButton"
        android:contentDescription="@string/logo_desc"
        android:src="@drawable/menu_button"
        android:text="@string/button1"
        android:textColor="@color/button1_color"
        android:textSize="45sp" />

</LinearLayout>

</RelativeLayout>

和这样的源文件:

public class MainActivity extends FragmentActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (findViewById(R.id.fragment_container) != null) {

            // However, if we're being restored from a previous state,
            // then we don't need to do anything and should return or else
            // we could end up with overlapping fragments.
            if (savedInstanceState != null) {
                return;
            }

            // Create a new Fragment to be placed in the activity layout
            ArticleFragment firstFragment = new ArticleFragment();

            // In case this activity was started with special instructions from an
            // Intent, pass the Intent's extras to the fragment as arguments
            firstFragment.setArguments(getIntent().getExtras());

            // Add the fragment to the 'fragment_container' FrameLayout
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragment_container, firstFragment).commit();
        }  

    }

    public static class ArticleFragment extends Fragment {

        // Animation
        Animation animFadein;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View rootView = inflater.inflate(R.layout.article_view, container, false);

            MyButton button1 = (MyButton) rootView.findViewById(R.id.mybutton1);

            // load the animation
            animFadein = AnimationUtils.loadAnimation(rootView.getContext().getApplicationContext(),
                    R.anim.fade_in);

            button1.startAnimation(animFadein);

            return rootView;
        }

        public static class MyButton extends View implements
        {
            Bitmap backgroundImage;
            Paint backgroundPaint;
            public MyButton(Context context, AttributeSet attrs) {
                super(context, attrs);
                backgroundImage = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
                backgroundPaint = new Paint();
            }

            @Override
            protected void onDraw(Canvas canvas)
            {
                canvas.drawBitmap(backgroundImage, 0, 0, backgroundPaint);
                super.onDraw(canvas);
            }

        };
    }
}

现在我在这一行收到错误ClassCastException:

MyButton button1 = (MyButton) rootView.findViewById(R.id.mybutton1);

这里是logcat:

07-16 17:35:36.855: E/AndroidRuntime(7039): FATAL EXCEPTION: main
07-16 17:35:36.855: E/AndroidRuntime(7039): Process: com.example.customui2, PID: 7039
07-16 17:35:36.855: E/AndroidRuntime(7039): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.customui2/com.example.customui2.MainActivity}: java.lang.ClassCastException: android.widget.ImageView cannot be cast to com.example.customui2.MainActivity$ArticleFragment$MyButton
07-16 17:35:36.855: E/AndroidRuntime(7039):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
07-16 17:35:36.855: E/AndroidRuntime(7039):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-16 17:35:36.855: E/AndroidRuntime(7039):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-16 17:35:36.855: E/AndroidRuntime(7039):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-16 17:35:36.855: E/AndroidRuntime(7039):     at android.os.Handler.dispatchMessage(Handler.java:102)
07-16 17:35:36.855: E/AndroidRuntime(7039):     at android.os.Looper.loop(Looper.java:136)
07-16 17:35:36.855: E/AndroidRuntime(7039):     at android.app.ActivityThread.main(ActivityThread.java:5017)
07-16 17:35:36.855: E/AndroidRuntime(7039):     at java.lang.reflect.Method.invokeNative(Native Method)
07-16 17:35:36.855: E/AndroidRuntime(7039):     at java.lang.reflect.Method.invoke(Method.java:515)
07-16 17:35:36.855: E/AndroidRuntime(7039):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-16 17:35:36.855: E/AndroidRuntime(7039):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-16 17:35:36.855: E/AndroidRuntime(7039):     at dalvik.system.NativeStart.main(Native Method)
07-16 17:35:36.855: E/AndroidRuntime(7039): Caused by: java.lang.ClassCastException: android.widget.ImageView cannot be cast to com.example.customui2.MainActivity$ArticleFragment$MyButton
07-16 17:35:36.855: E/AndroidRuntime(7039):     at com.example.customui2.MainActivity$ArticleFragment.onCreateView(MainActivity.java:110)
07-16 17:35:36.855: E/AndroidRuntime(7039):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1504)
07-16 17:35:36.855: E/AndroidRuntime(7039):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:942)
07-16 17:35:36.855: E/AndroidRuntime(7039):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
07-16 17:35:36.855: E/AndroidRuntime(7039):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
07-16 17:35:36.855: E/AndroidRuntime(7039):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
07-16 17:35:36.855: E/AndroidRuntime(7039):     at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:571)
07-16 17:35:36.855: E/AndroidRuntime(7039):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
07-16 17:35:36.855: E/AndroidRuntime(7039):     at android.app.Activity.performStart(Activity.java:5241)
07-16 17:35:36.855: E/AndroidRuntime(7039):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
07-16 17:35:36.855: E/AndroidRuntime(7039):     ... 11 more
07-16 17:35:39.895: I/Process(7039): Sending signal. PID: 7039 SIG: 9

我应该更改什么才能将类附加到视图以外的类型,如ImageView?

1 个答案:

答案 0 :(得分:2)

如果要将ImageView强制转换为ImageView,MyButton必须扩展ImageView。

如果您的MyButton类只是一个View,它缺少您尝试使用的所有ImageView组件。也许你只能使用常规的ImageView。

编辑:

您可以使用小写视图标记+ class属性在XML中引用MyButton类:

<view
    android:id="@+id/mybutton1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="75"
    class="com.example.customui2.MainActivity$ArticleFragment$MyButton"
    android:contentDescription="@string/logo_desc"
    android:src="@drawable/menu_button"
    android:text="@string/button1"
    android:textColor="@color/button1_color"
    android:textSize="45sp" />

然后声明你的静态类:

public static class MyButton extends ImageView 

我测试了它并且它没有崩溃。