Android Studio - Cannot resolve method 'setText(java.lang.Object)' - java

I Am Getting An Error in my code That Says Cannot resolve method 'setText(java.lang.Object)'. Does anyone know what causes this error and how i can fix it?
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//String str_phonenumber = txt_number.toString();
//str_phonenumber = str_phonenumber.replaceAll("\\D+","");
ArrayList phonenum = new ArrayList(results.get(0).getPhoneNumbers());
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == CONTACT_PICKER_REQUEST){
if(resultCode == RESULT_OK) {
results = MultiContactPicker.obtainResult(data);
StringBuilder names = new StringBuilder(results.get(0).getDisplayName());
for (int j=0;j<results.size();j++){
if(j!=0)
names.append(", ").append(results.get(j).getDisplayName());
}
//txt_number.set(phonenum);
txt_number.setText(phonenum.get(0)); //ERROR HERE: Cannot resolve method 'setText(java.lang.Object)'
Log.d("MyTag", results.get(0).getDisplayName());
} else if(resultCode == RESULT_CANCELED){
System.out.println("User closed the picker without selecting items.");
}
}

The reason for index out of bound is because your arraylist is empty but you are trying to get the first element of an empty array!!

Related

How to croping image with source just from camera with Canhub Android Image Cropper with Java

I want to croping image from other activity to another activity with Canhub Android Image Cropper library. This is my code :
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
mCropImageUri = result.getOriginalUri();
createImage();
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
AppLogger.e(result.getError().getMessage());
}
}
if (requestCode == PermissionCheckUtils.LOCATION_PERMISSION_REQUEST_CODE && resultCode == RESULT_OK) {
getLocation();
}
}
and this when i access the camera :
private void openCropImage() {
Intent intent = CropImage.activity().setGuidelines(CropImageView.Guidelines.ON).getIntent(this);
startActivityForResult(intent, CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE);
}
with these code, i open camera but source include galery. My question is how to open the croper with source just from camera. I already read the documentation but I'm confused : https://github.com/CanHub/Android-Image-Cropper
It is very simple !!
In Your openCropImage() Function set following code :
Intent intent = CropImage
.activity()
.setImageSource(includeGallery = false, includeCamera = true)
.setGuidelines(CropImageView.Guidelines.ON)
.getIntent(this);
startActivityForResult(intent, CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE);
That's it !! Happy Coding :-)

Error: Required: Image, Found:Image & Parcelable

While trying to get multiple selected images from gallery, there is a compatible issue as shown in screenshot when i use image array list. What could be the solution?
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_OK)
return;
if (requestCode == ConstantsCustomGallery.REQUEST_CODE && resultCode == Activity.RESULT_OK && data != null) {
//The array list has the image paths of the selected images
ArrayList<Image> images = data.getParcelableArrayListExtra(ConstantsCustomGallery.INTENT_EXTRA_IMAGES);
for (int i = 0; i < images.size(); i++) {
Uri uri = Uri.fromFile(new File(images.get(i).path));
// start play with image uri
}
As i said in comments You need to import the right Image .
import in.myinnos.awesomeimagepicker.models.Image;
instead of
import android.media.Image;

Need help on how to use onActivityResult() on Adapter Class?

I tried to return a phone number from a contact list on my adapter class, when i use super.onActivityResult(requestCode, resultCode, data); I got error.
btnContactGift.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
// Show only contacts with phone numbers
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
// Start the Contacts activity
context.startActivityForResult(intent, PICK_CONTACT);
}
});
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case PICK_CONTACT :
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
String[] projection = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Photo.PHOTO_THUMBNAIL_URI};
Cursor c = conR.query(contactData, projection, null, null, null);
c.moveToFirst();
int nameIdx =c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int phoneNumberIdx =c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int photoIdx = c.getColumnIndex(ContactsContract.CommonDataKinds.Photo.PHOTO_THUMBNAIL_URI);
String name = c.getString(nameIdx);
String phoneNumber = c.getString(phoneNumberIdx);
String photo = c.getString(photoIdx);
if (name == null) {
name = "No Name";
}
String nwPhone = phoneNumber.replace("+251", "0");
edtPhoneGift.setText(nwPhone);
c.close();
// Now you have the phone number
}
break;
}
}
Can not resolve method onActivityResult(int, int, Intent)
onActivityResult() needs to be implemented on the activity or fragment on which you call startActivityForResult(). In your case, that would be whatever activity or fragment is identified by context (from context.startActivityForResult(intent, PICK_CONTACT)).
Just delete the call to super super.onActivityResult(requestCode, resultCode, data), you don't need that.
Also, you need to change the ContactsContract.CommonDataKinds.Photo.PHOTO_THUMBNAIL_URI in your projection to something else, you can get Photo.XXX fields from the Uri returned from a Phone Picker intent, only columns within Phone.XXX or implicitly joined to it, you can try using Contacts.PHOTO_ID instead.

