我是一般的编码新手,我做错了什么?有人告诉我做异步任务,但我不知道怎么做... 我的最终目标是在登录后从xml页面获取数据,然后将其显示在我的设备上。
------------------------ JAVA-CODE -------------------- ------------------
公共类MainActivity扩展Activity { TextView textViewXML;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
HttpURLConnection urlConnection = null;
final String LOG_TAG = MainActivity.class.getSimpleName();
String[] params={"Acheivers"};
try {
final String FORECAST_BASE_URL="http://www.unimaxindia.net/webservice.asmx?";
final String QUERY_PARAM="op";
final String FORMAT_PARAM="mode";
Uri builtUri=Uri.parse(FORECAST_BASE_URL).buildUpon()
.appendQueryParameter(QUERY_PARAM, params[0])
.appendQueryParameter(FORMAT_PARAM,"xml")
.build();
URL url = new URL(builtUri.toString());
Log.v(LOG_TAG, "Built URI" + url);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
textViewXML =(TextView)findViewById(R.id.textViewXML);
InputStream inStream = urlConnection.getInputStream();
/*
* this.getAssets().open("AgentDetail.xml");
*
*
*/
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = domFactory.newDocumentBuilder();
Document doc = dBuilder.parse(inStream);
if (doc.hasChildNodes()) {
printNote(doc.getChildNodes());
}
} catch (Exception e) {
textViewXML.setText(textViewXML.getText().toString() + e.getMessage());
}
}
private void printNote(NodeList nodeList) {
for (int count = 0; count < nodeList.getLength(); count++) {
Node tempNode = nodeList.item(count);
// make sure it's element node.
if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
textViewXML.setText( tempNode.getTextContent());
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
------------------------ logcat的---------------------- --------------------
07-01 13:30:18.455 5298-5316/com.xmlparsing.tnh.sampleagent W/art﹕ Suspending all threads took: 13.264ms
07-01 13:30:18.566 5298-5298/com.xmlparsing.tnh.sampleagent D/AndroidRuntime﹕ Shutting down VM
07-01 13:30:18.594 5298-5298/com.xmlparsing.tnh.sampleagent E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.xmlparsing.tnh.sampleagent, PID: 5298`enter code here`
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xmlparsing.tnh.sampleagent/com.xmlparsing.tnh.sampleagent.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference
at com.xmlparsing.tnh.sampleagent.MainActivity.onCreate(MainActivity.java:62)
at android.app.Activity.performCreate(Activity.java:5937)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
答案 0 :(得分:-1)
首先,您应该在异步任务中调用与网络相关的操作,请参阅: Android: AsyncTask to make an HTTP GET Request?
还有很多东西要学习我建议在这里关注android培训: http://developer.android.com/training/index.html