获取回收站视图位置正在提供NullPointerException

时间:2017-02-10 17:28:37

标签: java android nullpointerexception android-recyclerview

将值转换为int时,我得到空指针异常。 这是我得到的错误

  

java.lang.NullPointerException:尝试调用虚方法' int 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;
        }
    }



}

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

2 个答案:

答案 0 :(得分:0)

使用以下代码

替换

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);

还要确保在使用getTag()进行具有指定位置的视图之前正在执行setTag。 希望这些可以帮助你。

答案 1 :(得分:-1)

请发布您致电的代码部分,并将View传递给方法

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