Android的Jsoup - NullPointerException异常解析图像时 [英] Android Jsoup - NullPointerException when parsing images
本文介绍了Android的Jsoup - NullPointerException异常解析图像时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在我的Android应用程序中使用Jsoup获得从互联网的图像,但一个越来越NullPointerException异常。
Java的:
I am using Jsoup in my Android app to get images from the internet but an getting a NullPointerException. Java:
public class CollegeInfo extends Activity{
TextView body;
ImageView image1;ImageView image2;ImageView image3;ImageView image4;
ImageView image8;ImageView image7;ImageView image6;ImageView image5;
String college;
String ataglance;
double satHigh;
double satLow;
Bitmap[] bitmap;
String[] imgSrcs;InputStream[] input;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.collegeinfo);
Bundle extras = getIntent().getExtras();
if (extras != null) {
college = extras.getString("tag");
}
SpannableStringBuilder buffer = new SpannableStringBuilder();
body = (TextView) findViewById(R.id.title);
image1 = (ImageView) findViewById(R.id.imageView1);
image2 = (ImageView) findViewById(R.id.imageView2);
image3 = (ImageView) findViewById(R.id.imageView3);
image4 = (ImageView) findViewById(R.id.imageView4);
image5 = (ImageView) findViewById(R.id.imageView5);
image6 = (ImageView) findViewById(R.id.imageView6);
image7 = (ImageView) findViewById(R.id.imageView7);
image8 = (ImageView) findViewById(R.id.imageView8);
try {
Document doc = Jsoup.connect("http://www.forbes.com/colleges/"+college+"/").get();
String name = doc.select("meta[name=keywords]").attr("content");
int nameLen = name.length();
buffer.append(name+"\n");
String city = doc.select("ul[class=address]").select("li").first().text();
buffer.append(city+"\n");
ataglance = "";
for(int i=0;i<8;i++){
ataglance += doc.select("div[class=ataglanz fleft]").select("li").get(i).ownText()+" ";
ataglance += doc.select("div[class=ataglanz fleft]").select("b").get(i).text();
ataglance += "\n";
}
String satRange = doc.select("div[class=ataglanz fleft]").select("b").get(8).text();
int satLength = satRange.length();
try{
satHigh = Double.parseDouble(satRange.substring(satLength-4, satLength));
satLow = Double.parseDouble(satRange.substring(0, satLength-5));
}catch(NumberFormatException e){
e.printStackTrace();
}
satHigh*=1.5; satLow*=1.5;
int satL = (int) Math.round(satLow); int satH = (int) Math.round(satHigh);
ataglance += "SAT Composite Range: "+Integer.toString(satL)+"-"+Integer.toString(satH);
buffer.append(ataglance+"\n");
for(int i = 0;i<8;i++){
Elements img = doc.select("div[id=photos]").select("a");
imgSrcs[i] = img.text();
input[i] = new java.net.URL(imgSrcs[i]).openStream();
bitmap[i] = BitmapFactory.decodeStream(input[i]);
}
image1.setImageBitmap(bitmap[0]);
image2.setImageBitmap(bitmap[1]);
image3.setImageBitmap(bitmap[2]);
image4.setImageBitmap(bitmap[3]);
image5.setImageBitmap(bitmap[4]);
image6.setImageBitmap(bitmap[5]);
image7.setImageBitmap(bitmap[6]);
image8.setImageBitmap(bitmap[7]);
body.setText(buffer.toString());
}catch(IOException e){
e.printStackTrace();
}
}
{
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy =
new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
}
}
XML:
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<HorizontalScrollView
android:id="@+id/horizontalScrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.06" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/imageView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/imageView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/imageView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
我在图像中加入前,一切工作正常,所以我相信我没有正确地分析图像。
Before I added in the images, everything was working fine, so I believe that I am not parsing the images correctly.
logcat的:
07-29 17:19:49.985: E/AndroidRuntime(10855): FATAL EXCEPTION: main
07-29 17:19:49.985: E/AndroidRuntime(10855): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.collegeselector/com.collegeselector.CollegeInfo}: java.lang.NullPointerException
07-29 17:19:49.985: E/AndroidRuntime(10855): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-29 17:19:49.985: E/AndroidRuntime(10855): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-29 17:19:49.985: E/AndroidRuntime(10855): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-29 17:19:49.985: E/AndroidRuntime(10855): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-29 17:19:49.985: E/AndroidRuntime(10855): at android.os.Handler.dispatchMessage(Handler.java:99)
07-29 17:19:49.985: E/AndroidRuntime(10855): at android.os.Looper.loop(Looper.java:137)
07-29 17:19:49.985: E/AndroidRuntime(10855): at android.app.ActivityThread.main(ActivityThread.java:5039)
07-29 17:19:49.985: E/AndroidRuntime(10855): at java.lang.reflect.Method.invokeNative(Native Method)
07-29 17:19:49.985: E/AndroidRuntime(10855): at java.lang.reflect.Method.invoke(Method.java:511)
07-29 17:19:49.985: E/AndroidRuntime(10855): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-29 17:19:49.985: E/AndroidRuntime(10855): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-29 17:19:49.985: E/AndroidRuntime(10855): at dalvik.system.NativeStart.main(Native Method)
07-29 17:19:49.985: E/AndroidRuntime(10855): Caused by: java.lang.NullPointerException
07-29 17:19:49.985: E/AndroidRuntime(10855): at com.collegeselector.CollegeInfo.onCreate(CollegeInfo.java:87)
07-29 17:19:49.985: E/AndroidRuntime(10855): at android.app.Activity.performCreate(Activity.java:5104)
07-29 17:19:49.985: E/AndroidRuntime(10855): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-29 17:19:49.985: E/AndroidRuntime(10855): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-29 17:19:49.985: E/AndroidRuntime(10855): ... 11 more
我应该改变,以获得图像的工作?
What should I change to get the images to work?
推荐答案
看看你的堆栈跟踪:
07-29 17:19:49.985: E/AndroidRuntime(10855): Caused by: java.lang.NullPointerException
07-29 17:19:49.985: E/AndroidRuntime(10855): at com.collegeselector.CollegeInfo.onCreate(CollegeInfo.java:87)
您还没有初始化字符串数组的String [] imgSrcs;
但你尝试内容添加到您的它每个回路:
You haven't initialized the string array String[] imgSrcs;
yet you try to add content to it in your for each loop:
imgSrcs[i] = img.text();
此外,数组的大小不能修改,让你无论是在大小为它决定何时初始化,然后坚持下去。如果你想要一个动态的大小,使用的ArrayList
对象,而不是
这篇关于Android的Jsoup - NullPointerException异常解析图像时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文