我做的这个RecyclerView怎么不能正常运行呢?

建筑师 2020-03-25 09:52:01
按照google的文档编写的,写了好几次了,每次都是这样的结果。画面都看不到。


开发环境:AMD CPU+Win10+Android studio 3.6.1+Genymotion
代码下载:https://download.csdn.net/download/K8001/12268232




...全文
934 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
建筑师 2020-03-27
  • 打赏
  • 举报
回复
引用 19 楼 王能 的回复:
[quote=引用 18 楼 建筑师 的回复:] [quote=引用 17 楼 王能 的回复:] [quote=引用 16 楼 建筑师 的回复:] 这个问题我已经解决了,就是因为类型转换引起的。 inflate成员函数返回值的类型是View,View类不能通过cast转换成TextView类。 好像是这样的。昨晚试验了一下两种间互转,基转子是失败的。 但我后来又看了一篇外国人写的讲Java类型转换的文章,里面提到有upcast(子类转基类),还有downcast(这一部分没仔细看了)。还需要仔细看一下。
view是所有控件的父类,他的子类数不胜数,而你的布局里是ConstraintLayout,想转成textview当然不行。面向对象思想就是现实生活的逻辑,view比作宇宙,ConstraintLayout比作动物,textview比作白菜,你的inflate返回了一个宇宙的东西(其实是动物),你却想把他当做白菜吃,肯定不行了。[/quote] TextView v = (TextView)LayoutInflater.from(parent.getContext()).inflate(R.layout.view, parent, false); 我觉得从语法来说应该是inflate函数的返回值类型转textview,为什么你们都认为是“ConstraintLayout转textview”?[/quote] 因为R.layout.view这个布局的根view就是ConstraintLayout呀,你这是自己写的都能忘啊[/quote] 谢谢你的讲解,我再领悟一下
建筑师 2020-03-27
  • 打赏
  • 举报
回复
VC++里面有这样一种写法 CXXXApp *app =(CXXXApp *)AfxGetApp(); 其中,AfxGetApp()的返回值类型是 CWinApp * ,CXXXApp是CWinApp的子类。 这条语句就实现了基类转子类,实现方法是只要把内存边界扩大到一定位置就能得到子类,我是这么认为的。
王能 2020-03-27
  • 打赏
  • 举报
回复
引用 18 楼 建筑师 的回复:
[quote=引用 17 楼 王能 的回复:] [quote=引用 16 楼 建筑师 的回复:] 这个问题我已经解决了,就是因为类型转换引起的。 inflate成员函数返回值的类型是View,View类不能通过cast转换成TextView类。 好像是这样的。昨晚试验了一下两种间互转,基转子是失败的。 但我后来又看了一篇外国人写的讲Java类型转换的文章,里面提到有upcast(子类转基类),还有downcast(这一部分没仔细看了)。还需要仔细看一下。
view是所有控件的父类,他的子类数不胜数,而你的布局里是ConstraintLayout,想转成textview当然不行。面向对象思想就是现实生活的逻辑,view比作宇宙,ConstraintLayout比作动物,textview比作白菜,你的inflate返回了一个宇宙的东西(其实是动物),你却想把他当做白菜吃,肯定不行了。[/quote] TextView v = (TextView)LayoutInflater.from(parent.getContext()).inflate(R.layout.view, parent, false); 我觉得从语法来说应该是inflate函数的返回值类型转textview,为什么你们都认为是“ConstraintLayout转textview”?[/quote] 因为R.layout.view这个布局的根view就是ConstraintLayout呀,你这是自己写的都能忘啊
建筑师 2020-03-27
  • 打赏
  • 举报
