I am developing a app and at the moment the app crashes if I use Android KitKat but with Lollipop devices the app runs without crash. The error from 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)
The class is inside the package of the error:
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;
}
}
So again the app runs without crash using Android Lollipop, anyone an idea why it crashes on KitKat? My minSDKversion is 16, the targetSDKversion 21
I have observed this error in two situations previously:
1. I was using a physical device (i.e. not an emulator) that had Kitkat with ART enabled. When I ran the app on another Kitkat device with Dalvik runtime, the error seemed to have vanished.
2. The same problem arose due to a
.dex
build error which occurred when I shifted the SVN to another location. I deleted the old project entirely and created a new repository from the original project on my computer, and after rebuilding the project entirely the error did not reappear.please check in gradle may be this code help you
dexOptions { javaMaxHeapSize "4g" preDexLibraries = false }
This can be happen if your application class is not instantiating double check your Menifest.xml for application class entry . And Your application class should look like:
}
add dependency
compile 'com.android.support:multidex:1.0.1'
add
multiDexEnabled = true
option to your build.gradle under default config:Thats it.