Android Studio:移动一个activity到单独的java类



我的原始代码如下:

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);
}
}

....................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................我想把GallertyActivity移动到另一个单独的类。我把一些部分复制到新类中。但它显示了一些错误,我做错了什么?

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: cannot find symbol
startActivityForResult(gallery, PICK_IMAGE);
^
symbol:   method startActivityForResult(Intent,int)
location: class GalleryActivity

我发现这里有几个问题

  1. GalleryActivity类不扩展AppCompatActivity(或任何其他类)

  2. GalleryActivity函数应a)以小写字母开头,b)属于ProfileActivity

  3. onActivityResult回调也属于ProfileActivity

根据你提供的信息,我对你的意图做出了假设。

相关内容

  • 没有找到相关文章

最新更新