Java 尝试在空对象引用上调用Adapter.notifyDataSetChanged()
我也尝试过在onCreate中添加适配器,它会抛出一个空指针异常,错误为“试图在空对象引用上调用listVIew.setAdapter” 下面是我在主要活动中使用notifyDataSetChange的代码Java 尝试在空对象引用上调用Adapter.notifyDataSetChanged(),java,android,listview,adapter,android-arrayadapter,Java,Android,Listview,Adapter,Android Arrayadapter,我也尝试过在onCreate中添加适配器,它会抛出一个空指针异常,错误为“试图在空对象引用上调用listVIew.setAdapter” 下面是我在主要活动中使用notifyDataSetChange的代码 import static com.name.sample.Tab1.adapter; public class Requests extends AsyncTask<String, Void, String> { @Override protected S
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;
}}
尝试使用:adapterClass.this.notifyDataSetChanged in onPostExcecute
尝试使用:adapterClass.this.notifyDataSetChanged in onPostExcecute
您在listview中添加了错误的适配器。在onCreateView中应该是这样的:
listNotifications.setAdapter(adapter);
尝试以下操作:您在listview中添加了错误的适配器。在onCreateView中应该是这样的:
listNotifications.setAdapter(adapter);
尝试以下操作:我将其用作Tab1.InvitedAdapter.this.notifyDataSetChanged;并且它给出了一个错误,即它不是您可以使用的封闭类:Tab1.InvitedAdapter.notifyDataSetChanged;改变数据;无法从静态上下文引用。是的,我也将适配器更改为非静态。问题是您的适配器在非活动状态下为空。您应该初始化它或使用适配器类名…来调用notifyDatasetChanged。我已尝试完全删除notifyDatasetChanged,它可以正常工作。为什么会发生这种情况?我将其用作Tab1.InvitedAdapter.this.notifyDataSetChanged;并且它给出了一个错误,即它不是您可以使用的封闭类:Tab1.InvitedAdapter.notifyDataSetChanged;改变数据;无法从静态上下文引用。是的,我也将适配器更改为非静态。问题是您的适配器在非活动状态下为空。您应该初始化它或使用适配器类名…来调用notifyDatasetChanged。我已尝试完全删除notifyDatasetChanged,它可以正常工作。为什么会发生这种情况?很抱歉误解了,但我实际上更改了变量的名称以保持简单,但我错过了一个。这不是解决办法。谢谢,我想你的问题出在listview上,因为它没有合适的适配器@Hamzaamin很抱歉误会了,但我实际上更改了变量的名称以保持简单,但我错过了一个。这不是解决办法。谢谢,我想你的问题出在listview上,因为它没有合适的适配器@哈姆扎明