我现在正在学习android,我根据这本书写了一些简单的代码。
我创建一个名为Chapter03_ResourceActivity的android项目,然后在res / xml /文件夹中创建 test.xml ,内容如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<customer name = "first_name" age = "1" email = "first_name@xml.com" />
<customer name = "second_name" age = "2" email = "second_name@xml.com" />
</resources>
并在 Chapter03_ResourceActivity.java (src / com.app.MainActivity / Chapter03_ResourceActivity.java)中键入代码,如下所示:
package com.app.MainActivity;
import org.xmlpull.v1.XmlPullParserException;
import android.app.Activity;
import android.content.res.XmlResourceParser;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Chapter03_ResourceActivity extends Activity {
/** Called when the activity is first created. */
private Button myButton;
private TextView myTextView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button myButton = (Button)findViewById(R.id.btn_xml);
final TextView myTextView = (TextView)findViewById(R.id.text_xml);
myButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
StringBuilder sb = new StringBuilder();
XmlResourceParser xrp = getResources().getXml(R.xml.test);
int counter = 0;
try {
while(xrp.getEventType() != XmlResourceParser.END_DOCUMENT) {
if(xrp.getEventType() == XmlResourceParser.START_TAG) {
String name = xrp.getName();
if(name.equals("customer")) {
counter ++;
sb.append(counter + " Customer" + "\n");
sb.append(xrp.getAttributeValue(0) + "\n");
sb.append(xrp.getAttributeValue(1) + "\n");
sb.append(xrp.getAttributeValue(2) + "\n\n");
}
xrp.next();
}
}
myTextView.setText(sb.toString()); <<--Here Eclipse mark an error.
catch(XmlPullParserException e) {
e.printStackTrace();
}
}
}
}); <<--Here Eclipse mark another error.
}
}
代码中标记了两个错误。我试图自己解决它,但原谅我的沉闷,我找不到它。我多次计算“{”和“}”的数量,并发现它们是正确的。也许我真的错了?或者我不知道的事情犯了这个错误?任何帮助表示赞赏。
答案 0 :(得分:3)
catch(XmlPullParserException e) {
e.printStackTrace();
}
应该是:
} catch(XmlPullParserException e) {
e.printStackTrace();
不是数量导致你的悲伤,只是放置。您的catch
实际上在<{1}}区块内,而不是紧接在它之后。
答案 1 :(得分:1)
你用{开始你的尝试块但是你永远不会用}关闭它。所以你应该在你的catch语句之前添加一个}。
答案 2 :(得分:1)
将eclipse标记为错误的行不在任何代码块中......
在捕获之前添加一个闭合的卷曲支撑,并以这种方式移动该行:
public class Chapter03_ResourceActivity extends Activity {
/** Called when the activity is first created. */
private Button myButton;
private TextView myTextView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button myButton = (Button)findViewById(R.id.btn_xml);
final TextView myTextView = (TextView)findViewById(R.id.text_xml);
myButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
StringBuilder sb = new StringBuilder();
XmlResourceParser xrp = getResources().getXml(R.xml.test);
int counter = 0;
try {
while(xrp.getEventType() != XmlResourceParser.END_DOCUMENT) {
if(xrp.getEventType() == XmlResourceParser.START_TAG) {
String name = xrp.getName();
if(name.equals("customer")) {
counter ++;
sb.append(counter + " Customer" + "\n");
sb.append(xrp.getAttributeValue(0) + "\n");
sb.append(xrp.getAttributeValue(1) + "\n");
sb.append(xrp.getAttributeValue(2) + "\n\n");
}
xrp.next();
}
}
myTextView.setText(sb.toString()); <<--Now is fine
} catch(XmlPullParserException e) {
e.printStackTrace();
}
}
}
});
}
}