gpt4 book ai didi

android - 转换位图图像在 Activity 中有效,但在 fragment 上生成空引用错误

转载 作者:行者123 更新时间:2023-11-30 00:02:05 25 4
gpt4 key购买 nike

将位图转换为字符串并压缩位图在 Activity 中运行良好,但在 fragment Activity 中,它会生成空对象引用错误

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.news.androidapp, PID: 5777
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference
at com.news.androidapp.News_Yeb.convertBitmapToString(News_Yeb.java:161)
at com.news.androidapp.News_Yeb.onClick(News_Yeb.java:110)

扩展 fragment

public class News_Yeb extends Fragment implements OnClickListener {
Button bt_register;
TextInputLayout til_name;
ImageView iv_profile;
String name, profile;
RequestQueue requestQueue;
boolean IMAGE_STATUS = false;
Bitmap profilePicture;
View view;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.news_yeb, container, false);
til_name = view.findViewById(R.id.til_name_reg);
iv_profile = view.findViewById(R.id.im_profile);
bt_register = view.findViewById(R.id.bt_register);

iv_profile.setOnClickListener(this);
bt_register.setOnClickListener(this);

return view;
}

使用 switch case 添加两个 onClick 事件,一个用于 imageview,另一个用于提交按钮

@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.im_profile:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, 1000);
break;

case R.id.bt_register:
name = til_name.getEditText().getText().toString();
if ( validateName(name) )

{
final ProgressDialog progress = new ProgressDialog(getActivity());
progress.setTitle("Please Wait");
progress.setMessage("Creating Your Account");
progress.setCancelable(false);
progress.show();
convertBitmapToString(profilePicture);
RegisterRequest registerRequest = new RegisterRequest(name, profile, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i("Response", response);
progress.dismiss();
try {
if (new JSONObject(response).getBoolean("success")) {
Toast.makeText(getActivity().getApplicationContext(), "Account Successfully Created", Toast.LENGTH_SHORT).show();
finish();
} else
Toast.makeText(getActivity().getApplicationContext(), "Something Has Happened. Please Try Again!", Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
});
requestQueue.add(registerRequest);

}
break;
}

}

连接PHP/Mysql

public class RegisterRequest extends StringRequest {

private static final String REGISTER_URL = "..";
private Map<String, String> parameters;

public RegisterRequest(String name, String mobile, String email, String image, Response.Listener<String> listener) {
super(Method.POST, REGISTER_URL, listener, null);
parameters = new HashMap<>();
parameters.put("name", name);
parameters.put("image", image);
}
@Override
protected Map<String, String> getParams() throws AuthFailureError {
return parameters;
}
}

将位图图像转换为 ByteArrayOutputStream,然后将流转换为字节数组

private void convertBitmapToString(Bitmap profilePicture) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
profilePicture.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] array = byteArrayOutputStream.toByteArray();
profile = Base64.encodeToString(array, Base64.DEFAULT);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1000 && resultCode == Activity.RESULT_OK && data != null) {
//Image Successfully Selected
try {
//parsing the Intent data and displaying it in the imageview
Uri imageUri = data.getData();//Geting uri of the data

InputStream imageStream = getActivity().getApplicationContext().getContentResolver().openInputStream(imageUri);//creating an imputstrea
profilePicture = BitmapFactory.decodeStream(imageStream);//decoding the input stream to bitmap
iv_profile.setImageBitmap(profilePicture);
IMAGE_STATUS = true;//setting the flag
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}

最佳答案

发生这种情况是因为您的 profilePicture 是 null ,原因是,您的 Intent 的 onActivityResult 是在托管 Activity 中调用的,而不是在调用 fragment 类中调用的,这就是代码在 Activity 中工作而不是在 Activity 中工作的原因在 fragment 中;P

选项 1:

由于 Activity 获得 onActivityResult() 的结果,您将需要覆盖 Activity 的 onActivityResult() 并调用 super.onActivityResult() 以传播到相应的 fragment 以获得未处理的结果代码或所有 fragment 。

如果上述选项不起作用,请引用选项 2,因为它肯定会起作用。

选项 2:

fragment对onActivityResult函数的显式调用如下。

在父Activity类中重写onActivityResult()方法,在Fragment类中重写onActivityResult()方法调用如下代码。

在 Activity 类中:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.x);
fragment.onActivityResult(requestCode, resultCode, data);
}

在 fragment 类中:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// do your stuff
}

希望这能解决你的问题

关于android - 转换位图图像在 Activity 中有效,但在 fragment 上生成空引用错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49749077/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com