In my code I used AQUery library to load image from web but it is showing null pointer exception at "aq.id(R.id.img);" in my code ...in class i created globle object of AQuery class and i define function inside inner class methode.....null pointer exception at "aq.id(R.id.img)" which is inside getview methode of cusomgrid class..
public class Customgrid extends Activity {
public int gotbread;
public int get, send;
AQuery aq;
String url1[] = { "https://www.dropbox.com/s/f308a9s5ycuc3mh/1.jpg",
"https://dl.dropbox.com/s/2s60g4e696566nt/2.jpg",
"https://dl.dropbox.com/s/59t6wo83ekzff0y/3.jpg",
"https://dl.dropbox.com/s/1b58qtj4ftm87yc/4.jpg",
"https://dl.dropbox.com/s/py7ogwstc0814zg/5.jpg",
"https://dl.dropbox.com/s/ocavtv1tkr6wtvv/6.jpg" };
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.customgview);
AQuery aq = new AQuery(Customgrid.this);
// Bundle gotbasket = getIntent().getExtras();
// gotbread = gotbasket.getInt("pos");
gotbread = getIntent().getExtras().getInt("position");
get = gotbread;
GridView grdview = (GridView) findViewById(R.id.gridView);
grdview.setAdapter(new Customgridadapter(this));
grdview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
Bundle basket = new Bundle();
basket.putInt("position", send);
Intent i = new Intent(Customgrid.this,
FullScreenImageActivity.class);
i.putExtras(basket);
startActivity(i);
}
});
grdview.setVerticalSpacing(0);
}
private InputStream OpenHttpConnection(String urlString) throws IOException {
InputStream in = null;
int response = -1;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");
try {
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
} catch (Exception ex) {
throw new IOException("Error connecting");
}
return in;
}
private Bitmap DownloadImage(String URL) {
Bitmap bitmap = null;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return bitmap;
}
public class Customgridadapter extends BaseAdapter {
public Integer[] images1 = { R.drawable.s_1, R.drawable.s_2,
R.drawable.s_3 };
public Integer[] images2 = { R.drawable.s_5, R.drawable.s_6,
R.drawable.s_4 };
Context m1Context;
public Customgridadapter(Context context) {
super();
this.m1Context = context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return images1.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@SuppressLint("NewApi")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
int pos = 0;
LayoutInflater inflater = ((Activity) m1Context)
.getLayoutInflater();
View customRow1 = inflater.inflate(R.layout.gview, null);
ImageView image = (ImageView) customRow1
.findViewById(R.id.imageforgrid);
aq.id(R.id.imageforgrid);
aq.image(url1[0],true,true);
Bitmap bm = DownloadImage(url1[0]);
switch (gotbread) {
case 0:
image.setImageBitmap(bm);
break;
case 1:
image.setImageBitmap(bm);
break;
}
image.setAdjustViewBounds(true);
image.setScaleX((float) 0.9);
image.setScaleY((float) 0.9);
switch (get) {
case 0:
send = get + position;
break;
case 1:
send = 10 * 1 + position;
break;
case 2:
send = 10 * 2 + position;
break;
case 3:
send = 10 * 3 + position;
break;
case 4:
send = 10 * 4 + position;
break;
}
// image.setOnClickListener(new OnImageClickListener(position));
return customRow1;
}
}
You have
in
onCreate
becomes local toonCreate
.Change to
The instance variable
aq
was never initialized giving NPE.Also no network related operation on the ui thread.