本文介绍了ClassCastException:android.app.Application的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

导致错误的类如下:

package com.extrasmart;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
//import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemSelectedListener;

public class ActivityImgGrid extends Activity {

    ExtraSmartApplication application = (ExtraSmartApplication) getApplication();

    GridView mGrid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activityimg_grid);
        mGrid = (GridView) findViewById(R.id.imgGrid);
        mGrid.setAdapter(new AppsAdapter());
        mGrid.setOnItemSelectedListener(new OnItemSelectedListener()
        {
            public void onNothingSelected(AdapterView v) {
                // TODO Auto-generated method stub

            }

            public void onItemSelected(AdapterView parent, View v, int position, long id) {
                ExtraSmartApplication application = (ExtraSmartApplication) getApplication();

                application.setSelCategoryIcon(position);
                setResult(RESULT_OK);
                finish();

            }

        });

    }

    public class AppsAdapter extends BaseAdapter {
        ExtraSmartApplication application = (ExtraSmartApplication) getApplication();
        private Integer[] imageIDs = application.getCategoryIcons();

        public AppsAdapter() {
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView i;

            if (convertView == null) {
                i = new ImageView(ActivityImgGrid.this);
                //i.setScaleType(ImageView.ScaleType.FIT_CENTER);
                i.setLayoutParams(new GridView.LayoutParams(48, 48));
                i.setScaleType(ImageView.ScaleType.CENTER_CROP);
                i.setPadding(5, 5, 5, 5);
            } else {
                i = (ImageView) convertView;
            }

            i.setImageResource(imageIDs[position]);
            return i;
       }


        public final int getCount() {
            return imageIDs.length;
        }

        public final Object getItem(int position) {
            return imageIDs[position];
        }

        public final long getItemId(int position) {
            return position;
        }
    }

}

xml文件:

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/imgGrid"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:numColumns="auto_fit"
    android:columnWidth="48px"
    android:stretchMode="columnWidth"
    android:gravity="center"
    android:minHeight="48px"
    android:minWidth="48px"
    />

ActivityImgGrid类使用的类:

Class used by ActivityImgGrid class:

public class ExtraSmartApplication extends Application {

    //private static final String APP_CREDENTIALS = null;
    private Integer selCategoryImg;
    private boolean isCardImageNew = false;
    private byte[] cardImage = null;

    private Integer[] categoryImgs = {
            R.drawable.cricket,
            R.drawable.football,
            R.drawable.vollyball
    };

    private boolean isCatCreated = false;

    public ExtraSmartApplication() {
        super();
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
    }


    public Integer getSelCategoryIcon() {
        return this.selCategoryImg;
    }

    public Integer[] getCategoryIcons() {
        return this.categoryImgs;
    }

    public void setSelCategoryIcon(int position) {
        this.selCategoryImg = categoryImgs[position];
    }

    public void setCatCreated(boolean isCatCreated) {
        this.isCatCreated = isCatCreated;
    }

    public boolean isCatCreated() {
        return isCatCreated;
    }

    public void setCardImageNew(boolean isCardImageNew) {
        this.isCardImageNew = isCardImageNew;
    }

    public boolean isCardImageNew() {
        return isCardImageNew;
    }

    public void setCardImage(byte[] cardImage) {
        this.cardImage = cardImage;
    }

    public byte[] getCardImage() {
        return cardImage;
    }
}

推荐答案

如果您可以共享一些代码段,那就更好了……

It would be better if u can share some code snippets...

无论如何,就 ClassCastException 而言,这意味着您正在声明某种类型的变量,并将其分配给在布局xml文件中定义的另一种类型...

Anyways, as far as ClassCastException is concerned, it means you are declaring a variable of some type and assigning it to another type you have defined in a layout xml file...

例如,在xml中,您可能已经拥有:

for example,in the xml you may have had:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_height="wrap_content"
android:id="@+id/btn1"
android:layout_width="wrap_content">
</Button>
</LinearLayout>

但是在将组件连接到代码时:

but while connecting the component to code:

ImageView img1 = (ImageView)context.findViewById(R.id.btn1);

这将触发 ClassCastException bcoz,您正在将Button投射到ImageView变量,据您所知,这是不可能的!

This will fire a ClassCastException bcoz you are casting a Button to an ImageView variable which is as u understand not possible!

如果这不能解决您的问题,那么最好在找出导致错误的代码段后发布一些代码段!

If this doesnt solve your problem then it'll be better if u post some code snippets after figuring out which code snippet causes the error!

这篇关于ClassCastException:android.app.Application的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-08 21:25