回复
引用 17 楼 王能 的回复:
[quote=引用 16 楼 建筑师 的回复:] 这个问题我已经解决了,就是因为类型转换引起的。 inflate成员函数返回值的类型是View,View类不能通过cast转换成TextView类。 好像是这样的。昨晚试验了一下两种间互转,基转子是失败的。 但我后来又看了一篇外国人写的讲Java类型转换的文章,里面提到有upcast(子类转基类),还有downcast(这一部分没仔细看了)。还需要仔细看一下。
view是所有控件的父类,他的子类数不胜数,而你的布局里是ConstraintLayout,想转成textview当然不行。面向对象思想就是现实生活的逻辑,view比作宇宙,ConstraintLayout比作动物,textview比作白菜,你的inflate返回了一个宇宙的东西(其实是动物),你却想把他当做白菜吃,肯定不行了。[/quote] TextView v = (TextView)LayoutInflater.from(parent.getContext()).inflate(R.layout.view, parent, false); 我觉得从语法来说应该是inflate函数的返回值类型转textview,为什么你们都认为是“ConstraintLayout转textview”?
王能 2020-03-27
  • 打赏
  • 举报
回复
引用 16 楼 建筑师 的回复:
这个问题我已经解决了,就是因为类型转换引起的。 inflate成员函数返回值的类型是View,View类不能通过cast转换成TextView类。 好像是这样的。昨晚试验了一下两种间互转,基转子是失败的。 但我后来又看了一篇外国人写的讲Java类型转换的文章,里面提到有upcast(子类转基类),还有downcast(这一部分没仔细看了)。还需要仔细看一下。
view是所有控件的父类,他的子类数不胜数,而你的布局里是ConstraintLayout,想转成textview当然不行。面向对象思想就是现实生活的逻辑,view比作宇宙,ConstraintLayout比作动物,textview比作白菜,你的inflate返回了一个宇宙的东西(其实是动物),你却想把他当做白菜吃,肯定不行了。
建筑师 2020-03-27
  • 打赏
  • 举报
回复
这个问题我已经解决了,就是因为类型转换引起的。 inflate成员函数返回值的类型是View,View类不能通过cast转换成TextView类。 好像是这样的。昨晚试验了一下两种间互转,基转子是失败的。 但我后来又看了一篇外国人写的讲Java类型转换的文章,里面提到有upcast(子类转基类),还有downcast(这一部分没仔细看了)。还需要仔细看一下。
王能 2020-03-26
  • 打赏
  • 举报
回复
R.layout.view不是textview怎么能直接强转呢
建筑师 2020-03-26
  • 打赏
  • 举报
回复

package com.example.myapplication06;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    private List<String> mDataset;

    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public static class MyViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        public TextView textView;
        public MyViewHolder(TextView v) {
            super(v);
            textView =  v.findViewById(R.id.my_text_view);;
        }
    }

    // Provide a suitable constructor (depends on the kind of dataset)
    public MyAdapter(List<String> myDataset) {
        mDataset = myDataset;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        TextView v = (TextView)LayoutInflater.from(parent.getContext()).inflate(R.layout.view, parent, false);
        MyViewHolder vh = new MyViewHolder(v);
        return vh;
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        // - get element from your dataset at this position
        // - replace the contents of the view with that element
        holder.textView.setText(mDataset.get(position));

    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return mDataset.size();
    }
}
建筑师 2020-03-26
  • 打赏
  • 举报
回复

   --------- beginning of crash
03-25 22:56:32.479 2138-2138/com.example.myapplication06 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myapplication06, PID: 2138
    java.lang.ClassCastException: androidx.constraintlayout.widget.ConstraintLayout cannot be cast to android.widget.TextView
        at com.example.myapplication06.MyAdapter.onCreateViewHolder(MyAdapter.java:34)
        at com.example.myapplication06.MyAdapter.onCreateViewHolder(MyAdapter.java:10)
        at androidx.recyclerview.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:7078)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6235)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
        at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
        at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
        at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
        at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
        at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
        at android.view.View.layout(View.java:15671)
        at android.view.ViewGroup.layout(ViewGroup.java:5038)
        at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1915)
        at android.view.View.layout(View.java:15671)
        at android.view.ViewGroup.layout(ViewGroup.java:5038)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
        at android.view.View.layout(View.java:15671)
        at android.view.ViewGroup.layout(ViewGroup.java:5038)
        at androidx.appcompat.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:444)
        at android.view.View.layout(View.java:15671)
        at android.view.ViewGroup.layout(ViewGroup.java:5038)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
        at android.view.View.layout(View.java:15671)
        at android.view.ViewGroup.layout(ViewGroup.java:5038)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
        at android.view.View.layout(View.java:15671)
        at android.view.ViewGroup.layout(ViewGroup.java:5038)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
        at android.view.View.layout(View.java:15671)
        at android.view.ViewGroup.layout(ViewGroup.java:5038)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2086)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1843)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
        at android.view.Choreographer.doCallbacks(Choreographer.java:580)
        at android.view.Choreographer.doFrame(Choreographer.java:550)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
