您好,我的应用程序有问题,当我运行它时它停止了。它让我明白了:
09-19 11:15:42.941: W/dalvikvm(6202): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
09-19 11:15:43.083: E/AndroidRuntime(6202): FATAL EXCEPTION: main
09-19 11:15:43.083: E/AndroidRuntime(6202): java.lang.RuntimeException: Unable to start activity ComponentInfo{cz.bakalari.bakalari/cz.bakalari.bakalari.ParseActivity}: java.lang.NullPointerException
09-19 11:15:43.083: E/AndroidRuntime(6202): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
09-19 11:15:43.083: E/AndroidRuntime(6202): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
09-19 11:15:43.083: E/AndroidRuntime(6202): at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-19 11:15:43.083: E/AndroidRuntime(6202): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
09-19 11:15:43.083: E/AndroidRuntime(6202): at android.os.Handler.dispatchMessage(Handler.java:99)
09-19 11:15:43.083: E/AndroidRuntime(6202): at android.os.Looper.loop(Looper.java:137)
09-19 11:15:43.083: E/AndroidRuntime(6202): at android.app.ActivityThread.main(ActivityThread.java:5041)
09-19 11:15:43.083: E/AndroidRuntime(6202): at java.lang.reflect.Method.invokeNative(Native Method)
09-19 11:15:43.083: E/AndroidRuntime(6202): at java.lang.reflect.Method.invoke(Method.java:511)
09-19 11:15:43.083: E/AndroidRuntime(6202): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-19 11:15:43.083: E/AndroidRuntime(6202): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-19 11:15:43.083: E/AndroidRuntime(6202): at dalvik.system.NativeStart.main(Native Method)
09-19 11:15:43.083: E/AndroidRuntime(6202): Caused by: java.lang.NullPointerException
09-19 11:15:43.083: E/AndroidRuntime(6202): at cz.bakalari.bakalari.XML_Parser.getValue(XML_Parser.java:112)
09-19 11:15:43.083: E/AndroidRuntime(6202): at cz.bakalari.bakalari.ParseActivity.onCreate(ParseActivity.java:45)
09-19 11:15:43.083: E/AndroidRuntime(6202): at android.app.Activity.performCreate(Activity.java:5104)
09-19 11:15:43.083: E/AndroidRuntime(6202): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
09-19 11:15:43.083: E/AndroidRuntime(6202): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
09-19 11:15:43.083: E/AndroidRuntime(6202): ... 11 more
这是我的活动:
public class ParseActivity extends Activity {
static final String KEY_XML = "xml";
static final String KEY_VERZE = "verze";
static final String KEY_BKTYP = "bktyp";
static final String KEY_STATUS = "status";
static final String KEY_UTYP = "utyp";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_parse);
XML_Parser parser = new XML_Parser();
String xml = "<xml><verze>1.0</verze><bktyp>LOGIN</bktyp><status>OK</status><utyp>R</utyp></xml>";
Document doc = parser.getDomElement(xml);
NodeList nl = doc.getElementsByTagName(KEY_XML);
Element e = (Element) nl.item(1);
TextView parseview1 = (TextView) findViewById(R.id.parseview1);
TextView parseview2 = (TextView) findViewById(R.id.parseview2);
TextView parseview3 = (TextView) findViewById(R.id.parseview3);
parseview1.setText(parser.getValue(e,KEY_VERZE));
parseview2.setText(parser.getValue(e,KEY_BKTYP));
parseview3.setText(parser.getValue(e,KEY_STATUS));
};
}
这是我的 XML_Parser.class:
public class XML_Parser {
// constructor
public XML_Parser() {
}
/**
* Getting XML from URL making HTTP request
* @param url string
* */
public String getXmlFromUrl(String url) {
String xml = null;
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// return XML
return xml;
}
/**
* Getting XML DOM element
* @param XML string
* */
public Document getDomElement(String xml){
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
} catch (ParserConfigurationException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (SAXException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (IOException e) {
Log.e("Error: ", e.getMessage());
return null;
}
return doc;
}
/** Getting node value
* @param elem element
*/
public final String getElementValue( Node elem ) {
Node child;
if( elem != null){
if (elem.hasChildNodes()){
for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
if( child.getNodeType() == Node.TEXT_NODE ){
return child.getNodeValue();
}
}
}
}
return "";
}
/**
* Getting node value
* @param Element node
* @param key string
* */
public String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
return this.getElementValue(n.item(0));
}
}
你能看出这个问题吗?我是android dev的新手,所以我需要一些简单的答案。