java.lang.UnsupportedOperationException..crash

时间:2016-03-19 12:52:09

标签: java android google-api

我在应用程序启动然后崩溃时在logcat中收到此错误

03-19 15:41:34.644 5167-5167/com.example.abdulmajeedmohammad.insertertels E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.abdulmajeedmohammad.insertertels, PID: 5167
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.abdulmajeedmohammad.insertertels/com.example.abdulmajeedmohammad.insertertels.MainActivity}: java.lang.UnsupportedOperationException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3149)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3248)
    at android.app.ActivityThread.access$1000(ActivityThread.java:197)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1681)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:6872)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
 Caused by: java.lang.UnsupportedOperationException
    at java.util.AbstractList.add(AbstractList.java:404)
    at java.util.AbstractList.add(AbstractList.java:425)
    at android.widget.ArrayAdapter.add(ArrayAdapter.java:179)
    at com.example.abdulmajeedmohammad.insertertels.MainActivity.onCreate(MainActivity.java:83)
    at android.app.Activity.performCreate(Activity.java:6550)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3102)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3248) 
    at android.app.ActivityThread.access$1000(ActivityThread.java:197) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1681) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:145) 
    at android.app.ActivityThread.main(ActivityThread.java:6872) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 

这是活动类

public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks
    , GoogleApiClient.OnConnectionFailedListener {

private Spinner spinner;
private EditText name, number, fax, street;
private Button takePictureButton, sendButton;
private ArrayAdapter<String> listTypesAdapter;
private final static int REQUEST_IMAGE_CAPTURE = 1;
String type = "";
Bitmap photo;
Uri fileUri;
Uri selectedImage;
String bitmap1, picturePath;
GoogleApiClient googleApiClient;
Location lastLocation;
String location;
String Url = "";
RequestQueue requestQueue;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    buildGoogleApiClient();

    if (googleApiClient != null) {
        googleApiClient.connect();
    }

    spinner = (Spinner) findViewById(R.id.types);

    name = (EditText) findViewById(R.id.name);
    number = (EditText) findViewById(R.id.number);
    fax = (EditText) findViewById(R.id.fax);
    street = (EditText) findViewById(R.id.street);
    takePictureButton = (Button) findViewById(R.id.takePictureButton);

    String[] list = getResources().getStringArray(R.array.types_array);

    listTypesAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
    listTypesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(listTypesAdapter);

        listTypesAdapter.add("الرجاء الإختيار");
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            switch (position) {

                case 0:
                    type = "جهة حكومية";
                    break;
                case 1:
                    type = "مستشفى - مستوصف";
                    break;
                case 2:
                    type = "مطعم";
                    break;
                case 3:
                    type = "جهة خيرية";
                    break;
                case 4:
                    type = "قطع غيار وزينة السيارات";
                    break;
                case 5:
                    type = "صيدلية";
                    break;
                case 6:
                    type = "تموينات - سوبر ماركت";
                    break;
                case 7:
                    type = "ملابس وكماليات";
                    break;
                case 8:
                    type = "ملابس وكماليات رجالية";
                    break;
                case 9:
                    type = "أجهزة الكترونية";
                    break;

            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });


}

private void takeImage() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        selectedImage = data.getData();
        photo = (Bitmap) data.getExtras().get("data");

        String[] filepath = {MediaStore.Images.Media.DATA};

        Cursor cursor = getContentResolver().query(selectedImage, filepath, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filepath[0]);
        picturePath = cursor.getColumnName(columnIndex);
        cursor.close();

        Bitmap photo = (Bitmap) data.getExtras().get("data");
        imageView = (ImageView) findViewById(R.id.tokenImage);
        imageView.setImageBitmap(photo);

    }
}

protected synchronized void buildGoogleApiClient() {


    googleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
}

@Override
public void onConnected(Bundle bundle) {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    lastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);

    location = "Latitude: " + String.valueOf(lastLocation.getLatitude()) + "Longitude: " + String.valueOf(lastLocation.getLongitude());

    }

    @Override
    public void onConnectionSuspended(int i) {

        location = "Suspended";
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        location = "Failed";
    }


private void send() {

    if (name.getText().toString().equalsIgnoreCase("") || name.getText() == null) {
        Toast.makeText(getApplicationContext(), "الرجاء إدخال الإسم", Toast.LENGTH_SHORT).show();
    } else if (number.getText().toString().equalsIgnoreCase("") || number.getText() == null) {
        Toast.makeText(getApplicationContext(), "الرجاء إدخال الرقم", Toast.LENGTH_SHORT).show();
    } else if (fax.getText().toString().equalsIgnoreCase("") || fax.getText() == null) {
        Toast.makeText(getApplicationContext(), "الرجاء إدخال الفاكس أو كتابة none", Toast.LENGTH_SHORT).show();
    } else if (street.getText().toString().equalsIgnoreCase("") || street.getText() == null) {
        Toast.makeText(getApplicationContext(), "الرجاء كتابة الشارع", Toast.LENGTH_SHORT).show();
    } else if (location.equals("Suspended")) {
        Toast.makeText(getApplicationContext(), "يرجى تشغيل الجي بي إس لتحديد الموقع", Toast.LENGTH_SHORT).show();
    } else if (location.equals("Failed")) {
        Toast.makeText(getApplicationContext(), "فشل في الحصول على الموقع", Toast.LENGTH_SHORT).show();
    } else if (type.equals("")){
        Toast.makeText(getApplicationContext(), "الرجاء اختيار نوع الموقع", Toast.LENGTH_SHORT).show();
    } else if (photo == null) {
        Toast.makeText(getApplicationContext(), "يرجى أخذ صورة للموقع", Toast.LENGTH_SHORT).show();
    } else {


        Bitmap bm = BitmapFactory.decodeFile(picturePath);
        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.JPEG, 50, bao);
        byte[] by = bao.toByteArray();
        bitmap1 = Base64.encodeToString(by, Base64.DEFAULT);

        requestQueue = Volley.newRequestQueue(this);

        StringRequest request = new StringRequest(Request.Method.POST, Url,

                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                        AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
                        alertDialog.setMessage("تم الإرسال بنجاح");
                        alertDialog.setPositiveButton("موافق", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                finish();
                            }
                        });
                      alertDialog.show();

                        name.setText("");
                        number.setText("");fax.setText("");street.setText("");
                        imageView.setImageBitmap(null);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                        Toast.makeText(getApplicationContext(),"حدث خطأ في الإرسال", Toast.LENGTH_SHORT).show();

                    }
                }


        );
      requestQueue.add(request);
    }

}
}

1 个答案:

答案 0 :(得分:1)

如果您将常规add()传递给构造函数,则只能在insert()上使用remove()ArrayAdapterArrayList。如果您传入Java数组(正如您所做)或使用Arrays.asList()将数组转换为List,它们将无法工作。