带有KitKat的Android设备上的java.lang.NoClassDefFoundError [英] java.lang.NoClassDefFoundError on Android devices with KitKat

查看:127
本文介绍了带有KitKat的Android设备上的java.lang.NoClassDefFoundError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,此刻如果我使用Android KitKat但使用Lollipop设备时该应用程序崩溃,则该应用程序运行时不会崩溃. LogCat中的错误:

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)

该类在错误包内:

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

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..我正在使用启用了ART的Kitkat的物理设备(即非仿真器).当我使用Dalvik运行时在另一个Kitkat设备上运行该应用程序时,该错误似乎消失了.

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..由于我将SVN移至另一个位置时发生了.dex构建错误,因此出现了相同的问题.我完全删除了旧项目,并从计算机上的原始项目中创建了一个新的存储库,并且在完全重建该项目之后,该错误不再出现.

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.

这篇关于带有KitKat的Android设备上的java.lang.NoClassDefFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