尝试在null对象引用上调用Adapter.notifyDataSetChanged()'

时间:2017-04-24 19:29:18

标签: java android listview adapter android-arrayadapter

我也尝试在onCreate()中添加适配器,它会抛出一个空指针异常,并在“空对象引用”上尝试调用listVIew.setAdapter。

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

Sub boldIt()
Dim pos_bold As Integer
Dim celltxt As String

For i = 2 To 200000
    ' first cell will always be populated - if not - exit
    If (Range("Plan!E" & i).Value = "") Then Exit For

    ' if second cell is empty - take only first cell as normal txt
    If (Range("Plan!F" & i).Value = "") Then
        Range("Kalender!F" & i).Value = Range("Plan!E" & i).Value
    Else
        ' calculate start of bold text
        pos_bold = Len(Range("Plan!E" & i).Value) + 1

        ' create the string
        celltxt = Range("Plan!E" & i).Value & " - " & Range("Plan!F" & i).Value
        ' add string to field and add bold to last part
        With Worksheets("Kalender").Range("F" & i)
            .Value = celltxt
            .Characters(pos_bold).Font.Bold = True
        End With
    End If
Next i
End Sub

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

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


    }
}

2 个答案:

答案 0 :(得分:1)

尝试在adapterClass.this.notifyDataSetChanged()

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

答案 1 :(得分:1)

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

listNotifications.setAdapter(adapter);

试试这个:)