我是android的新手。我不知道为什么我得到了这个空指针异常。我在API 17上运行我的程序并且执行没有任何问题,但是当我在API 19上运行时,我得到了Null指针异常。 在我的应用程序中,我只是从图库中选择一个图像,图像的URI保存在Database.all中,这发生在Fragment中。 清单文件:
<?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>
我的片段类:
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();
}
}
例外:
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)
异常发生在onSelectFromGalleryResult(data);
和Bitmap photoBitMap = Bitmap.createScaledBitmap(bm, 40, 40, true);
行
答案 0 :(得分:0)
使用下面提到的更新mysql -uusername -ppassword database-name < file.sql
方法:
onSelectFromGalleryResult()
答案 1 :(得分:0)
最后我通过此link
解决了这个问题问题是因为API&lt; 19和&gt; 19中的Intent。