java.lang.ClassCastException:android.view.ViewGroup $ LayoutParams无法强制转换为android.widget.Gallery $ LayoutParams

时间:2015-06-20 07:48:04

标签: android android-linearlayout android-relativelayout

我正在尝试在我的应用中添加Fancycoverflow,它适用于this example中给出的静态图像。

但我在Adapter中做了一些更改,我尝试运行我的应用程序,它崩溃并显示以下错误

  FATAL EXCEPTION: main
  java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.Gallery$LayoutParams
    at android.widget.Gallery.setUpChild(Gallery.java:889)
    at android.widget.Gallery.makeAndAddView(Gallery.java:858)
    at android.widget.Gallery.layout(Gallery.java:665)
    at android.widget.Gallery.onLayout(Gallery.java:357)
    at android.view.View.layout(View.java:14118)
    at android.view.ViewGroup.layout(ViewGroup.java:4467)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1670)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1528)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1441)
    at android.view.View.layout(View.java:14118)
    at android.view.ViewGroup.layout(ViewGroup.java:4467)
    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1021)
    at android.view.View.layout(View.java:14118)
    at android.view.ViewGroup.layout(ViewGroup.java:4467)
    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1021)
    at android.view.View.layout(View.java:14118)
    at android.view.ViewGroup.layout(ViewGroup.java:4467)
    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1021)
    at android.view.View.layout(View.java:14118)
    at android.view.ViewGroup.layout(ViewGroup.java:4467)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    at android.view.View.layout(View.java:14118)
    at android.view.ViewGroup.layout(ViewGroup.java:4467)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1670)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1528)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1441)
    at android.view.View.layout(View.java:14118)
    at android.view.ViewGroup.layout(ViewGroup.java:4467)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    at android.view.View.layout(View.java:14118)
    at android.view.ViewGroup.layout(ViewGroup.java:4467)
    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2183)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1947)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1139)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4872)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
    at android.view.Choreographer.doCallbacks(Choreographer.java:579)
    at android.view.Choreographer.doFrame(Choreographer.java:548)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762)
    at android.os.Handler.handleCallback(Handler.java:800)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:194)
    at android.app.ActivityThread.main(ActivityThread.java:5371)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
    at dalvik.system.NativeStart.main(Native Method)

在示例中,它以

的形式给出
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import at.technikum.mti.fancycoverflow.FancyCoverFlow;
import at.technikum.mti.fancycoverflow.FancyCoverFlowAdapter;
import at.technikum.mti.fancycoverflow.samples.R;

public class ViewGroupExample extends Activity {

    // =============================================================================
    // Supertype overrides
    // =============================================================================

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

        FancyCoverFlow fancyCoverFlow = (FancyCoverFlow) findViewById(R.id.fancyCoverFlow);
        fancyCoverFlow.setAdapter(new ViewGroupExampleAdapter());
    }

    // =============================================================================
    // Private classes
    // =============================================================================

    private static class ViewGroupExampleAdapter extends FancyCoverFlowAdapter {

        // =============================================================================
        // Private members
        // =============================================================================

        private int[] images = {R.drawable.image1, R.drawable.image2, R.drawable.image3, R.drawable.image4, R.drawable.image5, R.drawable.image6,};

        // =============================================================================
        // Supertype overrides
        // =============================================================================

        @Override
        public int getCount() {
            return images.length;
        }

        @Override
        public Integer getItem(int i) {
            return images[i];
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getCoverFlowItem(int i, View reuseableView, ViewGroup viewGroup) {
            CustomViewGroup customViewGroup = null;

            if (reuseableView != null) {
                customViewGroup = (CustomViewGroup) reuseableView;
            } else {
                customViewGroup = new CustomViewGroup(viewGroup.getContext());
                customViewGroup.setLayoutParams(new FancyCoverFlow.LayoutParams(300, 600));
            }

            customViewGroup.getImageView().setImageResource(this.getItem(i));
            customViewGroup.getTextView().setText(String.format("Item %d", i));

            return customViewGroup;
        }
    }

    private static class CustomViewGroup extends LinearLayout {

        // =============================================================================
        // Child views
        // =============================================================================

        private TextView textView;

        private ImageView imageView;

        private Button button;

        // =============================================================================
        // Constructor
        // =============================================================================

        private CustomViewGroup(Context context) {
            super(context);

            this.setOrientation(VERTICAL);

            this.textView = new TextView(context);
            this.imageView = new ImageView(context);
            this.button = new Button(context);

            LinearLayout.LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            this.textView.setLayoutParams(layoutParams);
            this.imageView.setLayoutParams(layoutParams);
            this.button.setLayoutParams(layoutParams);

            this.textView.setGravity(Gravity.CENTER);

            this.imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            this.imageView.setAdjustViewBounds(true);

            this.button.setText("Goto GitHub");
            this.button.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("https://davidschreiber.github.com/FancyCoverFlow"));
                    view.getContext().startActivity(i);
                }
            });

            this.addView(this.textView);
            this.addView(this.imageView);
            this.addView(this.button);
        }

        // =============================================================================
        // Getters
        // =============================================================================

        private TextView getTextView() {
            return textView;
        }

        private ImageView getImageView() {
            return imageView;
        }
    }
}

