java - 分享图片 Android nullPointerException
问题描述
我正在尝试与 android 共享图像,我在 resId 中恢复图像资源,然后在 shareImage 方法中使用它。类代码是这样的:
public class SinglePhotoActivity extends AppCompatActivity {
ImageView singlePhoto;
String image;
Button shareButton;
DBHelper db;
Integer userId;
DrawerLayout drawerLayout;
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_single_photo);
singlePhoto = (ImageView) findViewById(R.id.singlePhoto);
shareButton = (Button) findViewById(R.id.btn_share);
userId = Utils.USER.getID();
drawerLayout = findViewById(R.id.drawer_layout);
db = new DBHelper(this);
this.context = context;
//Set menu
TextView nome = (TextView) findViewById(R.id.menuName);
nome.setText(Utils.USER.getNAME() + " " + Utils.USER.getLASTNAME());
if (Utils.USER.getPROFILEPHOTO() != "") {
Bitmap bitmapProfile = ProfileImage.StringToBitMap(Utils.USER.getPROFILEPHOTO());
ImageView profileMenu = (ImageView) findViewById(R.id.menuProfileImage);
profileMenu.setImageBitmap(bitmapProfile);
}
//Recupero nome immagine e la mostro
image = getIntent().getStringExtra("imageName");
final int resID = getResources().getIdentifier(image, "drawable", getPackageName());
singlePhoto.setImageResource(resID);
shareButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
shareImage(getParent(), resID);
}
});
}
public static void shareImage(Activity context, Integer resource_id) {
Bitmap b = BitmapFactory.decodeResource(context.getResources(), resource_id);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(context.getContentResolver(), b, "Title", null);
Uri imageUri = Uri.parse(path);
share.putExtra(Intent.EXTRA_STREAM, imageUri);
context.startActivity(Intent.createChooser(share, "Select"));
}}
但不工作。我尝试了不同的上下文并在 shareImage 签名中更改了上下文中的活动类型,但问题仍然存在。我有这个错误。
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 23199
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.SinglePhotoActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at com.example.myapplication.SinglePhotoActivity.onCreate(SinglePhotoActivity.java:69)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
我无法解决问题并使其全部正常工作。我必须改变上下文吗?还是我错了?
解决方案
改变
shareImage(getParent(), resID);
至
shareImage(SinglePhotoActivity.this, resID);
推荐阅读
- java - 如何在 url 中查找子路径
- php - 如果满足单个条件,php将数组元素移到第一个
- python - 如果在 Windows 7 上同时运行,两个 python 应用程序会在一段时间后崩溃
- firebase - 创建 24 小时后删除 Firestore 文档
- c++ - 像 c# 中的 ConcurrentBag 一样,cpp(MFC) 中有哪些可用的选项?
- php - 如何对这个动态创建的表中的每一列求和,如 excel
- jenkins - Github jenkins webhook 集成失败
- ruby - Clojure 相当于 ruby 的#methods 方法?
- reactjs - React TypeScript:将 base64 字符串保存到 useState
- c# - 将 JSON 反序列化为 C# 对象