【问题标题】:Unable to define onActivityResult(int requestCode, int resultCode, Intent data)无法定义 onActivityResult(int requestCode, int resultCode, Intent data)
【发布时间】:2015-09-19 16:03:54
【问题描述】:

我正在尝试在我的应用程序中从图库中选择图像,为此我编写了从图库中选择图像的代码。但是当我尝试定义 onActivityResult(int requestCode, int resultCode, Intent data) 时,我得到了错误

onActivityResult(int requestCode, int resultCode, Intent data) in com.example.kmi_dev.fbloginsample 与 onActivityResult(int requestCode, int resultCode, Intent data) in android.app.Fragment

我应该如何定义onActivityResult。 这是代码sn-p

public class MyAccount extends Fragment {
    private Button button;
    private Button button1;
    private Button remove;
    private Button change;
    private EditText editName;
    private EditText editGender;
    private EditText editAge;
    private EditText editCountry;
    private EditText editDesccription;
    private EditText editSports;
    private EditText editFood;
    private String apikey;
    private ImageView add;
    private static int RESULT_LOAD_IMAGE = 1;
    String val = null, imgpath=null;
    // Session Manager Class
    SessionManager session;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


        // Session class instance
        session = new SessionManager(getActivity().getApplicationContext());

        // get user data from session
        HashMap<String, String> user = session.getUserDetails();


        // apikey
        apikey = user.get(SessionManager.KEY_api);

        final LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.my_account_a, null);
        button = (Button) layout.findViewById(R.id.account);
        button1 = (Button) layout.findViewById(R.id.profile);
        remove = (Button) layout.findViewById(R.id.remove);
        change = (Button) layout.findViewById(R.id.change);
        editName = (EditText) layout.findViewById(R.id.editName);
        editGender = (EditText) layout.findViewById(R.id.editGender);
        editAge = (EditText) layout.findViewById(R.id.editAge);
        editCountry = (EditText) layout.findViewById(R.id.editCountry);
        editDesccription = (EditText) layout.findViewById(R.id.editDescription);
        editFood = (EditText) layout.findViewById(R.id.editFood);
        editSports = (EditText) layout.findViewById(R.id.editSports);
        add = (ImageView) layout.findViewById(R.id.add);

        // create bitmap from resource
        Bitmap bm = BitmapFactory.decodeResource(getResources(),
                R.drawable.profile_blank_xlarge);

        // set circle bitmap
        ImageView mImage = (ImageView) layout.findViewById(R.id.image);
        mImage.setImageBitmap(getCircleBitmap(bm));
        mImage.setTag("profile_blank_xlarge");

        new FetchOperation().execute();

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    post();
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
        });

            if(!mImage.getTag().equals("profile_blank_xlarge")) {
                remove.setOnClickListener(new View.OnClickListener() {
                                              @Override
                                              public void onClick(View v) {
                                                  remove_image();
                                              }
                                          }

                );
            }
        else
            {
                remove.setEnabled(false);
            }
        change.setOnClickListener(new View.OnClickListener() {
                                      @Override
                                      public void onClick(View v) {
                                          change_image();
                                      }
                                  }

        );

        return layout;
    }

    //Functions for profile image



    private Bitmap getCircleBitmap(Bitmap bitmap) {
        final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
                bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        final Canvas canvas = new Canvas(output);

        final int color = Color.RED;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawOval(rectF, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);

        bitmap.recycle();

        return output;
    }

    // Function to remove Image
    public void remove_image()
    {

        // create bitmap from resource
        Bitmap bm = BitmapFactory.decodeResource(getResources(),
                R.drawable.com_facebook_profile_picture_blank_portrait);

        // set circle bitmap
        ImageView mImage = (ImageView) getView().findViewById(R.id.image);
        mImage.setImageBitmap(getCircleBitmap(bm));
        mImage.setTag("profile_blank_xlarge");

    }

    //Function to change image
    public  void  change_image()
    {
        Intent i = new Intent(
                Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

        startActivityForResult(i, RESULT_LOAD_IMAGE);

    }



    //Want to define onActivityResult here

【问题讨论】:

标签: android


【解决方案1】:

如果你只是调用startActivityForResult() 函数,那将调用FragmentstartActivityForResult() 函数,因为你是从它调用的。

因此您需要覆盖片段中的onActivityResult() 以接收其回调。

如果你需要在activity中回调,那么你需要调用like

getActivity().startActivityForResult()

【讨论】:

    猜你喜欢
    • 2012-06-07
    • 1970-01-01
    • 1970-01-01
    • 2016-01-27
    • 2016-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多