我使用许多来自Internet的引用编写了这段代码,但是我不知道为什么我的ListView
在进行其他活动时不起作用。谁能帮助我找到我的代码中的问题?
这是更新 MainActivity
,我使用了Parcelable
将数据移到detailActivity
。
public class detailActivity extends AppCompatActivity {
public static final String EXTRA_MOVIE = "extra_movie";
private TextView tvJudul, tvDurasi, tvGenre,
tvRilis, tvRate, tvRatecount, tvShortdesc, tvSinopsis;
private ImageView ivPoster;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
tvJudul = findViewById(R.id.tvDetailJudul);
tvDurasi = findViewById(R.id.tvDetailDurasi);
tvGenre = findViewById(R.id.tvDetailGenre);
tvRilis = findViewById(R.id.tvDetailRilis);
tvRate = findViewById(R.id.tvDetailRate);
tvRatecount = findViewById(R.id.tvDetailRateCount);
tvShortdesc = findViewById(R.id.tvDetailShortDesc);
tvSinopsis = findViewById(R.id.tvDetailSinopsis);
ivPoster = findViewById(R.id.ivDetailPoster);
Movie movie = getIntent().getParcelableExtra(EXTRA_MOVIE);
tvJudul.setText(movie.getJudul());
tvDurasi.setText(movie.getDurasi());
tvGenre.setText(movie.getGenre());
tvRilis.setText(movie.getRilis());
tvRate.setText(movie.getRate());
tvRatecount.setText(movie.getRatecount());
tvShortdesc.setText(movie.getShortdesc());
tvSinopsis.setText(movie.getSinopsis());
}
}
这是我的detailActivity
。在此类中,我使用MainActivity
从Parcelable
获取数据。
public class detailActivity extends AppCompatActivity {
public static final String EXTRA_MOVIE = "extra_movie";
String dtJudul, dtDurasi, dtGenre, dtRilis,
dtRate, dtRateCount, dtShortDesc, dtSinopsis, dtPoster;
ViewHolder holder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
Movie movie = getIntent().getParcelableExtra(EXTRA_MOVIE);
holder.tvJudul.setText(movie.getJudul());
holder.tvDurasi.setText(movie.getDurasi());
holder.tvGenre.setText(movie.getGenre());
holder.tvRilis.setText(movie.getRilis());
holder.tvRate.setText(movie.getRate());
holder.tvRatecount.setText(movie.getRatecount());
holder.tvShortdesc.setText(movie.getShortdesc());
holder.tvSinopsis.setText(movie.getSinopsis());
}
static class ViewHolder{
private TextView tvJudul, tvDurasi, tvGenre,
tvRilis, tvRate, tvRatecount, tvShortdesc, tvSinopsis;
private ImageView ivPoster;
void ViewHolder(View view){
tvJudul = view.findViewById(R.id.tvDetailJudul);
tvDurasi = view.findViewById(R.id.tvDetailDurasi);
tvGenre = view.findViewById(R.id.tvDetailGenre);
tvRilis = view.findViewById(R.id.tvDetailRilis);
tvRate = view.findViewById(R.id.tvDetailRate);
tvRatecount = view.findViewById(R.id.tvDetailRateCount);
tvShortdesc = view.findViewById(R.id.tvDetailShortDesc);
tvSinopsis = view.findViewById(R.id.tvDetailSinopsis);
ivPoster = view.findViewById(R.id.ivDetailPoster);
}
}
}
这是MovieAdapter
public class MovieAdapter extends BaseAdapter {
//private final Context context;
private ArrayList<Movie> listMovie;
private LayoutInflater layoutInflater;
public MovieAdapter(Context context, ArrayList<Movie> listMovie){
this.listMovie = listMovie;
layoutInflater = LayoutInflater.from(context);
}
public void setMovies(ArrayList<Movie> movies ) {
this.listMovie = movies;
}
@Override
public int getCount() {
return listMovie.size();
}
@Override
public Object getItem(int position) {
return listMovie.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
ViewHolder holder;
if(view == null){
view = layoutInflater.inflate(R.layout.item_movie, null);
holder = new ViewHolder();
holder.ViewHolder(view);
view.setTag(holder);
}
else {
holder = (ViewHolder) view.getTag();
}
Glide.with(viewGroup.getContext())
.load(listMovie.get(position).getPoster())
.apply(new RequestOptions().centerCrop())
.into(holder.ivPoster);
holder.tvJudul.setText(listMovie.get(position).getJudul());
holder.tvDurasi.setText(listMovie.get(position).getDurasi());
holder.tvGenre.setText(listMovie.get(position).getGenre());
holder.tvRilis.setText(listMovie.get(position).getRilis());
holder.tvRate.setText(listMovie.get(position).getRate());
holder.tvRatecount.setText(listMovie.get(position).getRatecount());
holder.tvShortdesc.setText(listMovie.get(position).getShortdesc());
return view;
}
static class ViewHolder {
private TextView tvJudul, tvDurasi, tvGenre,
tvRilis, tvRate, tvRatecount, tvShortdesc, tvSinopsis;
private ImageView ivPoster;
void ViewHolder(View view){
tvJudul = view.findViewById(R.id.tvItemJudul);
tvDurasi = view.findViewById(R.id.tvItemDurasi);
tvGenre = view.findViewById(R.id.tvItemGenre);
tvRilis = view.findViewById(R.id.tvItemRilis);
tvRate = view.findViewById(R.id.tvItemRate);
tvRatecount = view.findViewById(R.id.tvItemRateCount);
tvShortdesc = view.findViewById(R.id.tvItemShortDesc);
ivPoster = view.findViewById(R.id.ivPItemPoster);
}
}
}
请帮助我修复它。我已经搜索了互联网并尝试了许多方法,但是没有任何效果。单击ListView
时,这是logcat中的通知。
D/ViewRootImpl@82b704[MainActivity]: ViewPostIme pointer 0
D/ViewRootImpl@82b704[MainActivity]: ViewPostIme pointer 1
D/AbsListView: onTouchUp() mTouchMode : 0
非常感谢您的回复。
答案 0 :(得分:0)
仅通过查看此代码,它不起作用。如果detailActivity活动开始,则应该立即与NPE崩溃,因为未在任何地方初始化holder变量。没有任何代码可以调用ViewHolder(view)构造函数。
Java没有未初始化的变量。没有显式初始化程序和数组元素的类和对象的字段会自动使用其类型的默认值进行初始化(布尔值为false,所有数值类型为0,所有引用类型为 null )
要么您没有显示完整的代码,要么这不起作用。