早上好,結構和發送兩個數組相同的接收功能
我想送兩個數組,以相同的功能(OnPostExecute)(從getValuesGraph(DOC)和getValuesFooter(DOC)分別返回),我是Java和Android的新手,如果應用代碼結構正確,我現在不會。所以我對「災難」表示歉意。 logcat的說:
02-21 00:07:25.852:W/dalvikvm(3282):線程ID = 1:螺紋與未捕獲的異常退出(組= 0x40a71930) 02-21 00:07:25.862:E/AndroidRuntime(3282):致命例外:main 02-21 00:07:25.862:E/AndroidRuntime(3282):java.lang.NullPointerException 02-21 00:07:25.862:E/AndroidRuntime(3282):at com .Example.kwhora1.MainActivity $ BackGroundTask.onPostExecute(MainActivity.java:159) 02-21 00:07:25.862:E/AndroidRuntime(3282):at com.example.kwhora1.MainActivity $ BackGroundTask.onPostExecute(MainActivity.java :1) 02-21 00:07:25.862:E/AndroidRuntime(3282):at android.os.AsyncTask.finish(AsyncTask.java:631) 02-21 00:07:25.862:E/AndroidRun時間(3282):在android.os.AsyncTask.access $ 600(AsyncTask.java:177) 02-21 00:07:25.862:E/AndroidRuntime(3282):在android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask .java:644) 02-21 00:07:25.862:E/AndroidRuntime(3282):at android.os.Handler.dispatchMessage(Handler.java:99) 02-21 00:07:25.862:E/AndroidRuntime (3282):在android.os.Looper.loop(Looper.java:137) 02-21 00:07:25.862:E/AndroidRuntime(3282):at android.app.ActivityThread.main(ActivityThread.java:5041 ) 02-21 00:07:25.862:E/AndroidRuntime(3282):at java.lang.reflect.Method.invokeNative(Native Method) 02-21 00:07:25.862:E/AndroidRuntime(3282):at java.lang.reflect.Method.invoke(Method.java:511) 02-21 00:07:25.862:E/AndroidRuntime(3282):at com.android.internal.os.ZygoteInit $ MethodAndArgsC (ZygoteInit.java:793) 02-21 00:07:25.862:E/AndroidRuntime(3282):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 02-21 00:07:25.862:E/AndroidRuntime(3282):在dalvik.system.NativeStart.main(本機方法)
下面的代碼:
package com.example.kwhora1;
import java.io.IOException;
import java.net.URL;
import java.util.Arrays;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView tvmax, tvmid, tvmin, tvactualval;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvmax=(TextView)findViewById(R.id.tvmaximo);
tvmid=(TextView)findViewById(R.id.tvmedio);
tvmin=(TextView)findViewById(R.id.tvminimo);
new BackGroundTask().execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
class BackGroundTask extends AsyncTask<Void, Void, String[]> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
public String[] getValuesGraph(Document doc) {
int cont=24,var=7;
String bar[] = new String[cont];
/*
* Getting elements from the graphic in an array from 0-23. 0 it's 1:00am, 23 it's 00:00am
*/
for (cont=0; cont < 24; cont++){
String onMouseOver = doc.select("a").get(var+cont).attr("onMouseOver");
bar[cont] = onMouseOver.split("'")[9];
}
return bar;
}
public String[] getValuesFooter(Document doc) {
String values[] = new String[7];
/*
* Getting elements from the graphic footer
*/
Elements elements = doc.select("td.cabeceraRutaTexto");
elements.size(); // 6
/* Getting text from table */
values[0] = elements.get(0).text(); // TITLE
values[1] = elements.get(1).text(); // TEXT MAX VALUE
values[2] = elements.get(2).text(); // TEXT MIDDLE VALUE
values[3] = elements.get(3).text(); // TEXTO MIN VALUE
/* Getting numbers from table */
values[4] = elements.get(4).text(); // NUMBER MAX VALUE
values[5] = elements.get(5).text(); // NUMBER MIDDLE VALUE
values[6] = elements.get(6).text(); // NUMBER MIN VALUE
return values;
}
@Override
public String[] doInBackground(Void... params) {
try {
URL url= new URL("http://www.mywebtoextractdata.com");
Document doc = Jsoup.connect(url.toString()).get();
getValuesGraph(doc);
getValuesFooter(doc);
/*
* Getting elements from the graphic in an array from 0-23. 0 it's 1:00am, 23 it's 00:00am
*/
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String[] values) {
/*tvactualval.setText();*/
tvmax.setText(values[4]);
tvmid.setText(values[5]);
tvmin.setText(values[6]);
/*System.out.println(val[4]);*/
super.onPostExecute(values);
}
}
}
預先感謝您的幫助和時間。
非常感謝你我真的很感謝你的幫助。這樣做是最好的方式嗎?現在編譯器說「不能從String []轉換爲String」,因爲getValuesGraph(doc)返回一個數組,我認爲它試圖將整個數組保存在arr [0]中。 – user3321425
是的,它是有點混亂是不是。我將創建一個新的類,它包含單獨的字符串數組,並從'doInBackground'傳回# –
非常感謝,最後我創建了一個帶數組聲明的「Pair」類,並在doInBackground中使用It。它工作正常! – user3321425