在android中导出图表时为空指针

时间:2012-12-03 09:58:41

标签: android

电子邮件适用于小图表,但大图表无法导出并提供空指针异常     案例电子邮件:

try {

  new LoadAllData().execute();
  pDialog.setMessage("Please wait..");

             doExport();

            } catch (IOException e) {
                e.printStackTrace();

                dialog = new Dialog(testActivity.this);
                dialog.setContentView(R.layout.description);
                dialog.show();
                dialog.setTitle("Access Denied!");
                description = (TextView) dialog.findViewById(R.id.descriptionText);
                description.setText("Cannot export Chart: " +    e.getMessage());
                description.setTextColor(0xFFFFFFFF);
                okButton = (Button) dialog.findViewById(R.id.okButton);
                okButton.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.cancel();
                    }
                });
            }

导出代码

  private void doExport() throws IOException {
    final Intent sendIntent = new Intent(Intent.ACTION_SEND);
    final Image image = chart.getExport().getImage()
            .image(chart.getWidth(), chart.getHeight());

    File file;
    final FileOutputStream stream;

    file = new File("/sdcard/test.png");
    stream = new FileOutputStream(file);

    image.save(stream);
    stream.flush();
    stream.close();

    sendIntent.setType("image/png");
    sendIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse("file:///sdcard/test.png"));

    startActivity(Intent.createChooser(sendIntent, "Export"));
}

这是logcat

  12-03 13:59:32.174: E/AndroidRuntime(11871): java.lang.NullPointerException
  12-03 13:59:32.174: E/AndroidRuntime(11871): at    com.sympo.test.android.testActivity.doExport(testActivity.java:8261)
 12-03 13:59:32.174: E/AndroidRuntime(11871):   at com.sympo.test.android.testActivity.MenuItemSelectedEvent(testActivity.java:8215)
 12-03 13:59:32.174: E/AndroidRuntime(11871):   at com.sympo.test.android.CustomMenuListener$1.onClick(CustomMenuListener.java:166)
 12-03 13:59:32.174: E/AndroidRuntime(11871):   at android.view.View.performClick(View.java:4101)
 12-03 13:59:32.174: E/AndroidRuntime(11871):   at android.view.View$PerformClick.run(View.java:17078)
 12-03 13:59:32.174: E/AndroidRuntime(11871):   at android.os.Handler.handleCallback(Handler.java:615)
 12-03 13:59:32.174: E/AndroidRuntime(11871):   at android.os.Handler.dispatchMessage(Handler.java:92)
 12-03 13:59:32.174: E/AndroidRuntime(11871):   at android.os.Looper.loop(Looper.java:155)
 12-03 13:59:32.174: E/AndroidRuntime(11871):   at android.app.ActivityThread.main(ActivityThread.java:5485)
 12-03 13:59:32.174: E/AndroidRuntime(11871):   at java.lang.reflect.Method.invokeNative(Native Method)
 12-03 13:59:32.174: E/AndroidRuntime(11871):   at java.lang.reflect.Method.invoke(Method.java:511)
 12-03 13:59:32.174: E/AndroidRuntime(11871):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)

我在doExport()方法上遇到空指针异常。我怀疑图表可能太大了,因为代码与其他小图表完美配合,任何人都知道如何去做它?

1 个答案:

答案 0 :(得分:0)

我通过导出包含我想要发送电子邮件的整个布局来解决它。请注意,您可以通过电子邮件发送任何您希望的布局...享受!!!!

 private void doExport() throws IOException {


RelativeLayout rl =(RelativeLayout)findViewById(R.id.testlayout);
    Bitmap b = Bitmap.createBitmap(rl.getWidth(), rl.getHeight(),Bitmap.Config.ARGB_8888);

    int weight,height;

    weight =  rl.getWidth();

    height =  rl.getHeight();

    Bitmap cs = Bitmap.createBitmap(weight, height, Bitmap.Config.ARGB_8888);

    Canvas c = new Canvas(cs);

 c.drawBitmap(b,0,0,null);

  rl.draw(c); 

  File file1 = new File("/sdcard/test.png");
   FileOutputStream fos = new FileOutputStream(file1);



  if (fos != null) {

         cs.compress(Bitmap.CompressFormat.PNG, 90, fos);

       fos.close();
   }
 Intent picMessageIntent = new Intent(android.content.Intent.ACTION_SEND);
 picMessageIntent.setType("message/rfc822");

 picMessageIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse("file:///sdcard/test.png"));

        startActivity(Intent.createChooser(picMessageIntent, "Export"));
   }