根据我的要求我改变的是

MyAdapter

private static class ViewGroupExampleAdapter extends FancyCoverFlowAdapter {

 private LayoutInflater inflater;
    public Activity a;
    View vi;
    public ArrayList<HashMap<String, String>> arr;
    public ArrayList<HashMap<String, String>> data;


    public ViewGroupExampleAdapter(Activity homeActivity, ArrayList<HashMap<String, String>> myList) {

        arr = myList;
        a = homeActivity;
        inflater = (LayoutInflater) a.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    // =============================================================================
    // Private members
    // =============================================================================

  //  private int[] images = {R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher,};

    // =============================================================================
    // Supertype overrides
    // =============================================================================

    @Override
    public int getCount() {
        return arr.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        System.out.println("position=" + position);
        return position;
    }

    @Override
    public View getCoverFlowItem(int i, View reuseableView, ViewGroup viewGroup) {
        View vi = reuseableView;
        if (vi == null)
            vi = inflater.inflate(R.layout.create_club_inflate, null);

        TextView date1 = (TextView) vi.findViewById(R.id.txtDate1);
        TextView date = (TextView) vi.findViewById(R.id.txtDate);
        TextView team1_name = (TextView) vi.findViewById(R.id.txtTeamName);
        TextView team2_name = (TextView) vi.findViewById(R.id.txtVanue);
        TextView ground = (TextView) vi.findViewById(R.id.txt_time);

        HashMap<String, String> product = new HashMap<String, String>();
        product = arr.get(i);

        System.out.println("name 1= " + product.get("str_team1_name") + " team 2="
                + product.get("str_team2_obj_name"));
        date1.setText(product.get("str_srs"));
        date.setText(product.get("str_startdt"));
        team1_name.setText(product.get("str_team1_name"));
        team1_name.setAlpha(5000);
        team2_name.setText(product.get("str_team2_obj_name"));
        team2_name.setAlpha(5000);
    //  Typeface font = Typeface.createFromAsset(getAssets(), "TitilliumText22L006.otf");

        int[] color = { Color.rgb(100, 100, 100), Color.rgb(255, 255, 255) };
        float[] color_position = { 0, 1 };
        TileMode tile_mode = TileMode.MIRROR; // or TileMode.REPEAT;
        LinearGradient lin_grad = new LinearGradient(0, 0, 0, 50, color, color_position, tile_mode);
        Shader shader_gradient = lin_grad;
        team1_name.getPaint().setShader(shader_gradient);
        team2_name.getPaint().setShader(shader_gradient);
        //team1_name.setTypeface(font);
    //  team2_name.setTypeface(font);
        ground.setText(product.get("str_grnd"));

        product.get("str_sName");
        product.get("str_team2_obj_sName");


        String first_team_id = product.get("str__team1_id");
        String second_team_id = product.get("str_team2_obj_id");

        switch (first_team_id) {
        case "PAK":
            team1_name.setBackgroundResource(R.drawable.pak);
            break;
        case "UAE":
            team1_name.setBackgroundResource(R.drawable.uae);
            break;
        case "AUS":
            team1_name.setBackgroundResource(R.drawable.aus);
            break;
        case "AFG":
            team1_name.setBackgroundResource(R.drawable.afg);
            break;
        case "6":
            team1_name.setBackgroundResource(R.drawable.ban);
            break;
        case "23":
            team1_name.setBackgroundResource(R.drawable.sco);
            break;
        case "2":
            team1_name.setBackgroundResource(R.drawable.ind);
            break;
        case "WI":
            team1_name.setBackgroundResource(R.drawable.wi);
            break;
        case "13":
            team1_name.setBackgroundResource(R.drawable.nz);
            break;
        case "SL":
            team1_name.setBackgroundResource(R.drawable.sl);
            break;
        case "9":
            team1_name.setBackgroundResource(R.drawable.eng);
            break;
        case "27":
            team1_name.setBackgroundResource(R.drawable.ir);
            break;
        case "11":
            team1_name.setBackgroundResource(R.drawable.rsa);
            break;
        case "ZIM":
            team1_name.setBackgroundResource(R.drawable.zim);
            break;
        case "63":
            team1_name.setBackgroundResource(R.drawable.kol_fl);
            break;
        case "62":
            team1_name.setBackgroundResource(R.drawable.mum_fl);
            break;
        case "58":
            team1_name.setBackgroundResource(R.drawable.chn_fl);
            break;
        case "61":
            team1_name.setBackgroundResource(R.drawable.del_fl);
            break;
        case "65":
            team1_name.setBackgroundResource(R.drawable.pun_fl);
            break;
        case "64":
            team1_name.setBackgroundResource(R.drawable.raj_fl);
            break;
        case "255":
            team1_name.setBackgroundResource(R.drawable.hyd_fl);
            break;
        case "59":
            team1_name.setBackgroundResource(R.drawable.blr_fl);
            break;
        default:
            team1_name.setBackgroundResource(R.drawable.otherflag);
            // h_upcoming.tv_left.setText(str1);
            break;
        }

        // second team

        switch (second_team_id) {
        case "PAK":
            team2_name.setBackgroundResource(R.drawable.pak);
            break;
        case "UAE":
            team2_name.setBackgroundResource(R.drawable.uae);
            break;
        case "AUS":
            team2_name.setBackgroundResource(R.drawable.aus);
            break;
        case "AFG":
            team2_name.setBackgroundResource(R.drawable.afg);
            break;
        case "6":
            team2_name.setBackgroundResource(R.drawable.ban);
            break;
        case "23":
            team2_name.setBackgroundResource(R.drawable.sco);
            break;
        case "2":
            team2_name.setBackgroundResource(R.drawable.ind);
            break;
        case "WI":
            team2_name.setBackgroundResource(R.drawable.wi);
            break;
        case "13":
            team2_name.setBackgroundResource(R.drawable.nz);
            break;
        case "SL":
            team2_name.setBackgroundResource(R.drawable.sl);
            break;
        case "9":
            team2_name.setBackgroundResource(R.drawable.eng);
            break;
        case "27":
            team2_name.setBackgroundResource(R.drawable.ir);
            break;
        case "11":
            team2_name.setBackgroundResource(R.drawable.rsa);
            break;
        case "ZIM":
            team2_name.setBackgroundResource(R.drawable.zim);
            break;
        case "63":
            team2_name.setBackgroundResource(R.drawable.kol_fl);
            break;
        case "62":
            team2_name.setBackgroundResource(R.drawable.mum_fl);
            break;
        case "58":
            team2_name.setBackgroundResource(R.drawable.chn_fl);
            break;
        case "61":
            team2_name.setBackgroundResource(R.drawable.del_fl);
            break;
        case "65":
            team2_name.setBackgroundResource(R.drawable.pun_fl);
            break;
        case "64":
            team2_name.setBackgroundResource(R.drawable.raj_fl);
            break;
        case "255":
            team2_name.setBackgroundResource(R.drawable.hyd_fl);
            break;
        case "59":
            team2_name.setBackgroundResource(R.drawable.blr_fl);
            break;
        default:
            team1_name.setBackgroundResource(R.drawable.otherflag);
            // h_upcoming.tv_left.setText(str1);
            break;
        }

        return vi;

    }
}

