我最近尝试在我的Android应用程序中添加一个RecycleView,从那以后,Demo应用程序只是拒绝在模拟器上打开,所有代码编译,我无法找到我出错的区域,帖子不好相关(我认为,因为我不确定是否错误)下面的代码,但也会提供项目的下载链接,以防任何人更容易。
我知道有很多代码,但我非常难以理解这出错的地方并导致演示应用程序根本无法工作,我相当肯定它位于RecycleView内但又一次,我真的不知道是什么,所以任何非常感谢指导
下载链接https://www.dropbox.com/s/7uott0yaiktieym/JavaMini.zip?dl=0
图像列表适配器类
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class ImageListAdapter
extends RecyclerView.Adapter<ImageListAdapter.ViewHolder> {
public class ViewHolder extends RecyclerView.ViewHolder {
TextView imageTitle;
ImageView mainImage;
public ViewHolder(View itemView) {
super(itemView);
imageTitle = (TextView) itemView.findViewById(R.id.imageTitle);
mainImage = (ImageView) itemView.findViewById(R.id.mainImage);
}
}
public ArrayList<String> imageList = new ArrayList<String>();
//context
private Context context;
public ImageListAdapter(Context context) { this.context = context;}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.cell_image_card,
parent, false);
ImageListAdapter.ViewHolder vh = new
ImageListAdapter.ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
String image = imageList.get(position);
holder.imageTitle.setText(image);
}
@Override
public int getItemCount() {
return 5;
}
}
MainActivity
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private LinearLayoutManager layoutManager;
private ImageListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cell_image_card);
recyclerView = (RecyclerView) findViewById(R.id.imageList);
layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
adapter = new ImageListAdapter(this);
recyclerView.setAdapter(adapter);
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:orientation="vertical">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="@+id/imageList"
android:layout_width="361dp"
android:layout_height="484dp"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">
</android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>
</LinearLayout>