向上滚动Listview时活动崩溃

时间:2017-04-25 05:57:18

标签: android listview scroll listadapter

活动由ListView组成,使用volly从服务器获取数据。

向下滚动ListView时,会发生错误。

使用以下代码将listview的最后一行设置为按钮:

 if (position == datamodel.size() - 1) 
    {
            convertView = inflater.inflate(R.layout.list_button, parent, false);
           TextView mytxt =(TextView)convertView.findViewById(R.id.txt_btn_more);

            mytxt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent myint = new Intent(context,Home.class);
                    editor.putString("btn_more_cliked","yes");
                    editor.commit();
                    myint.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(myint);

                }
    });

    return convertView;

ListView向下滚动时工作正常但向下滚动完成ListView再次向上滚动时会发生错误。

 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                                  at com.kachhua.blog.Home_ListAdapter.getView(Home_ListAdapter.java:92)
                                                                  at android.widget.AbsListView.obtainView(AbsListView.java:2346)
                                                                  at android.widget.ListView.makeAndAddView(ListView.java:1875)
第92行

我有这个代码

   title1.setText(m.getPost_title());

这是我的整个代码列表adpater

public class Home_ListAdapter extends BaseAdapter {
private Context context;
private LayoutInflater inflater;
private List<DataModel_ListPost> datamodel;
public SharedPreferences pref;
public SharedPreferences.Editor editor;


public Home_ListAdapter(Context context, List<DataModel_ListPost> datamodel) {
    this.context = context;
    this.datamodel = datamodel;
}

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

@Override
public Object getItem(int position) {
    return datamodel.get(position);
}

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

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    pref = context.getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
    editor = pref .edit();



    if (inflater == null)
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if(position == datamodel.size() - 1) // last element is a Button
    {

            convertView = inflater.inflate(R.layout.list_button, parent, false);
           TextView mytxt =(TextView)convertView.findViewById(R.id.txt_btn_more);

            mytxt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent myint = new Intent(context,Home.class);
                    editor.putString("btn_more_cliked","yes");
                    editor.commit();
                    myint.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(myint);

                }
            });

            return convertView;

    }
    else
    {


    if (convertView == null)
    {
        convertView = inflater.inflate(R.layout.single_row_home_listview, null);
    }
    else
    {
        TextView title1 = (TextView) convertView.findViewById(R.id.txt_title_text);
        TextView date =(TextView)convertView.findViewById(R.id.date_in_single_row);

        DataModel_ListPost m = datamodel.get(position);
        title1.setText(m.getPost_title());
        date.setText(m.getPost_date());




        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String id = datamodel.get(position).getPost_id();
                String title= datamodel.get(position).getPost_title();
                String content= datamodel.get(position).getPost_content();
                String link   = datamodel.get(position).getPost_url();
                String date = datamodel.get(position).getPost_date();
                String modified_date = datamodel.get(position).getPost_modified_date();

                editor.putString("id_from_list_adapter",id);
                editor.putString("title_from_list_adapter",title);
                editor.putString("content_from_list_adapter",content);
                editor.putString("post_url_from_list_adapter",link);
                editor.putString("post_date_from_list_adapter",date);
                editor.putString("post_modified_date_from_list_adapter",modified_date);
                editor.commit();

                Intent myint = new Intent(context,Post_Activivty.class);
                myint.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(myint);
            }
        });

    }

        return convertView;
    }

  }
}

这是我的listview的代码

     postlist=(ListView)findViewById(R.id.postList_home);
      adapter = new Home_ListAdapter(getApplicationContext(), datamodel);
            postlist.setAdapter(adapter);

2 个答案:

答案 0 :(得分:0)

请在此行调试:

DataModel_ListPost m = datamodel.get(position);
title1.setText(m.getPost_title());

对于任何特定位置,m都有可能为空。

答案 1 :(得分:0)

请你尝试删除这个条件

 if (convertView == null)
{
    convertView = inflater.inflate(R.layout.single_row_home_listview, null);
}
else
{
    TextView title1 = (TextView) convertView.findViewById(R.id.txt_title_text);
    TextView date =(TextView)convertView.findViewById(R.id.date_in_single_row);

并在每次初始化title1和日期TextViews之前初始化convertView。