从API 19上的库中选择图像的空值 [英] null poiner on select image from gallery on API 19

查看:129
本文介绍了从API 19上的库中选择图像的空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android中很新。我不知道为什么我得到这个空指针异常。我运行我的程序在API 17,它执行没有任何问题,但是当我在API 19上运行我有空指针异常。
在我的应用程序我只是选择一个图像从图库和图像的URI保存在Database.all这发生在片段。
清单文件:

 <?xml version =1.0encoding =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>



我的片段类: p>

  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;

/ **
*一个简单的{@link Fragment}子类。
* /
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;
私人Uri selectedImageUri;
private CircleImageView fragmentEditPicture;
私人Bitmap bm;
private Button fragmentEditBtUpdate;
private int id;
private int rowUpdate;
private String name;
private String family;
private String phoneNumber;
私人联系人联系人


public EditProfileFragment(){
//必需的空公共构造函数
}

@Override
public void onAttach Context context){
super.onAttach(context);
Log.d(TAG,onAttach:is running);

editFragmentCallBack =(EditFragmentCallBack)context;
Log.d(TAG,onAttach:editFragmentCallBack已初始化);
}

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup容器,Bundle savedInstanceState){

Log.d(TAG,onCreateView- EditFragment:正在运行);
查看fragmentView = inflater.inflate(R.layout.fragment_edit_profile,container,false);
initFragment(fragmentView);
return fragmentView;
}


private void initFragment(查看父项){

/ **
*引用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){

日志.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);
日志联系人(id,name,family,phoneNumber,selectedImageUri.toString());
rowUpdate = App.getInstanceImplementation()。updateContact(contact);

Log.i(==&;btnUpdateContact:+ rowUpdate);

}
});


/ **
*通过CallBack通过ProfileActivity调用获取联系
* /
contact = editFragmentCallBack.getContact();

fragmentEditEditTextName.setText(contact.getName());
fragmentEditEditTextFamily.setText(contact.getFamily());
fragmentEditEditTextPhoneNumber.setText(contact.getPhonNumber());


}



public void selectImage(){
final CharSequence [] items = {从库中选择,取消};
builder = new AlertDialog.Builder(getActivity());

builder.setTitle(添加照片);
builder.setItems(items,new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog,int item){
if(items [item] .equals (从库中选择)){

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 (取消)){

}
}
});
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(意图数据){
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);


位图photoBitMap = Bitmap.createScaledBitmap(bm,40,40,true);
fragmentEditPicture.setImageBitmap(photoBitMap);
}

}


public interface EditFragmentCallBack {
联系getContact();

void finishProfile();

}

}

异常: / p>

  AndroidRuntime:FATAL EXCEPTION:main 
进程:com.example.sayres.myapplication7,PID:17962
java.lang.RuntimeException:提交结果失败ResultInfo {who = null,request = 65536,result = -1,data = Intent {dat = file:/// storage / emulated / 0 / Download / 139137_(2560x1600).JPG} }到activity {com.example.sayres.myapplication7 / com.example.sayres.myapplication7.mvp.view.profile.ProfileActivity}:java.lang.NullPointerException
在android.app.ActivityThread.deliverResults(ActivityThread.java :3351)
at android.app.ActivityThread.handleSendResult(ActivityThread.jav a: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)
在android.os.Looper.loop(Looper.java:136)
在android.app.ActivityThread.main(ActivityThread.java: 5001)
在java.lang.reflect.Method.invokeNative(Native Method)
在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)
引起的:java.lang.NullPointerException
at android.graphics.Bitmap.createScaledBitmap(Bitmap .java:590)
at com.example.sayres.myapplication7.mvp.view.profile.EditProfileFragment.onSelectFromGalleryResult(EditProfileFragment.java:196)
在com.example.sayres.myapplication7.mvp.view.profile.EditProfileFragment.onActivityResult(EditProfileFragment.java:171)
在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)
在android.app.ActivityThread.access $ 1300(ActivityThread.java:13 5)
在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1244)
在android.os.Handler.dispatchMessage(Handler.java:102)
在android.os .Looper.loop(Looper.java:136)
在android.app.ActivityThread.main(ActivityThread.java:5001)
在java.lang.reflect.Method.invokeNative(本机方法)
在java.lang.reflect.Method.invoke(Method.java:515)
在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); 位图photoBitMap = Bitmap.createScaledBitmap(bm,40,40,true);
line

解决方案

最后我通过这个链接



问题是因为Intent在API< 19和19中。


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

解决方案

finally I fix this problem by this link

the problem was because of Intent in API<19 and >19.

这篇关于从API 19上的库中选择图像的空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