这是我的三个活动,问题是我在 LineGraph 中得到 null 作为 Xstring 的输出。当我调试它时,它显示如下模式:
parseStrings = mJsoupAct.getOutput(); LineGraph.java
mExecute(); jsoupAct.java
new Parsee().execute(); jsoupAct.java
Log.d("xoutputD", output + ""); jsoupAct.java
url = " http://www.google.co.in/ "; jsoupAct.java ......
现在的问题是..为什么第 4 行在第 5 行之前执行。由于此执行,它返回 null 然后执行doInBackground。为什么会这样???
主要活动
public class MainActivity extends Activity {
jsoupAct mJsoupAct = new jsoupAct();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("xmain", "main");
Intent i = new Intent(getApplicationContext(), LineGraph.class);
startActivity(i);
}
}
线形图
public class LineGraph extends Activity {
String parseStrings;
jsoupAct mJsoupAct;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mJsoupAct = new jsoupAct();
Xparse();
}
public void Xparse() {
parseStrings = mJsoupAct.getOutput();
Log.d("xstring", parseStrings + "");
}
}
jsoup法案
public class jsoupAct extends Activity {
public String output;
public String url;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void mExecute() {
new Parsee().execute();
}
public class Parsee extends AsyncTask<String, String, String> {
protected String doInBackground(String... params) {
try {
url = "http://www.google.co.in/";
Document doc = Jsoup.connect(url).get();
String body = doc.body().text();
output = body.toString();
Log.d("xoutputD0", output + "");
} catch (IOException e) {
e.printStackTrace();
}
return output;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(output);
Log.d("xoutputD1", output + "");
}
}
public String getOutput() {
mExecute();
Log.d("xoutputD", output + "");
return output;
}
public void setOutput(String output) {
this.output = output;
}
}