关于setText方法的android studio java nullpointerexeption

时间:2017-01-17 10:33:34

标签: java android nullpointerexception settext

我有一个问题,从昨天开始搜索,我发布了代码:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_wishes);

    mListView = (ListView) findViewById(R.id.listId);

    refreshData();

}

private void refreshData() {

    wishes.clear();

    dbHandler = new DatabaseHandler(getApplicationContext());

    ArrayList<MyWishes> wishesFromDB = dbHandler.getWishes();

    for (int i = 0; i < wishesFromDB.size(); i++){

        String title = wishesFromDB.get(i).getTitle();
        String content = wishesFromDB.get(i).getContent();
        String date = wishesFromDB.get(i).getRecordDate();

        MyWishes myWishes = new MyWishes();

        myWishes.setTitle(title);
        myWishes.setContent(content);
        myWishes.setRecordDate(date);

        wishes.add(myWishes);

    }

    dbHandler.close();

    mWishAdapter = new WishAdapter(showWishesActivity.this, R.layout.list_row, wishes);
    mListView.setAdapter(mWishAdapter);
    mWishAdapter.notifyDataSetChanged();
}

public class WishAdapter extends ArrayAdapter<MyWishes>{

    Activity activity;
    MyWishes wish;
    ArrayList<MyWishes> mData = new ArrayList<>();
    int layoutRessource;

    public WishAdapter(Activity act, int resource, ArrayList<MyWishes> data) {
        super(act, resource, data);
        activity = act;
        layoutRessource = resource;
        mData = data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public int getPosition(MyWishes item) {
        return super.getPosition(item);
    }

    @Nullable
    @Override
    public MyWishes getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View row = convertView;
        ViewHolder holder = null;

        if (row == null || (row.getTag()) == null){

            LayoutInflater inflater = LayoutInflater.from(activity);
            row = inflater.inflate(layoutRessource, null);

            holder = new ViewHolder();

            holder.mTitle = (TextView) findViewById(R.id.titre);
            holder.mDate = (TextView) findViewById(R.id.date);

            row.setTag(holder);
        }else {

            holder = (ViewHolder) row.getTag();
        }

        holder.mMyWishes = getItem(position);

        holder.mTitle.setText(holder.mMyWishes.getTitle());
        holder.mDate.setText(holder.mMyWishes.getRecordDate());

        return row;
    }

    class ViewHolder{

        MyWishes mMyWishes;
        TextView mTitle;
        TextView mContent;
        TextView mDate;
        TextView mId;
    }
}
在getView方法上的

,我在holder.setText。

上得到一个nullpointer异常

我也发布了xml:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:padding="12dp"
android:background="#90f96666">

<ImageView
    android:id="@+id/imageViewDatabaseId"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@android:drawable/ic_menu_agenda"/>

<TextView
    android:id="@+id/titre"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="The title"
    android:textStyle="bold"
    android:layout_toRightOf="@+id/imageViewDatabaseId"
    android:layout_marginLeft="20dp"/>

<TextView
    android:id="@+id/date"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="The detail"
    android:layout_toRightOf="@+id/imageViewDatabaseId"
    android:layout_below="@+id/titleId"
    android:layout_marginTop="3dp"
    android:layout_marginLeft="40dp"
    android:textStyle="italic"/>

    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                                                 at wishlist.yassine.com.mywishlist.showWishesActivity$WishAdapter.getView(showWishesActivity.java:133)

我不明白这个问题,任何人都可以帮助我,谢谢你的时间。

5 个答案:

答案 0 :(得分:1)

而不是这个

holder.mTitle = (TextView) findViewById(R.id.titre);

试试这个

holder.mTitle = (TextView) convertView.findViewById(R.id.titre);

答案 1 :(得分:0)

    if (row == null || (row.getTag()) == null){
        LayoutInflater inflater = LayoutInflater.from(activity);
        row = inflater.inflate(layoutRessource, null);
        holder = new ViewHolder();
        holder.mTitle = (TextView) row.findViewById(R.id.titre);
        holder.mDate = (TextView) row.findViewById(R.id.date);
        row.setTag(holder);
       }

答案 2 :(得分:0)

您的观点未在getView内初始化。所以:

删除它:

        holder.mTitle = (TextView) findViewById(R.id.titre);
        holder.mDate = (TextView) findViewById(R.id.date);

有了这个:

        holder.mTitle = (TextView) row.findViewById(R.id.titre);
        holder.mDate = (TextView) row.findViewById(R.id.date);

答案 3 :(得分:0)

在你的getView中使用:

    holder.mTitle = (TextView)row.findViewById(R.id.titre);


    holder.mDate = (TextView)row.findViewById(R.id.date);

答案 4 :(得分:0)

您错过了参考视图

listview将每个项目替换为指定的布局

然后在getview item

的返回视图中使用listview方法

你可以使用

holder.mTitle = (TextView) row.findViewById(R.id.titre);

行是布局视图

 @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View row = convertView;
        ViewHolder holder = null;

        if (row == null || (row.getTag()) == null){

            LayoutInflater inflater = LayoutInflater.from(activity);
            row = inflater.inflate(layoutRessource, null);
            holder = new ViewHolder();

            holder.mTitle = (TextView) row.findViewById(R.id.titre);
            holder.mDate = (TextView) row.findViewById(R.id.date);

            row.setTag(holder);
        }else {

            holder = (ViewHolder) row.getTag();
        }

        holder.mMyWishes = getItem(position);

        holder.mTitle.setText(holder.mMyWishes.getTitle());
        holder.mDate.setText(holder.mMyWishes.getRecordDate());

        return row;
    }

    class ViewHolder{

        MyWishes mMyWishes;
        TextView mTitle;
        TextView mContent;
        TextView mDate;
        TextView mId;
    }