Хочу из галереи установить картинку в ImageView
, падает с ошибкой, как исправить?
LoadImage.java
public class LoadImage extends Activity {
private final String TAG = "log_tag";
private Context context;
private CircleImageView imageView;
public LoadImage(Context context, CircleImageView imageView) {
this.context = context;
this.imageView = imageView;
}
private static final int REQUEST_CAMERA = 1;
private static final int REQUEST_GALLERY = 2;
public void makePhoto() {
}
public void loadGallery(View view) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, REQUEST_GALLERY);// ошибка в этой строке
(пробовал менять на context.startActivityForResult(intent, REQUEST_GALLERY) но подсвечивает изначально как ошибка)
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_CAMERA:
break;
case REQUEST_GALLERY:
if (resultCode == RESULT_OK) {
try {
final Uri imageUri = data.getData();
final InputStream imageStream = getContentResolver().openInputStream(imageUri);
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
imageView.setImageBitmap(selectedImage);
} catch (FileNotFoundException e) {
e.getMessage();
}
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
}
ошибка
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.nikolai.engadgetnews, PID: 7851
java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference
at android.app.Activity.startActivityForResult(Activity.java:3918)
at android.app.Activity.startActivityForResult(Activity.java:3877)
at com.example.nikolai.engadgetnews.LoadImage.loadGallery(LoadImage.java:46)
at com.example.nikolai.engadgetnews.Profile.Profile$1.onMenuItemClick(Profile.java:96)
at android.support.v7.widget.PopupMenu$1.onMenuItemSelected(PopupMenu.java:108)
at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822)
at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:156)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:969)
at android.support.v7.view.menu.MenuPopup.onItemClick(MenuPopup.java:127)
at android.widget.AdapterView.performItemClick(AdapterView.java:310)
at android.widget.AbsListView.performItemClick(AbsListView.java:1145)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3042)
at android.widget.AbsListView$3.run(AbsListView.java:3879)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)