Android 致命异常:AsyncTask#1使用JSON在后台执行错误
我正在尝试使用JSON将图像解析到带有asynctask的viewpager,并将其解析到我的应用程序中。在修复JSON问题后,我已经遇到这个问题2天了,现在我遇到了另一个Async task问题我已经查看了stackoverflow中的所有答案,不幸的是没有一个能解决我的问题。这是我的代码和错误日志,如果有任何帮助,我们将不胜感激 json链接- 这里是MyPagerAdapter.javaAndroid 致命异常:AsyncTask#1使用JSON在后台执行错误,android,json,eclipse,android-asynctask,crash,Android,Json,Eclipse,Android Asynctask,Crash,我正在尝试使用JSON将图像解析到带有asynctask的viewpager,并将其解析到我的应用程序中。在修复JSON问题后,我已经遇到这个问题2天了,现在我遇到了另一个Async task问题我已经查看了stackoverflow中的所有答案,不幸的是没有一个能解决我的问题。这是我的代码和错误日志,如果有任何帮助,我们将不胜感激 json链接- 这里是MyPagerAdapter.java package vinajson.img; import org.json.JSONArray
package vinajson.img;
import org.json.JSONArray;
import android.content.Context;
....
public class MyPagerAdapter extends PagerAdapter {
//private String caption;
private Drawable d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13;
private JSONArray Image;
public MyPagerAdapter( Drawable draw1, Drawable draw2,Drawable draw3,Drawable draw4,Drawable draw5,
Drawable draw6,Drawable draw7,Drawable draw8,Drawable draw9,Drawable draw10,Drawable draw11,
Drawable draw12,Drawable draw13){
//caption = text;
d1 = draw1;
d2 = draw2;
....
}
public int getCount() {
return Image.length();
}
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = R.layout.first_page;
View view = inflater.inflate(resId, null);
ImageView image1 = (ImageView)view.findViewById(R.id.imageView1);
switch (position) {
case 0:
image1.setImageDrawable(d1);
break;
case 1:
image1.setImageDrawable(d2);
break;
case 2:
image1.setImageDrawable(d3);
break;
....
}
((ViewPager) collection).addView(view, 0);
return view;
}
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
public Parcelable saveState() {
return null;
}
}您必须在构造函数上初始化
JSONArray映像。当它召唤
public int getCount() {
return Image.length();
}
它正在崩溃为空指针异常
<强>注释:< /强>也应考虑如果<代码> GETCONTUTE()/代码>应该真正返回<代码> JSONArray < /代码>
< P>好的,我通过在我的中设置一个固定的整数来解决这个问题。
public int getCount() {
return 13;
}
现在我的应用程序运行了,但为什么我不能将其设置为返回Image.length();?
如果有人能启发我,我将不胜感激
非常感谢KevinT和Christopher Fransisco提供了所有有用的提示和指导。这有点离题,但你应该看看Volley,它是一个很棒的网络框架。顺便说一句,看看你的MainActivity.java:103,它抛出了一个ArrayIndexOutOfBoundsException“java.lang.ArrayIndexOutOfBoundsException:length=12;index=12”谢谢,我修复了ArrayIndexOutOfBoundsException问题,但现在我遇到了致命的异常:main问题。错误日志已更新。错误在MyPagerAdapter
,您能在您的帖子中包含它的代码吗?MyPagerAdapter.java:47您可能在使用它之前没有初始化。我应该使用固定整数而不是返回JSONArray吗?好的,我通过在我的public int getCount(){return 13;}中设置一个固定整数来修复这个问题
这可能行得通,但是每次元素数量发生变化时,您都必须对其进行更改@Christopher Francisco,您能推荐其他方法吗?您可以使用可绘制数组,而不是d1、d2、d3、d4。。并使用length属性
public int getCount() {
return Image.length();
}
public int getCount() {
return 13;
}