【发布时间】:2015-05-13 13:17:48
【问题描述】:
经过长时间的搜索,我从here 找到了一些关于 android 打印的解释,我在下面做了一些代码,但它显示了空指针异常。请帮助我如何修复打印文档的空指针异常 这是我使用打印 API 所做的代码: 我的主要活动:
package com.example.customprint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.print.PrintManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Button printing_document;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
printing_document = (Button)findViewById(R.id.button1);
printing_document.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//start_printing();
PrintManager printmanager = (PrintManager)MainActivity.this.getSystemService(Context.PRINT_SERVICE);
String name = "MedeQuip";
//printmanager.print(name, new MyPrintDocumentAdapater(this), null);
printmanager.print(name, new MyPrintDocumentAdapater(), null);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
打印适配器:
package com.example.customprint;
import java.io.FileOutputStream;
import java.io.IOException;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.pdf.PdfDocument;
import android.graphics.pdf.PdfDocument.PageInfo;
import android.os.Bundle;
import android.os.CancellationSignal;
import android.os.ParcelFileDescriptor;
import android.print.PageRange;
import android.print.PrintAttributes;
import android.print.PrintDocumentAdapter;
import android.print.PrintDocumentInfo;
import android.print.pdf.PrintedPdfDocument;
public class MyPrintDocumentAdapater extends PrintDocumentAdapter {
PdfDocument pdfdocument;
Context context;
int pagewidth;
int pageheight;
int tota_pages=4;
@Override
public void onLayout(PrintAttributes oldAttributes,
PrintAttributes newAttributes,
CancellationSignal cancellationSignal,
LayoutResultCallback callback, Bundle extras) {
pdfdocument = new PrintedPdfDocument(context, newAttributes);
pageheight = newAttributes.getMediaSize().getHeightMils()/1000*72;
pagewidth = newAttributes.getMediaSize().getWidthMils()/1000*72;
if(cancellationSignal.isCanceled()){
callback.onLayoutCancelled();
return;
}
if(tota_pages>0){
PrintDocumentInfo.Builder information = new PrintDocumentInfo
.Builder("Lakshmansundeep")
.setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT)
.setPageCount(tota_pages);
PrintDocumentInfo information_build = information.build();
callback.onLayoutFinished(information_build, true);
}
else{
callback.onLayoutFailed("Error with page details");
}
}
@Override
public void onWrite(PageRange[] pageRanges,
ParcelFileDescriptor destination,
CancellationSignal cancellationSignal,
WriteResultCallback callback) {
// TODO Auto-generated method stub
for(int i=0;i<tota_pages;i++){
if(pageInRange(pageRanges,i)){
PageInfo newPage = new PageInfo.Builder(pagewidth, pageheight, i).create();
PdfDocument.Page page = pdfdocument.startPage(newPage);
if(cancellationSignal.isCanceled()){
callback.onWriteCancelled();
pdfdocument.close();
pdfdocument = null;
return;
}
drawPage(page,i);
pdfdocument.finishPage(page);
}
try{
pdfdocument.writeTo(new FileOutputStream(destination.getFileDescriptor()));
}catch(IOException e){
callback.onWriteFailed(e.toString());
}
finally{
pdfdocument.close();
pdfdocument = null;
}
callback.onWriteFinished(pageRanges);
}
}
private void drawPage(PdfDocument.Page page, int pageNumber) {
Canvas canvas = page.getCanvas();
pageNumber++;
int titleBaseLine = 72;
int Leftmargin = 54;
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(25);
canvas.drawText("This is a sample text print"+pageNumber,Leftmargin, titleBaseLine, paint);
paint.setTextSize(15);
canvas.drawText("This is a printing some Text from the android application by lakshmansundeep",Leftmargin,titleBaseLine+35,paint);
if(pageNumber%2 == 0)
paint.setColor(Color.RED);
else
paint.setColor(Color.GREEN);
PageInfo pageInfo = page.getInfo();
canvas.drawCircle(pageInfo.getPageWidth()/2, pageInfo.getPageHeight()/2, 150, paint);
}
private boolean pageInRange(PageRange[] pageRanges, int page) {
for(int i=0;i<pageRanges.length;i++){
if((page>=pageRanges[i].getEnd()))
return true;
}
return false;
}
}
这是我的日志:
01-02 08:07:13.810: E/AndroidRuntime(13986): FATAL EXCEPTION: main
01-02 08:07:13.810: E/AndroidRuntime(13986): Process: com.example.customprint, PID: 13986
01-02 08:07:13.810: E/AndroidRuntime(13986): java.lang.NullPointerException
01-02 08:07:13.810: E/AndroidRuntime(13986): at com.example.customprint.MyPrintDocumentAdapater.onWrite(MyPrintDocumentAdapater.java:80)
01-02 08:07:13.810: E/AndroidRuntime(13986): at android.print.PrintManager$PrintDocumentAdapterDelegate$MyHandler.handleMessage(PrintManager.java:812)
01-02 08:07:13.810: E/AndroidRuntime(13986): at android.os.Handler.dispatchMessage(Handler.java:102)
01-02 08:07:13.810: E/AndroidRuntime(13986): at android.os.Looper.loop(Looper.java:136)
01-02 08:07:13.810: E/AndroidRuntime(13986): at android.app.ActivityThread.main(ActivityThread.java:5017)
01-02 08:07:13.810: E/AndroidRuntime(13986): at java.lang.reflect.Method.invokeNative(Native Method)
01-02 08:07:13.810: E/AndroidRuntime(13986): at java.lang.reflect.Method.invoke(Method.java:515)
01-02 08:07:13.810: E/AndroidRuntime(13986): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-02 08:07:13.810: E/AndroidRuntime(13986): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-02 08:07:13.810: E/AndroidRuntime(13986): at dalvik.system.NativeStart.main(Native Method)
【问题讨论】:
标签: android api printing nullpointerexception