java.lang.NullPointerException:尝试调用虚拟方法';android.content.Intent android.content.Intent.putExtra

java.lang.NullPointerException:尝试调用虚拟方法';android.content.Intent android.content.Intent.putExtra,android,android-input-method,Android,Android Input Method,我在这里做错了什么。我想不出来。我有一个输入法服务,我的键盘上有图像,当我点击一个图像时,我试图将该图像共享给其他应用程序 服务代码: 在onCreateInputView方法下 mActivityManager =(ActivityManager)this.getSystemService(Context.ACTIVITY_SERVICE); if(Build.VERSION.SDK_INT > 20){ mPackageName = thi

我在这里做错了什么。我想不出来。我有一个输入法服务,我的键盘上有图像,当我点击一个图像时,我试图将该图像共享给其他应用程序

服务代码: 在onCreateInputView方法下

mActivityManager =(ActivityManager)this.getSystemService(Context.ACTIVITY_SERVICE);



        if(Build.VERSION.SDK_INT > 20){
            mPackageName = this.mActivityManager.getRunningAppProcesses().get(0).processName;
         //Toast.makeText(this,mPackageName, Toast.LENGTH_LONG).show();
        }
        else{
            mPackageName = this.mActivityManager.getRunningTasks(1).get(0).baseActivity.getPackageName();
        }


        shareIntent = new Intent(Intent.ACTION_SEND);

        shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
        shareIntent.setType("image/png"); 
        shareIntent.setPackage(mPackageName);
从asynctask的onPost调用的方法

public void sendImage(int position)
    {

        File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath().toString()+"/FecapsImage");
        //folder.mkdirs();

        if(folder.exists())
        {
          //Save the path as a string value
          String extStorageDirectory = folder.toString();



            String fileName = MainActivity.imageName.get(position)+".png";

            File dataFile = new File(extStorageDirectory,fileName);

            //Intent shareIntent = new Intent(Intent.ACTION_SEND);


            shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(dataFile));
            //Intent new_intent = Intent.createChooser(shareIntent, "Share via");
            //new_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
            startActivity(shareIntent);



        }
        else
        {
            Toast.makeText(this, "Not Found", Toast.LENGTH_LONG).show();

        }


    }
异步任务: 在中的sendImage方法中出现异常

shareIntent.putExtra(Intent.EXTRA_流,Uri.fromFile(数据文件))

logcat
确保
Uri.fromFile(dataFile)
不是
null
您确定在sendImage运行之前调用了onCreateInputView吗?如果您希望每次都保持相同的意图,那么在onCreate或onResume中初始化它会更安全。onCreateInputView仅在屏幕上显示键盘或其他输入时调用。在以下行之前初始化shareIntent时不为null@RustamTry:shareIntent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(dataFile));我已经做到了,没有任何改变@Ben
@Override
    protected String doInBackground(String... params) {

        SaveImage(this.url);


        return null;
    }

       private String SaveImage(String urlstr) {

            String filepath = null;
            try
            {   
              URL url = new URL(urlstr);
              HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
              urlConnection.setDoOutput(false);
              urlConnection.setRequestMethod("GET");
              //urlConnection.setDoOutput(true);
              urlConnection.setInstanceFollowRedirects(false);
              urlConnection.connect();                  

              File SDCardRoot = new File(Environment.getExternalStorageDirectory().getAbsolutePath().toString()+"/FecapsImage");
              SDCardRoot.mkdirs();
              String filename=this.name+".jpg";   
              Log.i("Local filename:",""+filename);
              File file = new File(SDCardRoot,filename);
              if(file.createNewFile())
              {
                file.createNewFile();
              }                 
              FileOutputStream fileOutput = new FileOutputStream(file);
              InputStream inputStream = urlConnection.getInputStream();
              int totalSize = urlConnection.getContentLength();
              int downloadedSize = 0;   
              byte[] buffer = new byte[1024];
              int bufferLength = 0;
              while ( (bufferLength = inputStream.read(buffer)) > 0 ) 
              {                 
                fileOutput.write(buffer, 0, bufferLength);                  
                downloadedSize += bufferLength;                 
                Log.i("Progress:","downloadedSize:"+downloadedSize+"totalSize:"+ totalSize) ;
              }             
              fileOutput.close();
              if(downloadedSize==totalSize) filepath=file.getPath();    
            } 
            catch (MalformedURLException e) 
            {
              e.printStackTrace();
            } 
            catch (IOException e)
            {
              filepath=null;
              e.printStackTrace();
            }
            Log.i("filepath:"," "+filepath);

            return filepath;
        }


       @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        if(ct!=null)
        {
//      dialog = new ProgressDialog(ct);
//      dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);  
//      dialog.setMessage("loading...");
        //dialog.show();
        }



    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        //dialog.dismiss();

        this.ct.sendImage(this.pos);

    }   
    startActivity(shareIntent);
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Intent android.content.Intent.putExtra(java.lang.String, android.os.Parcelable)' on a null object reference
at MainActivity.sendImage(MainActivity.java:550)
at 

    ImageDownload.onPostExecute(ImageDownload.java:118)
    ImageDownload.onPostExecute(ImageDownload.java:1)
    at android.os.AsyncTask.finish(AsyncTask.java:632)
    at android.os.AsyncTask.access$600(AsyncTask.java:177)
    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5312)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)