      /*  private static class CustomViewGroup extends LinearLayout {

    // =============================================================================
    // Child views
    // =============================================================================

    private TextView textView;

    private ImageView imageView;

    private Button button;

    // =============================================================================
    // Constructor
    // =============================================================================

    private CustomViewGroup(Context context) {
        super(context);

        this.setOrientation(VERTICAL);

        this.textView = new TextView(context);
        this.imageView = new ImageView(context);
        this.button = new Button(context);

        LinearLayout.LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        this.textView.setLayoutParams(layoutParams);
        this.imageView.setLayoutParams(layoutParams);
        this.button.setLayoutParams(layoutParams);

        this.textView.setGravity(Gravity.CENTER);

        this.imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        this.imageView.setAdjustViewBounds(true);

        this.button.setText("Goto GitHub");
        this.button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("https://davidschreiber.github.com/FancyCoverFlow"));
                view.getContext().startActivity(i);
            }
        });

        this.addView(this.textView);
        this.addView(this.imageView);
        this.addView(this.button);
    }

    // =============================================================================
    // Getters
    // =============================================================================

    private TextView getTextView() {
        return textView;
    }

    private ImageView getImageView() {
        return imageView;
    }
}*/

1 个答案:

答案 0 :(得分:1)

找到import android.view.ViewGroup.LayoutParams;这一行并替换import android.widget.LinearLayout.LayoutParams;这一行