在这里我有一个奇怪的情况,我使用构建工具28,目标是SDK版本28,但最低级别为16。
当我使用目标级别10时,下面的代码运行良好(至少在Android 8上如此)
在最终版本的Android 6,7,8,9代码上有效,但在android 4.1上无效(未在Android 5上进行测试)
这是个例外(示例中的代码行有些偏离,因为我不得不删除一些注释):
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NoClassDefFoundError: com.some.appname.ConfigurationLoader$1
at com.some.appname.ConfigurationLoader.load(ConfigurationLoader.java:46)
at com.some.appname.SomeApplication.onCreate(SomeApplication.java:80)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:999)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4151)
at android.app.ActivityThread.access$1300(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1255)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
有问题的代码部分是这一段:
StartElementListener ol = new StartElementListener() {
@Override
public void start(Attributes attributes) {
int nameIndex = attributes.getIndex("name");
int valueIndex = attributes.getIndex("value");
String name = attributes.getValue(nameIndex);
String value = attributes.getValue(valueIndex);
configuration.put(name, value);
}
};
这是整个班级:
package com.some.appname;
import java.io.IOException;
import java.io.InputStream;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import android.content.Context;
import android.sax.Element;
import android.sax.RootElement;
import android.sax.StartElementListener;
import android.util.Log;
import android.util.Xml;
/* package */class ConfigurationLoader {
private static final String TAG = "ConfigurationLoader";
/**
* Loads options from xml file
*
* @param context
* Context in which file is stored
* @return Loaded configuration
* @throws IOException
* If io errors occurs
* @throws SAXException
* If config.xml is invalid
*/
/* package */static Configuration load(Context context) throws IOException,
SAXException {
Log.i(TAG, "Loading configuration");
final Configuration configuration = Configuration.getConfiguration();
RootElement root = new RootElement("configuration");
Element option = root.getChild("option");
StartElementListener ol = new StartElementListener() {
@Override
public void start(Attributes attributes) {
int nameIndex = attributes.getIndex("name");
int valueIndex = attributes.getIndex("value");
String name = attributes.getValue(nameIndex);
String value = attributes.getValue(valueIndex);
configuration.put(name, value);
}
};
option.setStartElementListener(ol);
InputStream in = context.getResources().openRawResource(R.raw.config);
Xml.parse(in, Xml.Encoding.UTF_8, root.getContentHandler());
return configuration;
}
}