我正在使用GeoNames Java 客户端在 Android 应用程序中自动完成地名:用户键入一些字符并出现建议列表。
还需要库jdom-1.0.jar来解析 GeoNames 获取的 XML 结果。
shrinkResources true
一切正常,除非我添加build.gradle。
在这一点上,一些关键类迷路了,我不知道是哪一个……我只知道WebService.search()
失败了,但我不明白为什么会失败。实际上java.rmi.RemoteException
是在这里请求的,但是在 Android 中不支持 RMI 库,因此ClassNotFoundException
会抛出 a 而不是异常堆栈跟踪。
PlaceFinder.java:
public class PlaceFinder extends AppCompatAutoCompleteTextView {
ToponymSearchCriteria searchCriteria;
public PlaceFinder( Context context, AttributeSet attributeSet ) {
super( context, attributeSet );
Adapter adapter = new Adapter(context, android.R.layout.simple_spinner_dropdown_item);
setAdapter(adapter);
WebService.setUserName("demo");
searchCriteria = new ToponymSearchCriteria();
searchCriteria.setMaxRows(3);
}
class Adapter extends ArrayAdapter<String> implements Filterable {
List<String> places;
Adapter( Context context, int layout ) {
super( context, layout );
places = new ArrayList<>();
}
@Override
public int getCount() {
return places.size();
}
@Override
public String getItem(int index) {
return places.get(index);
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering( CharSequence constraint ) {
FilterResults filterResults = new FilterResults();
if (constraint != null) {
searchCriteria.setNameStartsWith(constraint.toString());
try {
ToponymSearchResult searchResult = WebService.search(searchCriteria); // search() fails
places.clear();
for( Toponym toponym : searchResult.getToponyms() ) {
places.add( toponym.getName() + ", " + toponym.getCountryName() );
}
filterResults.values = places;
filterResults.count = places.size();
} catch( Exception e ) {
e.printStackTrace(); // The class "java.rmi.RemoteException" is requested but missing
}
}
return filterResults;
}
@Override
protected void publishResults( CharSequence constraint, FilterResults results ) {
if (results != null && results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
};
}
}
}
致命异常(追溯):
FATAL EXCEPTION: Filter
Process: com.example, PID: 32084
java.lang.NoClassDefFoundError: Failed resolution of: Ljava/rmi/RemoteException;
at com.example.PlaceFinder$Adapter$1.performFiltering(:57)
at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.os.HandlerThread.run(HandlerThread.java:61)
Caused by: java.lang.ClassNotFoundException: Didn't find class "java.rmi.RemoteException" on path: DexPathList[[zip file "/data/app/com.example-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at org.jdom.JDOMException.getNestedException(:294)
at org.jdom.JDOMException.getMessage(:144)
at java.lang.Throwable.getLocalizedMessage(Throwable.java:188)
at java.lang.Throwable.toString(Throwable.java:355)
at java.lang.Throwable.printStackTrace(Throwable.java:315)
at java.lang.Throwable.printStackTrace(Throwable.java:282)
at org.jdom.JDOMException.printStackTrace(:216)
at java.lang.Throwable.printStackTrace(Throwable.java:236)
at org.jdom.JDOMException.printStackTrace(:189)
at com.example.PlaceFinder$Adapter$1.performFiltering(:57)
at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.os.HandlerThread.run(HandlerThread.java:61)
Suppressed: java.lang.ClassNotFoundException: java.rmi.RemoteException
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 15 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
如何解决这个双层问题?