我需要在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;
}
}
我该如何解决这个问题。我需要用alertdialog显示图像...我该如何解决这个问题...
答案 0 :(得分:1)
实际上你的上下文是null。所以像这样改变
GridViewAdapter(String[] fpath,Context context) {
filepath = fpath;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
在您的活动类中调用GridViewAdapter,如下所示
adapter1 = new GridViewAdapter(FilePathStrings,getApllicationContext());
答案 1 :(得分:1)
代码本身清楚地提到了它。
GridViewAdapter(Activity activity, String[] fpath) { //you need to pass Activity reference here
filepath = fpath;
activity = activity;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
请参阅参数Activity添加。您需要将此参数传递给构造函数。