[英]Android Jsoup - NullPointerException when parsing images
I am using Jsoup in my Android app to get images from the internet but an getting a NullPointerException. 我在Android应用程序中使用Jsoup从互联网获取图像,但是却收到NullPointerException。 Java: 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: 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: 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? 我应该更改些什么才能使图像起作用?
Look at your stacktrace: 查看您的堆栈跟踪:
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)
You haven't initialized the string array String[] imgSrcs;
您尚未初始化字符串数组String[] imgSrcs;
yet you try to add content to it in your for each loop: 但是您尝试在每个循环中向其中添加内容:
imgSrcs[i] = img.text();
Also, the size of an array cannot be modified, so you either decide upon a size of it when you initialize it and then stick with it. 另外,数组的大小无法修改,因此您可以在初始化数组时决定数组的大小,然后再坚持使用。 If you want a dynamic size, use an ArrayList
object instead. 如果需要动态大小,请改用ArrayList
对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.