03-25 22:56:37.783 2138-2138/com.example.myapplication06 I/Process: Sending signal. PID: 2138 SIG: 9

    @Override
    public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // create a new view
        TextView v = (TextView)LayoutInflater.from(parent.getContext()).inflate(R.layout.view, parent, false);   <<<这一行是第34行
        MyViewHolder vh = new MyViewHolder(v);
        return vh;
    }
建筑师 2020-03-26
  • 打赏
  • 举报
回复
引用 9 楼 HL_GT 的回复:
报错日志发出来看看
不好意思,我是安卓开发新手,日志怎么看?
HL_GT 2020-03-26
  • 打赏
  • 举报
回复
报错日志发出来看看
建筑师 2020-03-26
  • 打赏
  • 举报
回复
引用 7 楼 Write_BUG小能手 的回复:
https://www.jianshu.com/p/c4cfe38a91ed
网上的代码很多基本都是旧版的,不是使用androidx包的。我昨天找了一个androidx的,运行成功了。我自己的这个为什么不能成功还没找到原因。
Write_BUG小能手 初级 2020-03-26
  • 打赏
  • 举报
回复
java.lang.ClassCastException: androidx.constraintlayout.widget.ConstraintLayout cannot be cast to android.widget.TextView 日志有的 类型转换异常 ConstraintLayout不能强转 成TextView
建筑师 2020-03-25
  • 打赏
  • 举报
回复
引用 5 楼 Write_BUG小能手 的回复:
[quote=引用 4 楼 建筑师 的回复:] [quote=引用 3 楼 Write_BUG小能手 的回复:] [quote=引用 2 楼 建筑师 的回复:] [quote=引用 1 楼 Write_BUG小能手 的回复:] 代码都挡住了,没法看啊, 还有 你可以那俩test包删除了
点击链接下载 https://download.csdn.net/download/K8001/12268232[/quote] MainActivity(里面的问题) layoutManager = new LinearLayoutManager(this); linearLayoutManager.setOrientation(OrientationHelper.VERTICAL);(你没有设置这个方向属性) recyclerView.setLayoutManager(layoutManager); mAdapter = new MyAdapter(this,myDataset100);(构造方法没传上下文) recyclerView.setAdapter(mAdapter); MyAdapter(里的问题) 没有声明上下文 private Context mycontext; 构造方错误,这样写 public MyAdapter(Context context,String[] myDataset) { this. mDataset = myDataset; this.mycontext=context; } 我没有运行 就这么多,你改看看先 [/quote] 1) linearLayoutManager类没有setOrientation这个方法 2)MyAdapter类的初始化函数只有一个参数 你运行看看,不麻烦的,把local.properties里面的路径改一下啊应该就可以了 [/quote] 没有方向属性 ,不可能的兄弟,必须设置方向的 ,还有构造参数 有几个是你自己写的,需要什么传什么, ,你不加Context 上下文,关联不了页面的 , 你在百度一下具体用法吧 不难的[/quote] 我看的几篇文章没一个设置了方向的
Write_BUG小能手 初级 2020-03-25
  • 打赏
  • 举报
