带有KitKat的Android设备上的java.lang.NoClassDefFoundError

我正在开发一个应用程序,目前应用程序崩溃如果我使用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

解决方法:

我之前在两种情况下观察到此错误:

1.我正在使用具有启用ART的Kitkat的物理设备(即非仿真器).当我在使用Dalvik运行时的另一台Kitkat设备上运行应用程序时,错误似乎已经消失.

2.由于在我将SVN移动到另一个位置时发生的.dex构建错误,出现了同样的问题.我完全删除了旧项目,并在我的计算机上从原始项目创建了一个新的存储库,在完全重建项目后,错误没有重新出现.

上一篇:java-找不到类,即使它应该在类路径上


下一篇:java – Android:什么时候类被系统卸载?