【问题标题】:Attempt to invoke Adapter.notifyDataSetChanged()' on a null object refernce尝试在空对象引用上调用 Adapter.notifyDataSetChanged()'
【发布时间】:2017-04-24 19:29:17
【问题描述】:

我也尝试在 onCreate() 中添加适配器,但它会引发空指针异常,并显示错误“尝试在空对象引用上调用 listVIew.setAdapter。

这是我在 Main Activity 中使用 notifyDataSetChange() 的代码;

import static com.name.sample.Tab1.adapter;


public class Requests extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {

        Cursor c =  myDB.rawQuery("SELECT * FROM requests", null);

        int aIndex = c.getColumnIndex("name");
        int bIndex = c.getColumnIndex("nick");
        int cIndex = c.getColumnIndex("num");

        name.clear();
        nick.clear();
        number.clear();

        if (c.moveToFirst()){

            do {

                name.add(c.getString(aIndex));
                nick.add(c.getString(bIndex));
                number.add(c.getInt(cIndex));

            } while (c.moveToNext());
        }

        return null;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);


        adapter.notifyDataSetChanged();


    }
}

这是我的 Tab1 代码 这是适配器的代码

public class Tab1 extends Fragment {

static ListView listNotifications;
final static ArrayList<String> name = new ArrayList<>();
static ArrayList<Integer> number = new ArrayList<>();
static ArrayList<String> nick = new ArrayList<>();

static ImageAdapter adapter;


public static class ImageAdapter extends ArrayAdapter<String> {
    private final Context context;
    private final String[] values;

    public ImageAdapter (Context context, String[] values) {
        super(context, R.layout.list_with_icons, values);
        this.context = context;
        this.values = values;
    }

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

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

        View rowView = inflater.inflate(R.layout.list_with_icons, parent, false);

        TextView textView = (TextView) rowView.findViewById(R.id.textViewName);

        ImageView imageView = (ImageView) rowView.findViewById(R.id.imagePicture);

        textView.setText(values[position]);
        // Change the icon for Windows and iPhone


        return rowView;
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.tab1layout, container, false);

    String[] nameString = new String[name.size()];
    nameString = name.toArray(nameString);

    adapter = new ImageAdapter (getActivity(), nameString);



   listNotifications =(ListView)rootView.findViewById(R.id.listNotifications);

listNotifications.setAdapter(adapter);




    return rootView;


}}

【问题讨论】:

    标签: java android listview adapter android-arrayadapter


    【解决方案1】:

    尝试使用:adapterClass.this.notifyDataSetChanged() in onPostExcecute()

     @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        InvitedAdapter.this.notifyDataSetChanged();
    }
    

    【讨论】:

    • 我用它作为 Tab1.InvitedAdapter.this.notifyDataSetChanged();它给出了一个错误,它不是一个封闭类
    • 你可以使用:Tab1.InvitedAdapter.notifyDataSetChanged();
    • notifyDataSetChanged();不能从静态上下文中引用。是的,我也将适配器更改为非静态的。
    • 问题是您的adapter 在您的maiinActivity 中为空,您应该初始化它或使用适配器类名称...调用notifyDatasetChanged
    • 我尝试完全删除 notifyDatasetChanged() 并且它有效。为什么会这样?
    【解决方案2】:

    您在列表视图中添加了错误的适配器。在 onCreateView() 中应该是这样的:

    listNotifications.setAdapter(adapter);
    

    试试这个:)

    【讨论】:

    • 很抱歉造成误解,但我实际上将名称更改为我的变量以保持简单,但我错过了一个。这不是解决方案。谢谢
    • 我认为您的问题出在 listview 上,因为它没有得到合适的适配器。 @HamzaAmin
    猜你喜欢
    • 2023-03-13
    • 2020-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-09
    • 2019-05-10
    • 2021-08-01
    相关资源
    最近更新 更多