I'm trying to make an app for my project on android studio, but when I'm trying to run this java file, this file is crashing and app stopped working on my emulator.Im simply trying to import images from my default gallery to my app.
import ....
public class tenant_profile extends Fragment {
private final int SELECT_PHOTO = 1;
private ImageView imageView;
public tenant_profile() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.fragment_tenant_profile, container, false);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button pickImage = (Button) getView().findViewById(R.id.btn_pick);
pickImage.setOnClickListener(new OnClickListener() {
//enter code here
@Override
public void onClick(View view) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
}
});
}
@Override
public void onActivityResult ( int requestCode, int resultCode, Intent imageReturnedIntent){
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch (requestCode) {
case SELECT_PHOTO:
if (resultCode == Activity.RESULT_OK) {
try {
final Uri imageUri = imageReturnedIntent.getData();
final InputStream imageStream = getContext().getContentResolver().openInputStream(imageUri);
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
imageView.setImageBitmap(selectedImage);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
}
My exception is following:
01-29 08:04:13.233 1796-1796/? E/AndroidRuntime: FATAL EXCEPTION: main
01-29 08:04:13.233 1796-1796/? E/AndroidRuntime: java.lang.NullPointerException
01-29 08:04:13.233 1796-1796/? E/AndroidRuntime: at com.example.sanchit.helloworld.tenant_profile.onCreate(tenant_profile.java:55)
01-29 08:04:13.233 1796-1796/? E/AndroidRuntime: at android.support.v4.app.Fragment.performCreate(Fragment.java:1939)
01-29 08:04:13.233 1796-1796/? E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:988)
01-29 08:04:13.233 1796-1796/? E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.moveToState
(FragmentManager.java:1207)
01-29 08:04:13.233 1796-1796/? E/AndroidRuntime: at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
01-29 08:04:13.233 1796-1796/? E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1572)
01-29 08:04:13.233 1796-1796/? E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:493)
please help