gpt4 book ai didi

java - android 未知的空指针错误

转载 作者:行者123 更新时间:2023-12-02 03:08:00 26 4
gpt4 key购买 nike

我需要在alertdialog中显示gridview...当我使用此代码时,我收到此错误...

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.app.Activity.getSystemService(java.lang.String)' on a null object reference

我的maincode ....

 GridView grid;
GridViewAdapter adapter1;
listView_prev.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, final View arg1, int arg2, long arg3) {
//listview click event handling
TextView id = (TextView) arg1.findViewById(R.id.textView17);
final int id_To_Search = Integer.valueOf(id.getText().toString());
Cursor item=mydb.singlecons(id_To_Search);
Cursor att=mydb.attrs(id_To_Search);
Cursor picloc=mydb.singleconspic(id_To_Search);
att.moveToFirst();
List<String> list = new ArrayList<>();
// Log.d("temp",att.getColumnName(1));

while (!att.isAfterLast())
{
int l=att.getColumnCount();
Log.d("length", String.valueOf(l));
for(int i=2;i<l;i++){
Log.d("for","for");
if(att.getString(i)!=null){
String b= att.getColumnName(i)+" "+att.getString(i);
list.add(b);
Log.d("att",b);
}
}
Log.d("while","while");
att.moveToNext();
}
att.close();
Log.d("list", String.valueOf(list));
picloc.moveToFirst();

FilePathStrings = new String[picloc.getCount()];
int i=0;
while (!picloc.isAfterLast()){
Log.d("picloc",picloc.getString(2));
FilePathStrings[i]=picloc.getString(2);
i++;
picloc.moveToNext();
}

item.moveToFirst();

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(Consultation.this);
LayoutInflater inflater = getLayoutInflater();
View dialogView = inflater.inflate(R.layout.alert_label_editor, null);
dialogBuilder.setView(dialogView);

TextView con=(TextView)dialogView.findViewById(R.id.textView29);
con.setText("Consultation on "+item.getString(4));
TextView des=(TextView)dialogView.findViewById(R.id.textView28);
des.setText(item.getString(2));
TextView pre=(TextView)dialogView.findViewById(R.id.textView31);
pre.setText(item.getString(3));
TextView fee=(TextView)dialogView.findViewById(R.id.textView32);
fee.setText(item.getString(5));
adapter1 = new GridViewAdapter(FilePathStrings);

grid.setAdapter(adapter1);
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();


}
});

Gridviewadapter类...

class GridViewAdapter extends BaseAdapter {

private Activity activity;
private String[] filepath;
private static LayoutInflater inflater = null;
GridViewAdapter(String[] fpath) { //you need to pass Activity reference here
filepath = fpath;
activity = activity;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return filepath.length;

}

public Object getItem(int position) {
return position;
}

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

public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.gridview_item, null);
ImageView image = (ImageView) vi.findViewById(R.id.image);
Bitmap bmp = BitmapFactory.decodeFile(filepath[position]);
image.setImageBitmap(bmp);
return vi;
}

}

我该如何解决这个问题?我需要用警报对话框显示图像...我怎样才能解决这个问题...

最佳答案

实际上你的上下文是空的,所以像这样改变

GridViewAdapter(String[] fpath,Context context) { 
filepath = fpath;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

像这样在您的 Activity 类中调用 GridViewAdapter

adapter1 = new GridViewAdapter(FilePathStrings,getApllicationContext());

关于java - android 未知的空指针错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41462789/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com