Home > Software design >  Android Studio: move a activity to separate java class
Android Studio: move a activity to separate java class

Time:10-07

my original code is provide as below:

public class ProfileActivity extends AppCompatActivity {
ImageView imageView;
private static final int PICK_IMAGE = 100;
Uri imageUri;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);

    final ImageView exampleImage = (ImageView) this.findViewById(R.id.exampleImageView);
    exampleImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // [TODO] Implement application behavior when the user clicks the profile picture
            //Toast.makeText(ProfileActivity.this, "Button Clicked", Toast.LENGTH_SHORT).show();
            GalleryActivity();
        }
    });
}
private void GalleryActivity(){
    Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    startActivityForResult(gallery, PICK_IMAGE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == RESULT_OK && requestCode == PICK_IMAGE) {
        imageUri = data.getData();
        imageView.setImageURI(imageUri);
    }

}

.................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................... I want to move the GallertyActivity to another separate class. I did it by copying some parts to the new class. but it show some error, what did I do wrong?

import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;
import android.widget.ImageView;

public class GalleryActivity {
ImageView imageView;
private static final int PICK_IMAGE = 100;
Uri imageUri;

private void GalleryActivity(){
    Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    startActivityForResult(gallery, PICK_IMAGE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == RESULT_OK && requestCode == PICK_IMAGE) {
        imageUri = data.getData();
        imageView.setImageURI(imageUri);
    }

}

}

error message:

error: cannot find symbol
        startActivityForResult(gallery, PICK_IMAGE);
        ^
  symbol:   method startActivityForResult(Intent,int)
  location: class GalleryActivity

CodePudding user response:

I see a few problems here

  1. the GalleryActivity class does not extend AppCompatActivity (or any other class)

  2. the GalleryActivity function should a) start with a lowercase letter and b) belongs in the ProfileActivity class

  3. the onActivityResult callback also belongs in the ProfileActivity class

These points are based on assumptions I've made about your intentions given the info you've provided

CodePudding user response:

You need to extend GalleryActivity from AppCompatActivity like that: public class GalleryActivity extends AppCompatActivity. Because startActivityForResult comes from Activity class.

Or pass instance of ProfileActivity as a parameter to the new class and call startActivityForResult from its instance: profileActivity.startActivityForResult()

  • Related