雖然轉換價值爲int我得到空指針異常。 這是我得到獲取回收視圖位置是給NullPointerException異常
顯示java.lang.NullPointerException錯誤:嘗試上的空對象引用調用虛擬方法「詮釋java.lang.Integer.intValue()」
這裏是我的代碼
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;
}
}
}
我嘗試設置位置的變量值還是我得到同樣的空指針異常上述
你有一個標籤爲'R.id.edit_reminder'設置? –