値をintに変換する際に、NULLポインタ例外が発生しています。リサイクラのビュー位置を取得するとNullPointerExceptionが発生する
ここではnullオブジェクト参照の上 ')int型java.lang.Integer.intValue(' 仮想メソッドを呼び出すための試みがある: これは私が
がのjava.lang.NullPointerExceptionが取得していますエラーです私のコード
ここ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();*/ } }); }
私は取得していますnullポインタ例外
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'にタグが設定されていますか? –