gpt4 book ai didi

java - 获取回收器 View 位置给出 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 06:07:04 24 4
gpt4 key购买 nike

将 value 转换为 int 时,出现空指针异常。这是我收到的错误

java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference

这是我的代码

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location_reminder_list);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
dbAdapter=new LocationReminderDBAdapter(getApplicationContext());
dbAdapter.open();
/////////////////////////////////////////////////////////////////////////////////////
mRecyclerView = (RecyclerView)findViewById(R.id.reminders_recycler);

int[] locationImage = new int[5];
for (int i = 0; i < locationImage.length; i++) {
locationImage[i] = i;
}
reminderNames = new ArrayList<String>();
reminderLocations = new ArrayList<String>();
imagePaths=new ArrayList<String>();
mLayoutManager = new LinearLayoutManager(getApplicationContext());
mRecyclerView.setLayoutManager(mLayoutManager);
adapter = new SavedReminderAdapter(reminderNames, reminderLocations, imagePaths);
mRecyclerView.setAdapter(adapter);
populateReminderList();

adapter.setListener(new SavedReminderAdapter.Listener() {
@Override
public void onClick(int position) {
Intent intent=new Intent(getApplicationContext(),ViewAddedReminder.class);
startActivity(intent);
}


});
//////////////////////////////////////////////////////////////////////////////////////
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(LocationReminderList.this,AddLocationReminder.class);
startActivity(intent);
/* Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();*/
}
});


}

这里我收到空指针异常

public void editReminder(View view){
int itemPosition =(int)view.getTag(R.id.edit_reminder);

}

这是我的适配器代码

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

/**
* Created by sasikanth on 23-01-2017.
*/

public class SavedReminderAdapter extends RecyclerView.Adapter<SavedReminderAdapter.ViewHolder> {
private ArrayList<String> reminders;
private ArrayList<String> locations;
private ArrayList<String> imageIds;
private Listener listener;

@Override
public SavedReminderAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
CardView cv=(CardView) LayoutInflater.from(parent.getContext()).inflate(R.layout.reminder_adapter,parent,false);
return new ViewHolder(cv);
}
public static interface Listener{
public void onClick(int position);

/*public void setAlarm(int position);

public void editReminder(int position);

public void settaskCompleted(int position);*/
}

public SavedReminderAdapter(ArrayList<String> reminders, ArrayList<String> locations, ArrayList<String> imageIds) {
this.reminders = reminders;
this.locations = locations;
this.imageIds = imageIds;
}

public void setListener(Listener listener){
this.listener=listener;
}


@Override
public void onBindViewHolder(SavedReminderAdapter.ViewHolder holder, final int position) {
holder.itemView.setTag(R.id.edit_reminder,position);
CardView cardView=holder.cardView;
if(!imageIds.isEmpty()) {
if (imageIds.get(position) != null) {
ImageView imageView = (ImageView) cardView.findViewById(R.id.info_image);
BitmapFactory.Options bmOptions = new BitmapFactory.Options();

Bitmap bitmap = BitmapFactory.decodeFile(imageIds.get(position), bmOptions);
bitmap = Bitmap.createScaledBitmap(bitmap,250 , 150, true);
imageView.setImageBitmap(bitmap);
}
}
/*Drawable drawable=cardView.getResources().getDrawable(imageIds[position]);
imageView.setImageDrawable(drawable);
imageView.setContentDescription(reminders.get(position));*/
TextView itemsText=(TextView)cardView.findViewById(R.id.list_of_items);
itemsText.setText(reminders.get(position));
TextView locationText=(TextView)cardView.findViewById(R.id.location_value);
locationText.setText(locations.get(position));
cardView.setOnClickListener(new View.OnClickListener(){

/**
* Called when a view has been clicked.
*
* @param v The view that was clicked.
*/
@Override
public void onClick(View v) {
if(listener!=null){
listener.onClick(position);
}
}
});

}

/**
* Returns the total number of items in the data set hold by the adapter.
*
* @return The total number of items in this adapter.
*/
@Override
public int getItemCount() {
return reminders.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private CardView cardView;
public ViewHolder(CardView v) {
super(v);
cardView=v;
}
}



}

我尝试将位置设置为标签值,但仍然遇到与上面提到的相同的空指针异常

最佳答案

使用下面的代码

替换

holder.itemView.setTag(R.id.edit_reminder,"SelectedItemEdit");

holder.itemView.setTag(R.id.edit_reminder, position);

您在 setTag 时没有传递项目的位置,因为您在这里没有获得 int 值

int itemPosition =(int)view.getTag(R.id.edit_reminder);

还要确保在对指定位置的 View 使用 getTag() 之前执行 setTag。希望这些对您有帮助。

关于java - 获取回收器 View 位置给出 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42165293/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com