分享图片 Android nullPointerException
Share Image Android nullPointerException
我正在尝试与 android 共享图像,我在 resId 中恢复图像资源,然后在 shareImage 方法中使用它。
class 代码是这样的:
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 签名中上下文中的 activity 类型,但问题仍然存在。
我有这个错误。
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);
我正在尝试与 android 共享图像,我在 resId 中恢复图像资源,然后在 shareImage 方法中使用它。 class 代码是这样的:
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 签名中上下文中的 activity 类型,但问题仍然存在。 我有这个错误。
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);