我正在尝试从服务器获取图像并将其加载到gridview中,但我的应用程序没有响应。我正在使用picasso库来加载图像。
这是我的ImageAdapter类
package com.example.haha;
public class ImageAdapter extends BaseAdapter {
private Context context;
int length;
String[] poster_paths=null;
public ImageAdapter(Context c) {
context = c;
new BackgroundTask().execute();
}
public int getCount(){
return length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent){
//ImageView image;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView==null)
convertView=(ImageView)inflater.inflate(R.layout.gridviewitem, null);
Picasso.with(context) //USING PICASSO LIBRARY
.load("http://image.tmdb.org/t/p/w185/"+ poster_paths[position])
.placeholder(R.drawable.ic_launcher)
.into((ImageView)convertView);
return convertView;
}
public class BackgroundTask extends AsyncTask<String, Void, String[]>{
final String BASE_URL="http://api.themoviedb.org/3/discover/movie?"
+ "sort_by=popularity.desc&api_key=b6c282dc7fb604515fdcf8d8cf56e761";
private String[] jsonParse(String movieJsonStr) throws JSONException{
final String POSTER_PATH="poster_path"; //JSON STRING PARSING AND RETURN
//POSTER PATHS
JSONObject jsonObj=new JSONObject(movieJsonStr);
JSONArray resultArray= jsonObj.getJSONArray("results");
length=resultArray.length();
String[] poster_paths= new String[length];
for(int i=0;i<length;i++){
poster_paths[i]=resultArray.getJSONObject(i).getString(POSTER_PATH);
}
for(String res : poster_paths)
Log.v(POSTER_PATH, res);
return poster_paths;
}
@Override
protected String[] doInBackground(String... params) {
String movieJsonStr = null;
BufferedReader reader = null;
HttpURLConnection urlConnection = null; // READ THE INPUTSTREAM AND
// GET THE JSONSTRING
try {
URL url = new URL(BASE_URL);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
// Read the input stream into a String
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputStream == null) {
// Nothing to do.
return null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
// Since it's JSON, adding a newline isn't necessary (it
// won't affect parsing)
// But it does make debugging a *lot* easier if you print
// out the completed
// buffer for debugging.
buffer.append(line + "\n");
}
if (buffer.length() == 0) {
// Stream was empty. No point in parsing.
return null;
}
movieJsonStr = buffer.toString();
Log.v("Movie string: ", movieJsonStr);
} catch (Throwable e) {
Log.e("backgroundtask", "EXCEPTION", e);
} finally {
urlConnection.disconnect();
try {
reader.close();
} catch (IOException e) {
Log.e("READER.CLOSE()", e.toString());
}
}
try {
return jsonParse(movieJsonStr);
} catch (JSONException e) {
Log.e("BACKGROUNDTASK", "EXCEPTION FROM jsonParse()", e);
}
return null;
}
@Override
protected void onPostExecute(String[] result) {
if(result!=null)
poster_paths=result;
}
}
}
这是我的logcat
08-01 03:04:56.108: D/AndroidRuntime(2588): Shutting down VM
08-01 03:04:56.108: E/AndroidRuntime(2588): FATAL EXCEPTION: main
08-01 03:04:56.108: E/AndroidRuntime(2588): Process: com.example.haha, PID: 2588
08-01 03:04:56.108: E/AndroidRuntime(2588): java.lang.NullPointerException: Attempt to read from null array
08-01 03:04:56.108: E/AndroidRuntime(2588): at com.example.haha.ImageAdapter.getView(ImageAdapter.java:62)
08-01 03:04:56.108: E/AndroidRuntime(2588): at android.widget.AbsListView.obtainView(AbsListView.java:2344)
08-01 03:04:56.108: E/AndroidRuntime(2588): at android.widget.GridView.onMeasure(GridView.java:1060)
08-01 03:04:56.108: E/AndroidRuntime(2588): at android.view.View.measure(View.java:17430)
08-01 03:04:56.108: E/AndroidRuntime(2588): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
08-01 03:04:56.108: E/AndroidRuntime(2588): at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
08-01 03:04:56.108: E/AndroidRuntime(2588): at android.view.View.measure(View.java:17430)
08-01 03:04:56.108: E/AndroidRuntime(2588): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
08-01 03:04:56.108: E/AndroidRuntime(2588): at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
08-01 03:04:56.108: E/AndroidRuntime(2588): at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124)
08-01 03:04:56.108: E/AndroidRuntime(2588): at android.view.View.measure(View.java:17430)
08-01 03:04:56.108: E/AndroidRuntime(2588): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
08-01 03:04:56.108: E/AndroidRuntime(2588): at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:444)
08-01 03:04:56.108: E/AndroidRuntime(2588): at android.view.View.measure(View.java:17430)
08-01 03:04:56.108: E/AndroidRuntime(2588): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
08-01 03:04:56.108: E/AndroidRuntime(2588): at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
08-01 03:04:56.108: E/AndroidRuntime(2588): at android.view.View.measure(View.java:17430)
08-01 03:04:56.108: E/AndroidRuntime(2588): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
08-01 03:04:56.108: E/AndroidRuntime(2588): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
08-01 03:04:56.108: E/AndroidRuntime(2588): at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
08-01 03:04:56.108: E/AndroidRuntime(2588): at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
08-01 03:04:56.108: E/AndroidRuntime(2588): at android.view.View.measure(View.java:17430)
08-01 03:04:56.108: E/AndroidRuntime(2588): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
08-01 03:04:56.108: E/AndroidRuntime(2588): at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
08-01 03:04:56.108: E/AndroidRuntime(2588): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2560)
08-01 03:04:56.108: E/AndroidRuntime(2588): at android.view.View.measure(View.java:17430)
08-01 03:04:56.108: E/AndroidRuntime(2588): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2001)
08-01 03:04:56.108: E/AndroidRuntime(2588): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1166)
08-01 03:04:56.108: E/AndroidRuntime(2588): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1372)
08-01 03:04:56.108: E/AndroidRuntime(2588): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
08-01 03:04:56.108: E/AndroidRuntime(2588): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
08-01 03:04:56.108: E/AndroidRuntime(2588): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
08-01 03:04:56.108: E/AndroidRuntime(2588): at android.view.Choreographer.doCallbacks(Choreographer.java:580)
08-01 03:04:56.108: E/AndroidRuntime(2588): at android.view.Choreographer.doFrame(Choreographer.java:550)
08-01 03:04:56.108: E/AndroidRuntime(2588): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
08-01 03:04:56.108: E/AndroidRuntime(2588): at android.os.Handler.handleCallback(Handler.java:739)
08-01 03:04:56.108: E/AndroidRuntime(2588): at android.os.Handler.dispatchMessage(Handler.java:95)
08-01 03:04:56.108: E/AndroidRuntime(2588): at android.os.Looper.loop(Looper.java:135)
08-01 03:04:56.108: E/AndroidRuntime(2588): at android.app.ActivityThread.main(ActivityThread.java:5221)
08-01 03:04:56.108: E/AndroidRuntime(2588): at java.lang.reflect.Method.invoke(Native Method)
08-01 03:04:56.108: E/AndroidRuntime(2588): at java.lang.reflect.Method.invoke(Method.java:372)
08-01 03:04:56.108: E/AndroidRuntime(2588): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
08-01 03:04:56.108: E/AndroidRuntime(2588): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
08-01 03:04:56.109: W/ActivityManager(1226): Force finishing activity com.example.haha/.Main
08-01 03:04:56.110: W/ActivityManager(1226): Force finishing activity com.example.haha/.MainActivity
logcat说在getView方法中访问字符串数组poster_paths时存在nullpointerException,因为它是一个空数组但是在postexecute方法中,poster_paths数组被分配了字符串 请帮帮....
答案 0 :(得分:4)
你shadowing变量poster_paths
。取代
String[] poster_paths= new String[length];
与
posterPaths = new String[length];