Python中文网

一个关于 编程问题的解答网站.

有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法在startActivityForResult中识别int?

包com。实例触须

import 安卓.content.Intent;
import 安卓.net.Uri;
import 安卓.os.Bundle;
import 安卓.view.View;
import 安卓.widget.ImageButton;

import 安卓x.annotation.Nullable;
import 安卓x.appcompat.app.AppCompatActivity;




public class editprofile extends AppCompatActivity {
    ImageButton proileimagebutton;
    private static final int ImageRequest=1;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    proileimagebutton= findViewById(R.id.edit_profile);
    proileimagebutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_GET_CONTENT);
            intent.setType("imageeeeeee/");

在startActivityForResult中没有显示错误的ImageRequest。意味着我无法在此处识别图像请求

startActivityForResult(intent.ImageRequest);

        }
    });
    }

    }

共 (2) 个答案

  1. # 1 楼答案

    public class editprofile extends AppCompatActivity {
        ImageButton proileimagebutton;
        private static final int ImageRequest=1;
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //no content view? add like this setContentView(R.layout.yourViewLayout);
            proileimagebutton= findViewById(R.id.edit_profile);
            proileimagebutton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    intent.setType("image/*");
                    startActivityForResult(intent,ImageRequest);
    
                }
            });
        }
    
        @Override
        public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
    
            if (requestCode == ImageRequest&& resultCode == RESULT_OK) {
                // your ImageView.setUri(data.getData();      
            }
        }
    }
    
  2. # 2 楼答案

    您需要在“公共类editprofile扩展AppCompatActivity”上方声明int。 代码应该是

    private ImageButton proileimagebutton;
    
    
    private static final int ImageRequest=1;
    

    公共类editprofile扩展了AppCompatActivity{

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    proileimagebutton= findViewById(R.id.edit_profile);
    proileimagebutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_GET_CONTENT);
            intent.setType("imageeeeeee/");