无法在startActivityForResult中识别int?

huangapple go评论71阅读模式
英文:

Can't identify int in startActivityForResult?

问题

package com.example.feelhut;

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

import androidx.annotation.Nullable;
import androidx.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/");
                There ImageRequest showing Error in startActivityForResult. Means I am not able to identify image request here.
                startActivityForResult(intent.ImageRequest);
            }
        });
    }
}
英文:

package com.example.feelhut;

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

import androidx.annotation.Nullable;
import androidx.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/");

There ImageRequest showing Error in startActivityForResult. Means I am not able to identify image request here.

startActivityForResult(intent.ImageRequest);

        }
    });
    }

    }

答案1

得分: 2

public class editprofile extends AppCompatActivity {
    ImageButton proileimagebutton;
    private static final int ImageRequest = 1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 没有内容视图?可以像这样添加: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) {
            // 在这里设置你的 ImageView 的 URI:imageView.setImageURI(data.getData());
        }
    }
}
英文:
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

得分: 0

需要在"public class editprofile extends AppCompatActivity"之前声明int。

代码应该是:

private ImageButton proileimagebutton;

private static final int ImageRequest=1;

public class editprofile extends 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/");
英文:

You need to declare int above "public class editprofile extends AppCompatActivity".
The code should be

private ImageButton proileimagebutton;


private static final int ImageRequest=1;

public class editprofile extends 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/");

huangapple
  • 本文由 发表于 2020年9月26日 21:21:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/64078161.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定