我是初学程序员,我从天气网站开始使用网络抓取工具。当我在每个Activity的前两个城市正常工作时运行应用程序时,其他人使用以下消息停止应用程序:“Current_Weather被中断。如果我的问题有解决方案,请帮助我。
错误消息:
06-24 16:03:04.805 18004-18004/com.example.junior_marg.current_weather E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.junior_marg.current_weather, PID: 18004
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.junior_marg.current_weather/com.example.junior_marg.current_weather.Main14Activity}: java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Caused by: java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.get(ArrayList.java:411)
at com.example.junior_marg.current_weather.Main14Activity.onCreate(Main14Activity.java:94)
at android.app.Activity.performCreate(Activity.java:6942)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2880)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Java代码:
package com.example.junior_marg.current_weather;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
public class Main14Activity extends AppCompatActivity {
TextView mTextView_33;
TextView mTextView_34;
TextView mTextView_35;
TextView mTextView_36;
TextView mTextView_37;
TextView mTextView_38;
TextView mTextView_39;
TextView mTextView_40;
TextView mTextView_41;
TextView mTextView_42;
TextView mTextView_43;
TextView mTextView_44;
TextView mTextView_45;
TextView mTextView_46;
TextView mTextView_47;
TextView mTextView_48;
String therm_3;
String ygrasia_3;
String simdrosou_3;
String anemos_3;
String varom_3;
String simerinosy_3;
String ragd_3;
String trexkak_3;
String miniaiosy_3;
String ethsiosy_3;
String aisthpsix_3;
String yperithriakt_3;
String deiktisdis_3;
String hliakiakt_3;
String anatoli_3;
String disi_3;
String url;
Document doc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main14);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
mTextView_33 = (TextView) findViewById(R.id.textview77);
mTextView_34 = (TextView) findViewById(R.id.textview79);
mTextView_35 = (TextView) findViewById(R.id.textview81);
mTextView_36 = (TextView) findViewById(R.id.textview83);
mTextView_37 = (TextView) findViewById(R.id.textview85);
mTextView_38 = (TextView) findViewById(R.id.textview87);
mTextView_39 = (TextView) findViewById(R.id.textview89);
mTextView_40 = (TextView) findViewById(R.id.textview91);
mTextView_41 = (TextView) findViewById(R.id.textview93);
mTextView_42 = (TextView) findViewById(R.id.textview95);
mTextView_43 = (TextView) findViewById(R.id.textview97);
mTextView_44 = (TextView) findViewById(R.id.textview99);
mTextView_45 = (TextView) findViewById(R.id.textview101);
mTextView_46 = (TextView) findViewById(R.id.textview103);
mTextView_47 = (TextView) findViewById(R.id.textview105);
mTextView_48 = (TextView) findViewById(R.id.textview107);
doc = null;
url = "http://penteli.meteo.gr/stations/larissa/";
try {
doc = Jsoup.connect(url).get();
} catch (IOException e) {
e.printStackTrace();
}
Element table_33 = doc.select("table").get(0);
Elements rows_33 = table_33.select("tr");
Element row_33 = rows_33.get(5);
Elements cols_33 = row_33.select("td");
therm_3 = cols_33.get(1).text() ;
mTextView_33.setText(therm_3);
Element row_34 = rows_33.get(6);
Elements cols_34= row_34.select("td");
ygrasia_3 = cols_34.get(1).text() ;
mTextView_34.setText(ygrasia_3);
Element row_35 = rows_33.get(7);
Elements cols_35 = row_35.select("td");
simdrosou_3 = cols_35.get(1).text() ;
mTextView_35.setText(simdrosou_3);
Element row_36 = rows_33.get(8);
Elements cols_36 = row_36.select("td");
anemos_3 = cols_36.get(1).text() ;
mTextView_36.setText(anemos_3);
Element row_37 = rows_33.get(9);
Elements cols_37 = row_37.select("td");
varom_3 = cols_37.get(1).text() ;
mTextView_37.setText(varom_3);
Element row_38 = rows_33.get(10);
Elements cols_38 = row_38.select("td");
simerinosy_3 = cols_38.get(1).text() ;
mTextView_38.setText(simerinosy_3);
Element row_39 = rows_33.get(11);
Elements cols_39 = row_39.select("td");
ragd_3 = cols_39.get(1).text() ;
mTextView_39.setText(ragd_3);
Element row_40 = rows_33.get(12);
Elements cols_40 = row_40.select("td");
trexkak_3 = cols_40.get(1).text() ;
mTextView_40.setText(trexkak_3);
Element row_41 = rows_33.get(13);
Elements cols_41 = row_41.select("td");
miniaiosy_3 = cols_41.get(1).text() ;
mTextView_41.setText(miniaiosy_3);
Element row_42 = rows_33.get(14);
Elements cols_42 = row_42.select("td");
ethsiosy_3 = cols_42.get(1).text() ;
mTextView_42.setText(ethsiosy_3);
Element row_43 = rows_33.get(15);
Elements cols_43 = row_43.select("td");
aisthpsix_3 = cols_43.get(1).text() ;
mTextView_43.setText(aisthpsix_3);
Element row_44 = rows_33.get(16);
Elements cols_44 = row_44.select("td");
yperithriakt_3 = cols_44.get(1).text() ;
mTextView_44.setText(yperithriakt_3);
Element row_45 = rows_33.get(17);
Elements cols_45 = row_45.select("td");
deiktisdis_3 = cols_45.get(1).text() ;
mTextView_45.setText(deiktisdis_3);
Element row_46 = rows_33.get(18);
Elements cols_46 = row_46.select("td");
hliakiakt_3 = cols_46.get(1).text() ;
mTextView_46.setText(hliakiakt_3);
Element row_47 = rows_33.get(19);
Elements cols_47 = row_47.select("td");
anatoli_3 = cols_47.get(1).text() ;
mTextView_47.setText(anatoli_3);
Element row_48 = rows_33.get(20);
Elements cols_48 = row_48.select("td");
disi_3 = cols_48.get(1).text() ;
mTextView_48.setText(disi_3);
}
}
XML代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.junior_marg.current_weather.Main14Activity">
<TextView
android:id="@+id/textview76"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/c"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp" />
<TextView
android:id="@+id/textview77"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/textview76"
android:layout_marginLeft="20dp"
android:layout_marginStart="92dp"
android:layout_marginTop="10dp" />
<TextView
android:id="@+id/textview78"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/d"
android:layout_below="@+id/textview76"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/textview79"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/textview78"
android:layout_below="@+id/textview77"
android:layout_marginLeft="50dp"
android:layout_marginStart="133dp"
android:layout_marginTop="10dp" />
<TextView
android:id="@+id/textview80"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/h2"
android:layout_below="@+id/textview78"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp" />
<TextView
android:id="@+id/textview81"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/textview80"
android:layout_marginLeft="20dp"
android:layout_marginStart="77dp"
android:layout_marginTop="63dp" />
<TextView
android:id="@+id/textview82"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/i2"
android:layout_below="@+id/textview80"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/textview83"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/textview82"
android:layout_marginLeft="50dp"
android:layout_marginStart="130dp"
android:layout_marginTop="90dp" />
<TextView
android:id="@+id/textview84"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/j2"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_below="@+id/textview82"/>
<TextView
android:id="@+id/textview85"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/textview84"
android:layout_below="@+id/textview83"
android:layout_marginLeft="10dp"
android:layout_marginStart="104dp"
android:layout_marginTop="10dp" />
<TextView
android:id="@+id/textview86"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/k2"
android:layout_below="@+id/textview84"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/textview87"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/textview86"
android:layout_below="@+id/textview85"
android:layout_marginLeft="50dp"
android:layout_marginStart="63dp"
android:layout_marginTop="11dp" />
<TextView
android:id="@+id/textview88"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/l2"
android:layout_below="@+id/textview86"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp" />
<TextView
android:id="@+id/textview89"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/textview88"
android:layout_below="@+id/textview87"
android:layout_marginLeft="20dp"
android:layout_marginStart="94dp"
android:layout_marginTop="10dp" />
<TextView
android:id="@+id/textview90"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/m2"
android:layout_below="@+id/textview88"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/textview91"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/textview90"
android:layout_below="@+id/textview89"
android:layout_marginLeft="50dp"
android:layout_marginStart="36dp"
android:layout_marginTop="10dp" />
<TextView
android:id="@+id/textview92"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/n2"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_below="@+id/textview90"/>
<TextView
android:id="@+id/textview93"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/textview92"
android:layout_below="@+id/textview91"
android:layout_marginLeft="20dp"
android:layout_marginStart="75dp"
android:layout_marginTop="10dp" />
<TextView
android:id="@+id/textview94"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/o2"
android:layout_below="@+id/textview92"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/textview95"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/textview94"
android:layout_below="@+id/textview93"
android:layout_marginLeft="50dp"
android:layout_marginStart="85dp"
android:layout_marginTop="10dp" />
<TextView
android:id="@+id/textview96"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/p2"
android:layout_below="@+id/textview94"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp" />
<TextView
android:id="@+id/textview97"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/textview96"
android:layout_below="@+id/textview95"
android:layout_marginLeft="20dp"
android:layout_marginStart="73dp"
android:layout_marginTop="10dp" />
<TextView
android:id="@+id/textview98"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/q2"
android:layout_below="@+id/textview96"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/textview99"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/textview98"
android:layout_below="@+id/textview97"
android:layout_marginLeft="50dp"
android:layout_marginStart="24dp"
android:layout_marginTop="10dp" />
<TextView
android:id="@+id/textview100"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/r2"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_below="@+id/textview98"/>
<TextView
android:id="@+id/textview101"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/textview100"
android:layout_below="@+id/textview99"
android:layout_marginLeft="10dp"
android:layout_marginStart="52dp"
android:layout_marginTop="10dp" />
<TextView
android:id="@+id/textview102"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/s2"
android:layout_below="@+id/textview100"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/textview103"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/textview102"
android:layout_below="@+id/textview101"
android:layout_marginLeft="50dp"
android:layout_marginStart="45dp"
android:layout_marginTop="11dp" />
<TextView
android:id="@+id/textview104"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/t2"
android:layout_below="@+id/textview102"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp" />
<TextView
android:id="@+id/textview105"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/textview104"
android:layout_below="@+id/textview103"
android:layout_marginLeft="20dp"
android:layout_marginStart="122dp"
android:layout_marginTop="10dp" />
<TextView
android:id="@+id/textview106"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/u2"
android:layout_below="@+id/textview104"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/textview107"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/textview106"
android:layout_below="@+id/textview105"
android:layout_marginLeft="50dp"
android:layout_marginStart="151dp"
android:layout_marginTop="10dp" />
</RelativeLayout>
答案 0 :(得分:0)
我认为您的问题出在therm_3 = cols_33.get(1).text() ;
。当你试图得到它的第二个元素时,你的cols_33
似乎只有一个元素。如果你用这个代替那行,我希望你的问题能解决:
therm_3 = cols_33.get(0).text() ;