Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
186 views
in Technique[技术] by (71.8m points)

android - java.lang.NoClassDefFoundError: java.util.Objects

I'm getting the following crash report in Android development Console. My app runs fine on the simulator or devices that I tried the app on, but for some reason on a Galaxy Nexus (Maguro) it doesn't run. I don't get any compile errors either.

java.lang.NoClassDefFoundError: java.util.Objects
    at com.nivelsonic.nivelsonic.MyTankActivity$5.onResponse(MyTankActivity.java:199)
    at com.nivelsonic.nivelsonic.MyTankActivity$5.onResponse(MyTankActivity.java:160)
    at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
    at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
    at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
    at android.os.Handler.handleCallback(Handler.java:730)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5103)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    at dalvik.system.NativeStart.main(Native Method)

MyTankActivity.java

public void drawTankStatus() {
        tankView = (TankView) this.findViewById(R.id.vMyTank);
        tvLocation = (TextView) this.findViewById(R.id.tvLocation);
        tvLevel = (TextView) this.findViewById(R.id.tvLevel);
        ivRssi = (ImageView) this.findViewById(R.id.ivRssi);
        ivSettings = (ImageView) this.findViewById(R.id.ivSettings);
        ivAlert = (ImageView) this.findViewById(R.id.ivAlert);

        final Response.Listener<String> responseListener = new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                try {

                    JSONObject jsonResponse = new JSONObject(response);
                    //int success = jsonResponse.getInt("success");
                    JSONArray tank_data = jsonResponse.getJSONArray("tank_data");

                    if (tank_data.length() > 0) {

                        int i;
                        for (i = 0; i < tank_data.length(); i++) {
                            //Log.v("Result--", "" + tank_data.getString(i));

                            JSONObject tankObj = tank_data.getJSONObject(0);

                            location = (String) tankObj.getString("Location");
                            color = (String) tankObj.getString("Color");
                            level = (int) tankObj.getInt("CurrentLevel");
                            rssi = (int) tankObj.getInt("RSSI");
                            phone = (String) tankObj.getString("Phone");
                            status = (int) tankObj.getInt("Status");

                            Integer numOfNotifications = tankObj.getInt("NumberOfNotifications");

                            if (numOfNotifications > 0) {
                                ivAlert.setImageResource(R.drawable.alert_red);
                            } else {
                                ivAlert.setImageResource(R.drawable.alert);
                            }

                            tvLocation.setText(location);

                            if (level <= 55) {
                                tvLevel.setTextColor(Color.parseColor("#064e7b"));
                            } else {
                                // colo rvariable gets set from a value calculated in A_get_tanks.php
                                if (Objects.equals("#FFFFAD", color)) {
                                    tvLevel.setTextColor(Color.parseColor("#064e7b"));
                                } else {
                                    tvLevel.setTextColor(Color.parseColor("#FFFFFF"));
                                }
                            }

                            String levelPercent = Integer.toString(level);

                            tvLevel.setText(levelPercent + "%");

                            if (status != 0) {
                                switch (rssi) {
                                    case 0:
                                        ivRssi.setImageResource(R.drawable.rssi0);
                                        break;
                                    case 1:
                                        ivRssi.setImageResource(R.drawable.rssi1);
                                        break;
                                    case 2:
                                        ivRssi.setImageResource(R.drawable.rssi2);
                                        break;
                                    case 3:
                                        ivRssi.setImageResource(R.drawable.rssi3);
                                        break;
                                    case 4:
                                        ivRssi.setImageResource(R.drawable.rssi4);
                                        break;
                                    case 5:
                                        ivRssi.setImageResource(R.drawable.rssi5);
                                        break;
                                }
                            } else {
                                ivRssi.setImageResource(R.drawable.disconnected);
                            }

                            tankView.drawLevel(level, color);
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        };

        Intent intent = getIntent();
        userid = intent.getStringExtra("userid");
        impid = intent.getStringExtra("impid");

        MyTankRequest myTankRequest = new MyTankRequest(userid, responseListener);
        RequestQueue queue = Volley.newRequestQueue(MyTankActivity.this);
        queue.add(myTankRequest);

        if (ivSettings != null) {
            ivSettings.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent settingsIntent = new Intent(MyTankActivity.this, SettingsActivity.class);
                    settingsIntent.putExtra("impid", impid);
                    MyTankActivity.this.startActivity(settingsIntent);
                }
            });
        }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The exception is thrown because all static methods of java.util.Objects are available above API 19 (Android 4.4.+).

As you said in comments, your device has API 10 (Android 2.3.+) so that method doesn't exist in that Android version and NoClassDefFoundError is thrown.

If you want to check api level programmatically you can do:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    // your code available only above api 19
} else {
    // compatibility code
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...