我有一个引用数组,我将它带入适配器并加载它们。
public final class Constants {
public static final String[] IMAGES = new String[] {
"link1",
"link2",
"link3",
"link4",
"link5",
....
}
并在MyActivity中......
...
static String[] imageUrls = null;
...
imageUrls = Constants.IMAGES;
...
imageLoader.displayImage(**imageUrls[position]**, holder.imageView, options, new SimpleImageLoadingListener() {...}
一切正常!但是当我试图获得互联网上的链接时,就会出现错误......
interface IOnParseResult {void onDone(ArrayList<String> data);}
public class NewThread extends AsyncTask<String, Void, String> {
Elements title;
ArrayList<String> data;
IOnParseResult onParseResult;
@Override
protected String doInBackground(String... arg) {
Document doc;
try {
data = new ArrayList<String>();
doc = Jsoup.connect(arg[0]).get();
title = doc.select("div");
String m;
for (Element titles : title) {
if (titles.children().hasClass("btl")){
m = titles.select("img").attr("abs:src");
data.add(m);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
if (onParseResult == null) return;
onParseResult.onDone(data);
}
public void setOnParseResult(IOnParseResult onParseResult) {
this.onParseResult = onParseResult;
}
}
并且在输出数据流中可以正常登录,它们已加载,但我无法将它们传输到您的活动
...
static String[] imageUrls = null;
NewThread t = new NewThread();
...
t.setOnParseResult(new IOnParseResult() {
@Override
public void onDone(ArrayList<String> data) {
imageUrls = data.toArray(new String[data.size()]);
}
});
t.execute("http://www.kartinki24.ru/kartinki/3d/");
...
错误日志
04-05 10:43:53.973: ERROR/AndroidRuntime(1613): FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.SimpleFromEclipse/com.example.SimpleFromEclipse.ImageGridActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.SimpleFromEclipse.ImageGridActivity$ImageAdapter.getCount(ImageGridActivity.java:89)
at android.widget.GridView.setAdapter(GridView.java:131)
at com.example.SimpleFromEclipse.ImageGridActivity.onCreate(ImageGridActivity.java:70)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
... 11 more
// at com.example.SimpleFromEclipse.ImageGridActivity$ImageAdapter.getCount(ImageGridActivity.java:89)
@Override
public int getCount() {
return imageUrls.length;
}
完整代码
public class ImageGridActivity extends AbsListViewBaseActivity {
static String[] imageUrls = null;
DisplayImageOptions options;
NewThread t;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ac_image_grid);
imageLoader.init(ImageLoaderConfiguration.createDefault(this));
t = new NewThread();
t.setOnParseResult(new IOnParseResult() {
@Override
public void onDone(ArrayList<String> data) {
imageUrls = data.toArray(new String[data.size()]);
}
});
t.execute("http://www.kartinki24.ru/kartinki/3d/");
//Log.d("HZ", imageUrls[0]);
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_stub)
.showImageForEmptyUri(R.drawable.ic_empty)
.showImageOnFail(R.drawable.ic_error)
.cacheInMemory(true)
.cacheOnDisc(true)
.considerExifParams(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
listView = (GridView) findViewById(R.id.gridview);
((GridView) listView).setAdapter(new ImageAdapter());
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
startImagePagerActivity(position);
}
});
}
private void startImagePagerActivity(int position) {
Intent intent = new Intent(this, ImagePagerActivity.class);
intent.putExtra(Constants.Extra.IMAGES, imageUrls);
intent.putExtra(Constants.Extra.IMAGE_POSITION, position);
startActivity(intent);
}
public class ImageAdapter extends BaseAdapter {
@Override
public int getCount() {
return imageUrls.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
View view = convertView;
if (view == null) {
view = getLayoutInflater().inflate(R.layout.item_grid_image, parent, false);
holder = new ViewHolder();
assert view != null;
holder.imageView = (ImageView) view.findViewById(R.id.image);
holder.progressBar = (ProgressBar) view.findViewById(R.id.progress);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
imageLoader.displayImage(imageUrls[position], holder.imageView, options, new SimpleImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
holder.progressBar.setProgress(0);
holder.progressBar.setVisibility(View.VISIBLE);
}
@Override
public void onLoadingFailed(String imageUri, View view,
FailReason failReason) {
holder.progressBar.setVisibility(View.GONE);
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
holder.progressBar.setVisibility(View.GONE);
}
}, new ImageLoadingProgressListener() {
@Override
public void onProgressUpdate(String imageUri, View view, int current,
int total) {
holder.progressBar.setProgress(Math.round(100.0f * current / total));
}
}
);
return view;
}
class ViewHolder {
ImageView imageView;
ProgressBar progressBar;
}
}
}
答案 0 :(得分:1)
从哪里调用getCount()?因为如果您的代码是这样的
t.setOnParseResult(new IOnParseResult() {
@Override
public void onDone(ArrayList<String> data) {
imageUrls = data.toArray(new String[data.size()]);
}
});
t.execute("..");
SomeFunctionThatCausesCallOfGetCount();
它会崩溃。你需要它
t.setOnParseResult(new IOnParseResult() {
@Override
public void onDone(ArrayList<String> data) {
imageUrls = data.toArray(new String[data.size()]);
SomeFunctionThatCausesCallOfGetCount();
}
});
t.execute(...);
你可能在onDone回调之外调用setAdapter()
。你需要把它放在回调中。
更新
像这样改变你的onCreate
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ac_image_grid);
imageLoader.init(ImageLoaderConfiguration.createDefault(this));
t = new NewThread();
t.setOnParseResult(new IOnParseResult() {
@Override
public void onDone(ArrayList<String> data) {
imageUrls = data.toArray(new String[data.size()]);
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_stub)
.showImageForEmptyUri(R.drawable.ic_empty)
.showImageOnFail(R.drawable.ic_error)
.cacheInMemory(true)
.cacheOnDisc(true)
.considerExifParams(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
listView = (GridView) findViewById(R.id.gridview);
((GridView) listView).setAdapter(new ImageAdapter());
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
startImagePagerActivity(position);
}
});
}
});
t.execute("http://www.kartinki24.ru/kartinki/3d/");
}