Android onActivityResult() doesn't get called

I've got a problem I don't know how to solve.
This is my code of the MainActivity.java which starts a new activity:
Intent intent = new Intent(this, AssociationActivity.class);
intent.putExtra("data", json);
startActivityForResult(intent, 0);
Additionaly, I have the following onActivityResult()-method:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
System.out.print("works outside!");
if (requestCode == 1) {
if(resultCode == RESULT_OK){
System.out.print("works inside!");
}
if (resultCode == RESULT_CANCELED) {
}
}
}
And this is where the result should be sent to the MainActivity:
Intent returnIntent = new Intent();
returnIntent.putExtra("result", string);
setResult(RESULT_OK, returnIntent);
finish();
It seems that the onActivityResult() doesn't even get called as I don't have any output on the console.
In the AndroidManifest.xml I neither use the singleIntent nor the noHistory parameter.
Any suggestions why it doesn't work?
requestCode change from 1 to 0. as shown below
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
System.out.print("works outside!");
if (requestCode == 0) {
if(resultCode == RESULT_OK){
System.out.print("works inside!");
}
if (resultCode == RESULT_CANCELED) {
}
}
}
Okay, I found the issue, the wasn't any issue at all, I replaced the System.out statements with the Log from Android it's working all the time, Android just delayed the System.out, but I don't know why.
Thanks #Rene Juuse for that tip!

Picture File on RESULT_CANCELED

I want to be able to track image file names when a picture has been taken with the default Camera Glassware. This is so I can delete them when finished. I have the following code:
...
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, TAKE_PICTURE_REQUEST);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TAKE_PICTURE_REQUEST && resultCode == RESULT_OK) {
String imgPath = data.getStringExtra(Intents.EXTRA_PICTURE_FILE_PATH);
mService.addToImageQueue(imgPath);
}
else if (requestCode == TAKE_PICTURE_REQUEST && resultCode == RESULT_CANCELED) {
...
}
}
If I tap, the resultCode returns RESULT_OK. When I dismiss (swipe down), I get the resultCode RESULT_CANCELED. This is how I intended it to work, except it still generates the image file even if the resultCode is RESULT_CANCELED... I honestly feel like this might be a bug since I tried to use data.getStringExtra(Intents.EXTRA_PICTURE_FILE_PATH); and got a NullPointerException. Am I doing something wrong? Is there a way to get this file name even on RESULT_CANCELED?
You could create a temporary file first (look at the createImageFile() method in this tutorial). If successfully created, do two things:
Save the path of this file to a String.
Include this file's URI in the intent extra (putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile))).
If resultCode is RESULT_CANCELED, you can now trace back to the path of the temporary file and call delete() on it.
Here is some sample code:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
Log.v("MainActivity", "Result successful.");
} else if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_CANCELED) {
Log.v(TAG, "Result canceled. Uri of file is " + mCurrentPhotoPath);
File file = new File(mCurrentPhotoPath);
if (file.exists()) {
Log.v(TAG, "File exists.");
if(file.delete()) {
Log.v(TAG, "File was successfully deleted!");
} else {
Log.v(TAG, "File not successfully deleted.");
}
} else {
Log.v(TAG, "File does not exist!");
}
}
}
Note: For new File(mCurrentPhotoPath) to work, remove "file:" from the beginning of mCurrentPhotoPath.

Categories

Resources