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

2023-11-22

我正在尝试添加Fancycoverflow在我的应用程序中,它可以很好地处理静态图像,如这个例子.

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

  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;
        }
    }
}

我根据我的要求改变的是

我的适配器

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;
    }
}*/

find import android.view.ViewGroup.LayoutParams;这条线并替换为import android.widget.LinearLayout.LayoutParams;这条线

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

java.lang.ClassCastException:android.view.ViewGroup$LayoutParams 无法转换为 android.widget.Gallery$LayoutParams 的相关文章

  • 在 Android 中获取联系方式需要花费大量时间?

    您好 目前正在做一个与联系人相关的项目 我正在从联系人中获取详细信息 电子邮件 电话号码和联系人姓名 效果很好 但问题是获取联系方式需要很长时间 超过 1000 个联系人 包括从社交网站同步的联系人 这样我就放了一个Asynchronous
  • onBackPressed 仅关闭 ProgressDialog

    我意识到我的异步任务有一个小问题 我意识到 当我按 Android 设备上的后退按钮来关闭进度对话框和异步任务时 只有我的进度对话框被关闭 而我的异步任务仍在执行 我真的不知道为什么会发生这种情况 所以我只是希望有人能让我回到正确的轨道并帮
  • 如何在android中将多个图像合并为一个图像?

    我正在开发 android 的分布式应用程序 我已将单个图像分成 4 个部分 然后对其进行处理 现在我想将 4 个位图图像组合成一个图像 我怎样才能做到这一点 Bitmap parts new Bitmap 4 Bitmap result
  • 在 Android strings.xml 文件中使用 HREF

    我正在尝试从 strings xml 文件中为 TextView android text 属性分配以下字符串 我无法让链接显示为可点击的超链接 有什么建议么 我尝试过以下技术
  • 如何在状态更改时更改 Android 切换按钮的文本颜色?

    我的切换按钮对于每个状态都有不同的颜色背景 红色和白色 现在我需要在激活时更改切换按钮文本 红色 白色 的颜色 使用 xml 我无法让它工作 也许有人知道我做错了什么 我的布局 xml 中的按钮
  • 删除SD卡上的文件夹

    I tried File delete 但它不起作用 如何删除SD卡上的目录 我正在开发 Android 2 1 在删除目录本身之前 您必须将所有目录清空 请参阅here http www rgagnon com javadetails j
  • 针对 Android 开发优化 Eclipse

    我使用 Eclipse 和 ADT 插件开发 Android 而且速度 很慢 我必须经常重新启动 当我打开各种 Android 项目 当我使用库项目时需要 时 情况会变得更糟 使用 ADT 插件时 是否可以进行任何具体优化来提高 Eclip
  • 如何实现可运行队列

    我正在尝试实现一个可运行队列 在异步任务期间依次执行 意味着队列中的下一个将在另一个完成后执行 我编写了一个管理器来管理这些可运行对象和本身就是可运行对象的任务 然后 我获取异步任务中的第一个任务并运行它 希望它能够在队列中运行 但是它最终
  • Android - 对话框内VideoView的MediaController出现在对话框后面

    我有一个VideoView在自定义对话框中 我正在为VideoView即时并将其分配给VideoView在代码中 但是控制器实际上并没有出现在视频上 它出现在对话框后面 知道如何让控制器位于视频上方吗 我创建了一个静态对话框帮助器类来帮助构
  • Android“权限拒绝:无法使用相机”

    我正在学习有关在 Android 应用程序中使用相机的教程 我收到错误 权限被拒绝 无法使用相机 在模拟器和物理设备上运行调试时 我在清单文件中尝试了各种权限 似乎大多数遇到此错误的人都遇到了拼写错误 缺少权限或权限不在清单中的正确位置 这
  • Android键盘点击搜索输入时出现和消失

    我在用谷歌地图 Js API当我搜索一个地方时 我的输入搜索栏工作正常 当我通过 iPhone 设备使用它时 它也工作得很好 但是当我通过Android 设备然后键盘立即出现和消失 我已经找到了一些关于当我按下搜索栏时 android 键盘
  • 如何在android中播放内部和外部SD卡中的mp3文件?

    我正在开发一个 mp3 播放器应用程序 它可以播放内部 SD 卡内任何位置的 mp3 文件 我使用以下代码来获取内部存储中存在的 mp3 文件 ArrayList
  • 抽屉式导航不显示片段

    我创建了一个新的 Android Studio 项目 我的 MainActivity 是导航抽屉活动 所以 我无法显示碎片 我在互联网上和这里读过很多帖子 解释 我打开导航抽屉 选择菜单 播客 PodcastsFragment 应该显示 但
  • Android图层列表不显示比例可绘制项目?

    使用下面的图层列表 我的比例绘制从未显示 这是为什么 菜单 对话框 标题 xml
  • 尝试在后台使用 AsyncTask 解析 JSON 时强制关闭

    我是 Android 开发新手 正在研究 json 数据 我设法让解析工作 我想显示一个 ProgressDialog 我读到我需要使用 AsyncTask 但由于某种原因 一旦我将相同的工作代码放入 doInBackground 中 即使
  • Android smoothScrollTo 不调用 onScrollStateChanged

    我在用smoothScrollBy 滚动到 a 中的特定位置ListView 我希望在以下情况时得到通知ListView完成滚动以将其与当前集成onScrollStateChanged 当用户用手指滚动时触发的事件 目前我正在使用Timer
  • startDrag 方法 已弃用且无法编译程序

    startDrag android content ClipData android view View DragShadowBuilder java lang Object int 已弃用 如何解决这个问题而又不失去对旧版本的兼容性 还有
  • 如何强制刷新 CallLog.Calls.CACHED_NAME 列?

    我的目标是从通话记录中收集所有未知的电话号码 这可以通过以下代码来实现 private static final String CALLOG PROJECTION CallLog Calls ID CallLog Calls CACHED
  • 如何在运行时检查授予权限?

    In Android M 预览版 用户可以选择特定的应用程序并检索特定的权限 所以我问如何在运行时检查授予权限 您可以使用以下复制的代码https android googlesource com platform frameworks b
  • Android 布局以 开头 [重复]

    这个问题在这里已经有答案了 我是 Android 应用程序开发的初学者 我的问题很简单 我似乎无法确定布局文件夹中的 xml 文件是否应以以下开头 当我制作一个入门项目时 它不存在 但我也在读一本书 上面说它应该在那里 正确的方法是什么 嗯

随机推荐