我正在开发一个应用程序,如果我使用Android KitKat但是使用Lollipop设备,应用程序崩溃,应用程序运行时没有崩溃。来自LogCat的错误:
java.lang.NoClassDefFoundError: mypackage.utils.PlacesAutoCompleteAdapter$1
at mypackage.utils.PlacesAutoCompleteAdapter.getFilter(PlacesAutoCompleteAdapter.java:60)
at android.widget.AutoCompleteTextView.setAdapter(AutoCompleteTextView.java:635)
at mypackage.PlanRouteActivity.initializeLayout(PlanRouteActivity.java:148)
at mypackage.PlanRouteActivity.onCreate(PlanRouteActivity.java:83)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
该类位于错误包中:
package mypackage.utils;
public class PlacesAutoCompleteAdapter extends ArrayAdapter<String> implements Filterable {
private static final String TAG = "AutoCompleteAdapter";
// Autocomplete Google Places
private static final String PLACES_API_BASE = "https://maps.googleapis.com/maps/api/place";
private static final String TYPE_AUTOCOMPLETE = "/autocomplete";
private static final String OUT_JSON = "/json";
private static final String API_BROWSER_KEY = "AIzaSyD5JwM_VRieW2WmzPU_D4sl6YST0wuH6Io";
private ArrayList<String> resultList;
// Constructor
public PlacesAutoCompleteAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
@Override
public int getCount() {
return resultList.size();
}
@Override
public String getItem(int index) {
return resultList.get(index);
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if (constraint != null) {
// Retrieve the autocomplete results.
resultList = autocomplete(constraint.toString());
// Assign the data to the FilterResults
filterResults.values = resultList;
filterResults.count = resultList.size();
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, Filter.FilterResults results) {
if (results != null && results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
};
return filter;
}
/**
* Autocomplete text input with Google places
*
* @param input: Text input from AutoCompleteTextView
* @return ArrayList<String>
*/
private ArrayList<String> autocomplete(String input) {
ArrayList<String> resultList = null;
HttpURLConnection conn = null;
StringBuilder jsonResults = new StringBuilder();
try {
StringBuilder sb = new StringBuilder(PLACES_API_BASE + TYPE_AUTOCOMPLETE + OUT_JSON);
sb.append("?key=" + API_BROWSER_KEY);
sb.append("&components;=country:de");
sb.append("&input=" + URLEncoder.encode(input, "utf8"));
URL url = new URL(sb.toString());
conn = (HttpURLConnection) url.openConnection();
InputStreamReader in = new InputStreamReader(conn.getInputStream());
// Load the results into a StringBuilder
int read;
char[] buff = new char[1024];
while ((read = in.read(buff)) != -1) {
jsonResults.append(buff, 0, read);
}
} catch (MalformedURLException e) {
Log.e(TAG, "Error processing Places API URL", e);
return null;
} catch (IOException e) {
Log.e(TAG, "Error connecting to Places API", e);
return null;
} finally {
if (conn != null) {
conn.disconnect();
}
}
try {
// Create a JSON object hierarchy from the results
JSONObject jsonObj = new JSONObject(jsonResults.toString());
JSONArray predsJsonArray = jsonObj.getJSONArray("predictions");
// Extract the Place descriptions from the results
resultList = new ArrayList<String>(predsJsonArray.length());
for (int i = 0; i < predsJsonArray.length(); i++) {
resultList.add(predsJsonArray.getJSONObject(i).getString("description"));
}
} catch (JSONException e) {
Log.e(TAG, "Cannot process JSON results", e);
}
return resultList;
}
}
所以应用程序再次使用Android Lollipop运行时没有崩溃,任何人都知道它为何在KitKat上崩溃?我的minSDKversion是16,targetSDKversion 21
答案 0 :(得分:4)
如果您的应用程序类没有实例化,请检查您的Menifest.xml以获取应用程序类条目,这可能会发生。您的应用程序类应如下所示:
public class AppController extends MultiDexApplication {
public void onCreate() {
super.onCreate();
}
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
}
添加依赖compile 'com.android.support:multidex:1.0.1'
在默认配置下为build.gradle添加multiDexEnabled = true
选项:
defaultConfig {
applicationId "com.app"
minSdkVersion 19
targetSdkVersion 22
versionCode 1
versionName "1"
multiDexEnabled = true
}
多数民众赞成。
答案 1 :(得分:2)
我之前在两种情况下都观察到了这个错误:
1。我正在使用具有启用ART的Kitkat的物理设备(即非仿真器)。当我在使用Dalvik运行时的另一台Kitkat设备上运行应用程序时,错误似乎已经消失。
2。由于我将SVN转移到其他位置时出现.dex
构建错误,导致出现同样的问题。我完全删除了旧项目,并在我的计算机上从原始项目创建了一个新的存储库,完全重建项目后,错误没有重新出现。
答案 2 :(得分:0)
请检查gradle可能是此代码帮助您
dexOptions { javaMaxHeapSize“4g” preDexLibraries = false }