【发布时间】: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
【问题讨论】:
-
该错误表明您的方法存在冲突。你能 pst 你的活动/片段的一些代码吗?
-
@RoiDivon 已添加代码 sn-p
-
你把
@Override放了吗? -
顺便说一句:你可能想读一些关于代码风格的东西,尤其是。 Java 中的命名类/方法约定:oracle.com/technetwork/java/codeconventions-135099.html
标签: android