java.lang.ClassCastException:android.widget.RelativeLayout无法强制转换为android.support.v7.widget.RecyclerView

时间:2017-03-11 19:44:39

标签: android android-recyclerview onclicklistener classcastexception android-logcat

每当用户点击我的RecyclerView项目时,我都会尝试启动一项新活动。这就是我的活动看起来我的logcat显示错误

的方式
package com.example.nikit.phyprac4.ui;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;

import android.support.v7.widget.RecyclerView;

import com.example.nikit.phyprac4.R;
import com.example.nikit.phyprac4.adapter.ListAdapter;
import com.example.nikit.phyprac4.model.ListData;
import com.example.nikit.phyprac4.model.ListItem;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity  {




    private RecyclerView recyclerView;
    private ListAdapter listAdapter;
private ArrayList myData;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myData=(ArrayList) ListData.getListData();
        recyclerView=(RecyclerView)findViewById(R.id.ReList);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        listAdapter=new ListAdapter(myData,this);
        recyclerView.setAdapter(listAdapter);

    }


}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context="com.example.nikit.phyprac4.ui.MainActivity">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/ReList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >

    </android.support.v7.widget.RecyclerView>
</RelativeLayout>

logcat错误看起来像这样

03-12 00:53:17.306 9776-9776/? I/art: Late-enabling -Xcheck:jni
03-12 00:53:17.329 9776-9783/? I/art: Debugger is no longer active
03-12 00:53:17.329 9776-9783/? I/art: Starting a blocking GC Instrumentation
03-12 00:53:17.366 9776-9776/? W/System: ClassLoader referenced unknown path: /data/app/com.example.nikit.phyprac4-2/lib/arm
03-12 00:53:17.382 9776-9776/? I/InstantRun: Instant Run Runtime started. Android package is com.example.nikit.phyprac4, real application class is null.
03-12 00:53:17.944 9776-9776/? W/System: ClassLoader referenced unknown path: /data/app/com.example.nikit.phyprac4-2/lib/arm
03-12 00:53:18.123 9776-9776/? W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
03-12 00:53:18.346 9776-9776/com.example.nikit.phyprac4 D/AndroidRuntime: Shutting down VM
03-12 00:53:18.347 9776-9776/com.example.nikit.phyprac4 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                          Process: com.example.nikit.phyprac4, PID: 9776
                                                                          java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nikit.phyprac4/com.example.nikit.phyprac4.ui.MainActivity}: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.support.v7.widget.RecyclerView
                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2659)


                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724)
                                                                          at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                          at android.os.Looper.loop(Looper.java:154)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:6123)
                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
                                                                       Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.support.v7.widget.RecyclerView
                                                                          at com.example.nikit.phyprac4.ui.MainActivity.onCreate(MainActivity.java:29)
                                                                          at android.app.Activity.performCreate(Activity.java:6672)
                                                                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140)
                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2612)
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724) 
                                                                          at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473) 
                                                                          at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                          at android.os.Looper.loop(Looper.java:154) 
                                                                          at android.app.ActivityThread.main(ActivityThread.java:6123) 
                                                                          at java.lang.reflect.Method.invoke(Native Method) 
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757) 

请同时查看我的适配器

package com.example.nikit.phyprac4.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.nikit.phyprac4.R;
import com.example.nikit.phyprac4.model.ListItem;
import com.example.nikit.phyprac4.ui.FirstActivity;
import com.example.nikit.phyprac4.ui.MainActivity;
import com.example.nikit.phyprac4.ui.SecondActivity;

import java.util.List;

/**
 * Created by nikit on 11/3/17.
 */

public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ListHolder> {
    private List<ListItem> ListData;
    private LayoutInflater inflater;
    private  Context context;
    //private Button load;




    public ListAdapter(List<ListItem>ListData, Context c)
    {
        this.inflater=LayoutInflater.from(c);
        this.ListData=ListData;
    }
    @Override
    public ListHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view=inflater.inflate(R.layout.list_item,parent,false);
        return new ListHolder(view);
    }

    @Override
    public void onBindViewHolder(ListHolder holder, int position) {
        ListItem item=ListData.get(position);
        holder.title.setText(item.getTitle());
        holder.icon.setImageResource(item.getImageResId());

    }

    @Override
    public int getItemCount() {
        return ListData.size();
    }

    class ListHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        private TextView title;
        private ImageView icon;
        private View container;


        public ListHolder(View itemView) {
            super(itemView);
            title=(TextView)itemView.findViewById(R.id.lbl_item_text);
            icon=(ImageView)itemView.findViewById(R.id.im_item_icon);
            container=itemView.findViewById(R.id.cont_item_root);
            context = itemView.getContext();
            //load=(Button)itemView.findViewById(R.id.btn_card_load);
            container.setOnClickListener(this);


        }

        @Override
        public void onClick(View v) {
            if(v.getId()==R.id.cont_item_root)
            {
                final Intent intent;
                switch (getAdapterPosition()){
                    case 0:
                        intent =  new Intent(context, FirstActivity.class);
                        break;

                    case 1:
                        intent =  new Intent(context, SecondActivity.class);
                        break;

                    default:
                        intent =  new Intent(context, MainActivity.class);
                        break;
                }
                context.startActivity(intent);
            }

        }
    }

}

只要FirstActivity和SecondActivity为空,应用程序就可以正常运行,但只要我将textview放入其中就会崩溃 请帮我。我是业余开发人员。

1 个答案:

答案 0 :(得分:0)

评论此行myData =(ArrayList)ListData.getListData(); 在MainActivity解决了这个问题,当然在列表适配器中我们将传递ListData.getListData()而不是myData