回复
引用 4 楼 建筑师 的回复:
[quote=引用 3 楼 Write_BUG小能手 的回复:] [quote=引用 2 楼 建筑师 的回复:] [quote=引用 1 楼 Write_BUG小能手 的回复:] 代码都挡住了,没法看啊, 还有 你可以那俩test包删除了
点击链接下载 https://download.csdn.net/download/K8001/12268232[/quote] MainActivity(里面的问题) layoutManager = new LinearLayoutManager(this); linearLayoutManager.setOrientation(OrientationHelper.VERTICAL);(你没有设置这个方向属性) recyclerView.setLayoutManager(layoutManager); mAdapter = new MyAdapter(this,myDataset100);(构造方法没传上下文) recyclerView.setAdapter(mAdapter); MyAdapter(里的问题) 没有声明上下文 private Context mycontext; 构造方错误,这样写 public MyAdapter(Context context,String[] myDataset) { this. mDataset = myDataset; this.mycontext=context; } 我没有运行 就这么多,你改看看先 [/quote] 1) linearLayoutManager类没有setOrientation这个方法 2)MyAdapter类的初始化函数只有一个参数 你运行看看,不麻烦的,把local.properties里面的路径改一下啊应该就可以了 [/quote] 没有方向属性 ,不可能的兄弟,必须设置方向的 ,还有构造参数 有几个是你自己写的,需要什么传什么, ,你不加Context 上下文,关联不了页面的 , 你在百度一下具体用法吧 不难的
建筑师 2020-03-25
  • 打赏
  • 举报
回复
引用 3 楼 Write_BUG小能手 的回复:
[quote=引用 2 楼 建筑师 的回复:] [quote=引用 1 楼 Write_BUG小能手 的回复:] 代码都挡住了,没法看啊, 还有 你可以那俩test包删除了
点击链接下载 https://download.csdn.net/download/K8001/12268232[/quote] MainActivity(里面的问题) layoutManager = new LinearLayoutManager(this); linearLayoutManager.setOrientation(OrientationHelper.VERTICAL);(你没有设置这个方向属性) recyclerView.setLayoutManager(layoutManager); mAdapter = new MyAdapter(this,myDataset100);(构造方法没传上下文) recyclerView.setAdapter(mAdapter); MyAdapter(里的问题) 没有声明上下文 private Context mycontext; 构造方错误,这样写 public MyAdapter(Context context,String[] myDataset) { this. mDataset = myDataset; this.mycontext=context; } 我没有运行 就这么多,你改看看先 [/quote] 1) linearLayoutManager类没有setOrientation这个方法 2)MyAdapter类的初始化函数只有一个参数 你运行看看,不麻烦的,把local.properties里面的路径改一下啊应该就可以了
Write_BUG小能手 初级 2020-03-25
  • 打赏
  • 举报
回复
引用 2 楼 建筑师 的回复:
[quote=引用 1 楼 Write_BUG小能手 的回复:] 代码都挡住了,没法看啊, 还有 你可以那俩test包删除了
点击链接下载 https://download.csdn.net/download/K8001/12268232[/quote] MainActivity(里面的问题) layoutManager = new LinearLayoutManager(this); linearLayoutManager.setOrientation(OrientationHelper.VERTICAL);(你没有设置这个方向属性) recyclerView.setLayoutManager(layoutManager); mAdapter = new MyAdapter(this,myDataset100);(构造方法没传上下文) recyclerView.setAdapter(mAdapter); MyAdapter(里的问题) 没有声明上下文 private Context mycontext; 构造方错误,这样写 public MyAdapter(Context context,String[] myDataset) { this. mDataset = myDataset; this.mycontext=context; } 我没有运行 就这么多,你改看看先
建筑师 2020-03-25
  • 打赏
  • 举报
回复
引用 1 楼 Write_BUG小能手 的回复:
代码都挡住了,没法看啊, 还有 你可以那俩test包删除了
点击链接下载 https://download.csdn.net/download/K8001/12268232
Write_BUG小能手 初级 2020-03-25
  • 打赏
  • 举报
回复
代码都挡住了,没法看啊, 还有 你可以那俩test包删除了
Write_BUG小能手 初级 2020-03-25
  • 打赏
  • 举报
回复
https://www.jianshu.com/p/c4cfe38a91ed

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