I'm very new in android. I don't know why I got this null pointer exception.I run my program on API 17 and it was performed without any problems but when I'm running on API 19 I got Null pointer Exception. in my app I just select an image from gallery and image's URI is saved in Database.all this happens in Fragment. manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sayres.myapplication7">
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />
<application
android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".mvp.view.MainListActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".mvp.view.profile.ProfileActivity"></activity>
</application>
my fragment class:
package com.example.sayres.myapplication7.mvp.view.profile;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import com.example.sayres.myapplication7.App;
import com.example.sayres.myapplication7.R;
import com.example.sayres.myapplication7.entity.Contact;
import java.io.IOException;
import de.hdodenhof.circleimageview.CircleImageView;
/**
* A simple {@link Fragment} subclass.
*/
public class EditProfileFragment extends Fragment {
private EditFragmentCallBack editFragmentCallBack;
private EditText fragmentEditEditTextName;
private EditText fragmentEditEditTextFamily;
private EditText fragmentEditEditTextPhoneNumber;
private static final int SELECT_FILE = 0;
public static final String TAG = "====>";
private AlertDialog.Builder builder;
private Uri selectedImageUri;
private CircleImageView fragmentEditPicture;
private Bitmap bm;
private Button fragmentEditBtUpdate;
private int id;
private int rowUpdate;
private String name;
private String family;
private String phoneNumber;
private Contact contact;
public EditProfileFragment() {
// Required empty public constructor
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
Log.d(TAG, "onAttach: is running");
editFragmentCallBack = (EditFragmentCallBack) context;
Log.d(TAG, "onAttach: editFragmentCallBack was initialized");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d(TAG, "onCreateView- EditFragment: is running");
View fragmentView = inflater.inflate(R.layout.fragment_edit_profile, container, false);
initFragment(fragmentView);
return fragmentView;
}
private void initFragment(View parent) {
/**
* referencing of fragment_edit_profile
*/
fragmentEditPicture = (CircleImageView) parent.findViewById(R.id.fragment_edit_picture);
fragmentEditEditTextName = (EditText) parent.findViewById(R.id.fragment_edit_editText_name);
fragmentEditEditTextFamily = (EditText) parent.findViewById(R.id.fragment_edit_editText_family);
fragmentEditEditTextPhoneNumber = (EditText) parent.findViewById(R.id.fragment_edit_editText_phone);
fragmentEditBtUpdate = (Button) parent.findViewById(R.id.fragment_edit_btn_save);
fragmentEditBtUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: yess");
id = contact.get_id();
name = fragmentEditEditTextName.getText().toString();
family = fragmentEditEditTextFamily.getText().toString();
phoneNumber = fragmentEditEditTextPhoneNumber.getText().toString();
Log.d(TAG, "onClick: name " + name);
Log.d(TAG, "onClick: family " + family);
contact = new Contact(id, name, family, phoneNumber, selectedImageUri.toString());
rowUpdate = App.getInstanceImplementation().updateContact(contact);
Log.i("==>", "btnUpdateContact: " + rowUpdate);
}
});
/**
* get contact by invoke from CallBack on ProfileActivity
*/
contact = editFragmentCallBack.getContact();
fragmentEditEditTextName.setText(contact.getName());
fragmentEditEditTextFamily.setText(contact.getFamily());
fragmentEditEditTextPhoneNumber.setText(contact.getPhonNumber());
}
public void selectImage() {
final CharSequence[] items = {"Choose from Library", "Cancel"};
builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Add Photo ");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals("Choose from Library")) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent.createChooser(intent, "Select File"), SELECT_FILE);
} else if (items[item].equals("Cancel")) {
}
}
});
builder.show();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == SELECT_FILE) {
onSelectFromGalleryResult(data);
}
}
}
public void onSelectFromGalleryResult(Intent data) {
if (data != null) {
bm = null;
try {
selectedImageUri = data.getData();
bm = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), data.getData());
} catch (IOException e) {
e.printStackTrace();
}
Log.d(TAG, "selectedImageUri " + selectedImageUri);
Bitmap photoBitMap = Bitmap.createScaledBitmap(bm, 40, 40, true);
fragmentEditPicture.setImageBitmap(photoBitMap);
}
}
public interface EditFragmentCallBack {
Contact getContact();
void finishProfile();
}
}
Exception:
AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.sayres.myapplication7, PID: 17962
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65536, result=-1, data=Intent { dat=file:///storage/emulated/0/Download/139137_(2560x1600).JPG }} to activity {com.example.sayres.myapplication7/com.example.sayres.myapplication7.mvp.view.profile.ProfileActivity}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:3351)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3394)
at android.app.ActivityThread.access$1300(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:590)
at com.example.sayres.myapplication7.mvp.view.profile.EditProfileFragment.onSelectFromGalleryResult(EditProfileFragment.java:196)
at com.example.sayres.myapplication7.mvp.view.profile.EditProfileFragment.onActivityResult(EditProfileFragment.java:171)
at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:176)
at android.app.Activity.dispatchActivityResult(Activity.java:5423)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3347)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3394)
at android.app.ActivityThread.access$1300(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
the exception occur onSelectFromGalleryResult(data);
and Bitmap photoBitMap = Bitmap.createScaledBitmap(bm, 40, 40, true);
line
Use the below mentioned updated
onSelectFromGalleryResult()
method:finally I fix this problem by this link
the problem was because of Intent in API<19 and >19.