我试图将ArrayList文件的数据加载到ArrayList中,但是我遇到了这个异常错误:
07-07 16:52:21.741: W/System.err(1941): org.xml.sax.SAXParseException: Unexpected token (position:TEXT [Ljava.lang.Stri...@1:28 in java.io.StringReader@f7179f5)
07-07 16:52:21.741: W/System.err(1941): at org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:146)
07-07 16:52:21.743: W/System.err(1941): at com.example.ActivityQuizScreen.addflags(ActivityQuizScreen.java:253)
07-07 16:52:21.743: W/System.err(1941): at com.example.ActivityQuizScreen.onCreate(ActivityQuizScreen.java:108)
07-07 16:52:21.743: W/System.err(1941): at android.app.Activity.performCreate(Activity.java:6237)
07-07 16:52:21.743: W/System.err(1941): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
07-07 16:52:21.745: W/System.err(1941): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
07-07 16:52:21.745: W/System.err(1941): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
07-07 16:52:21.745: W/System.err(1941): at android.app.ActivityThread.-wrap11(ActivityThread.java)
07-07 16:52:21.745: W/System.err(1941): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
07-07 16:52:21.746: W/System.err(1941): at android.os.Handler.dispatchMessage(Handler.java:102)
07-07 16:52:21.746: W/System.err(1941): at android.os.Looper.loop(Looper.java:148)
07-07 16:52:21.746: W/System.err(1941): at android.app.ActivityThread.main(ActivityThread.java:5417)
07-07 16:52:21.747: W/System.err(1941): at java.lang.reflect.Method.invoke(Native Method)
07-07 16:52:21.747: W/System.err(1941): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
07-07 16:52:21.747: W/System.err(1941): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
07-07 16:52:21.877: W/EGL_emulation(1941): eglSurfaceAttrib not implemented
07-07 16:52:21.877: W/OpenGLRenderer(1941): Failed to set EGL_SWAP_BEHAVIOR on surface 0xac0b28c0, error=EGL_SUCCESS
07-07 16:52:22.429: E/Surface(1941): getSlotFromBufferLocked: unknown buffer: 0xabff43b0
07-07 16:53:21.877: W/EGL_emulation(1941): eglSurfaceAttrib not implemented
07-07 16:53:21.880: W/OpenGLRenderer(1941): Failed to set EGL_SWAP_BEHAVIOR on surface 0xac0b2ee0, error=EGL_SUCCESS
代码是:
@SuppressWarnings("unchecked")
private void addflags() throws ParserConfigurationException, SAXException, IOException{
ArrayList<Flag> flagList = new ArrayList<Flag>();
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
InputSource source = new InputSource();
source.setCharacterStream(new StringReader((getResources().getStringArray(R.array.flags)).toString()));
org.w3c.dom.Document doc = db.parse(source); // ActivityQuizScreen.java:253 is about here
NodeList nodes = doc.getElementsByTagName("item");
for(int i = 0; i< nodes.getLength();i++)
{
NamedNodeMap attr = nodes.item(i).getAttributes();
String name = attr.getNamedItem("name").getNodeValue();
String CountryCode = attr.getNamedItem("pic").getNodeValue().toLowerCase();
int pic = getResources().getIdentifier(CountryCode, "drawable", getPackageName());
Flag flag = new Flag(
attr.getNamedItem("name").getNodeValue(),
getResources().getIdentifier(CountryCode, "drawable", getPackageName()));
flagList.add(flag);
}
数组列表文件如:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="flags">
<item pic="AF" name="Afghanistan" />
<item pic="AL" name="Albania" />
<item pic="DZ" name="Algeria" />
<item pic="AS" name="American Samoa" />
<item pic="AD" name="Andorra" />
: : :
<item pic="YE" name="Yemen" />
<item pic="ZM" name="Zambia" />
<item pic="ZW" name="Zimbabwe" />
<item pic="AX" name="Åland Islands" />
</string-array>
</resources>
我如何解决这个问题,还是有更好的解决方案从文件中加载arrayl列表?有人用plzzzzzz吗?
谢谢!
答案 0 :(得分:0)
看起来您正在尝试在java字符串数组上执行XML解析。 我相信你正在寻找类似的东西:
ArrayList<String> flagList = new ArrayList<String>(Arrays.asList(getResources().getStringArray(R.array.flags)));
答案 1 :(得分:0)
1)您需要创建资产文件夹: Where do I place the 'assets' folder in Android Studio?
2)在资源文件夹中添加此xml文件(示例名称:flag_array.xml):
资源&GT;
<string-array name="flags">
<item pic="AF" name="Afghanistan" />
<item pic="AL" name="Albania" />
<item pic="DZ" name="Algeria" />
<item pic="AS" name="American Samoa" />
<item pic="AD" name="Andorra" />
<item pic="YE" name="Yemen" />
<item pic="ZM" name="Zambia" />
<item pic="ZW" name="Zimbabwe" />
<item pic="AX" name="Åland Islands" />
</string-array>
/资源&GT;
3)用addflags()方法替换你的代码:
ArrayList<Flag> flagList = new ArrayList<Flag>();
DocumentBuilder db = null;
try {
db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
AssetManager assetManager = getAssets();
InputStream is = assetManager.open("flag_array.xml");
org.w3c.dom.Document doc = db.parse(is);
NodeList nodes = doc.getElementsByTagName("item");
for(int i = 0; i< nodes.getLength();i++) {
NamedNodeMap attr = nodes.item(i).getAttributes();
String name = attr.getNamedItem("name").getNodeValue();
String CountryCode = attr.getNamedItem("pic").getNodeValue().toLowerCase();
int pic = getResources().getIdentifier(CountryCode, "drawable", getPackageName());
Flag flag = new Flag(
attr.getNamedItem("name").getNodeValue(),
getResources().getIdentifier(CountryCode, "drawable", getPackageName()));
flagList.add(flag);
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
答案 2 :(得分:0)
For multi-language purposes:
1) You need to create an assets folder: http://stackoverflow.com/questions/18302603/where-to-place-assets-folder-in-android-studio.
2) Add this xml file (Example name: flag_array.xml) in your assets folder:
resources>
<string-array name="flags">
<item pic="AF" name="Afghanistan" />
<item pic="AL" name="Albania" />
<item pic="DZ" name="Algeria" />
<item pic="AS" name="American Samoa" />
<item pic="AD" name="Andorra" />
<item pic="FR" name="French" />
<item pic="ES" name="Spanish" />
<item pic="YE" name="Yemen" />
<item pic="ZM" name="Zambia" />
<item pic="ZW" name="Zimbabwe" />
<item pic="AX" name="Åland Islands" />
</string-array>
/resources>
3) In res folder, you need to create the values folders for each language: Example: values-es, values-fr, and in each value folder you need to create a xml with the strings that you want to translate.
4) In your ActivityQuizScreen Class, you can add a ListView:
ListView listView = (ListView) findViewById(R.id.lvItems);
And in the XML:
<!--multiLanguageTextView is used to show the change in the languages-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/multiLanguageTextView"
android:padding="32dp"
android:text="@string/title" />
<ListView
android:layout_below="@+id/multiLanguageTextView"
android:id="@+id/lvItems"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"/>
5) Replace your code in the addflags() method with this:
ArrayList<Flag> flagList = new ArrayList<Flag>();
DocumentBuilder db = null;
try {
db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
AssetManager assetManager = getAssets();
InputStream is = assetManager.open("flag_array.xml");
org.w3c.dom.Document doc = db.parse(is);
NodeList nodes = doc.getElementsByTagName("item");
for(int i = 0; i< nodes.getLength();i++) {
NamedNodeMap attr = nodes.item(i).getAttributes();
String name = attr.getNamedItem("name").getNodeValue();
String CountryCode = attr.getNamedItem("pic").getNodeValue().toLowerCase();
int pic = getResources().getIdentifier(CountryCode, "drawable", getPackageName());
Flag flag = new Flag(
name,
CountryCode,
pic);
flagList.add(flag);
}
//Set ListView Custom Adapter
FlagAdapterItem adapter = new FlagAdapterItem(MainActivity.this, flagList);
listView.setAdapter(adapter);
6) Custom Flag Adapter
public class FlagAdapterItem extends ArrayAdapter<Flag> {
private Context context;
private ArrayList<Flag> list;
public FlagAdapterItem(Context context, ArrayList<Flag> list){
super(context, 0, list);
this.context = context;
this.list = list;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Flag flagEntity = list.get(position);
if (convertView == null)
convertView = LayoutInflater.from(context).inflate(R.layout.row_item, parent, false);
TextView tvCountryName = (TextView)convertView.findViewById(R.id.tvCountryName);
tvCountryName.setText(flagEntity.getName());
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setLocale(flagEntity.getCountryCode());
}
});
return convertView;
}
//Method to change the user LOCALE value (Example: fr, es)
//After change it, the app going to read from de selected Locale automatically
// (Example: values-es, values-fr).
public void setLocale(String lang) {
Locale myLocale = new Locale(lang);
Resources res = context.getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
if (!conf.locale.getLanguage().equals(lang)) {
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
//Refresh actual activity but you can go to the next activity
((Activity) context).finish();
context.startActivity(((Activity) context).getIntent());
}
}
}
7) FlagAdapterItem xml Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tvCountryName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tvCountryName" />
</LinearLayout>