0

我正在尝试将图像发送回whatsapp,类似于将图像返回给whatsapp

我使用了链接中给出的解决方案,但我在该行中得到了一个空指针异常

   ((Activity) context).setResult(Activity.RESULT_OK, shareIntent); 

我将在这里发布我的完整代码:-

公共类 Lst1Fragment 扩展片段{

GridView gridView1;
public static boolean isinint = false;
Context context;
File openprev;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_xml_title, container, false);
    createListView(view);
    // Inflate the layout for this fragment
    return view;
}

private void createListView(View view)
{
    gridView1 = (GridView) view.findViewById(R.id.gridView1);
    //Set option as Multiple Choice. So that user can able to select more the one option from list
    gridView1.setAdapter(new ImageAdapter(getActivity()));

    gridView1.setOnItemClickListener(new OnItemClickListener() {


        @Override
        public void onItemClick(AdapterView<?> view, View arg1, int pos,
                long arg3) {

            Uri path = Uri.parse("android.resource://"
                    + getActivity().getPackageName() + "/"
                    + R.drawable.action);

            Intent startUpIntent = getActivity().getIntent();   


            if (startUpIntent!=null && startUpIntent.getType()!=null)  //check if any application has executed your app
            {
                 if(startUpIntent.getType().indexOf("image/") != -1)

                     isinint=true; //check if the requested type is an image. If true, set a public static boolean, f.e. named isinint to true. Default is false.
            }

            if(isinint) //check if any app cares for the result
            {
                Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND, path);
                        //Uri.fromFile(openprev)); 
                //Create a new intent. First parameter means that you want to send the file.
                //The second parameter is the URI pointing to a file on the sd card. (openprev has the datatype File)

                ((Activity) context).setResult(Activity.RESULT_OK, shareIntent); //set the file/intent as result
                ((Activity) context).finish(); //close your application and get back to the requesting application like GMail and WhatsApp
                return; //do not execute code below, not important
            }

        }
    });


}

public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    // Keep all Images in array
    public Integer[] mThumbIds = {
            R.drawable.action, R.drawable.comedy,             
            R.drawable.drama,  R.drawable.horror

    };

    // Constructor
    public ImageAdapter(Context c){
        mContext = c;
    }

    @Override
    public int getCount() {
        return mThumbIds.length;
    }

    @Override
    public Object getItem(int position) {
        return mThumbIds[position];
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(mContext);
        imageView.setImageResource(mThumbIds[position]);
        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        return imageView;
    }
}
}

我的完整堆栈跟踪:-

10-25 09:53:56.605: W/dalvikvm(2913): threadid=1: thread exiting with uncaught exception (group=0x2aacc560)
10-25 09:53:56.605: E/AndroidRuntime(2913): Uncaught handler: thread main exiting due to uncaught exception
10-25 09:53:56.615: W/System.err(2913): java.io.IOException: Permission denied
10-25 09:53:56.625: E/AndroidRuntime(2913): FATAL EXCEPTION: main
10-25 09:53:56.625: E/AndroidRuntime(2913): java.lang.NullPointerException
10-25 09:53:56.625: E/AndroidRuntime(2913):     at com.example.myfragments.Lst1Fragment$1.onItemClick(Lst1Fragment.java:68)
10-25 09:53:56.625: E/AndroidRuntime(2913):     at android.widget.AdapterView.performItemClick(AdapterView.java:284)
10-25 09:53:56.625: E/AndroidRuntime(2913):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
10-25 09:53:56.625: E/AndroidRuntime(2913):     at android.os.Handler.handleCallback(Handler.java:587)
10-25 09:53:56.625: E/AndroidRuntime(2913):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-25 09:53:56.625: E/AndroidRuntime(2913):     at android.os.Looper.loop(Looper.java:130)
10-25 09:53:56.625: E/AndroidRuntime(2913):     at android.app.ActivityThread.main(ActivityThread.java:3683)
10-25 09:53:56.625: E/AndroidRuntime(2913):     at java.lang.reflect.Method.invokeNative(Native Method)
10-25 09:53:56.625: E/AndroidRuntime(2913):     at java.lang.reflect.Method.invoke(Method.java:507)
10-25 09:53:56.625: E/AndroidRuntime(2913):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:895)
10-25 09:53:56.625: E/AndroidRuntime(2913):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:653)
10-25 09:53:56.625: E/AndroidRuntime(2913):     at dalvik.system.NativeStart.main(Native Method)
10-25 09:53:56.645: W/System.err(2913): java.io.FileNotFoundException: /data/plog.log (Permission denied)
4

1 回答 1

0

您尚未Context context;在课堂上的任何地方进行初始化,但您已将其类型转换为Activity这就是您获得 NPE 的原因

于 2013-10-25T05:07:41.437 回答