我在Android Studio应用程序上工作,最后在工作中我实现了一个类,尝试通过打印机蓝牙打印某些内容。
我使用printooth库,但是当需要进行这项新活动时,它给了我一个例外:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.testaapplication0209, PID: 11325
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testaapplication0209/com.example.testaapplication0209.PrintActivity}: io.paperdb.PaperDbException: Paper.init is not called
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2768)
这是我的PrintActivity:
public class PrintActivity extends AppCompatActivity implements PrintingCallback {
Printing printing;
Button btnPrint, btnPairUnpair;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_print);
initView();
}
private void initView() {
btnPrint = findViewById(R.id.btnPrint);
btnPairUnpair = findViewById(R.id.btnPairUnpair);
if (printing != null)
printing.setPrintingCallback(this);
//event
btnPairUnpair.setOnClickListener(view -> {
if (Printooth.INSTANCE.hasPairedPrinter())
Printooth.INSTANCE.removeCurrentPrinter();
else {
startActivityForResult(new Intent(PrintActivity.this, ScanningActivity.class), ScanningActivity.SCANNING_FOR_PRINTER);
changePairAndUnpair();
}
});
btnPrint.setOnClickListener(view -> {
if (!Printooth.INSTANCE.hasPairedPrinter())
startActivityForResult(new Intent(PrintActivity.this, ScanningActivity.class), ScanningActivity.SCANNING_FOR_PRINTER);
else {
printData();
}
});
changePairAndUnpair();
}
private void printData() {
ArrayList<Printable> printables = new ArrayList<>();
printables.add(new RawPrintable.Builder(new byte[]{27,100,4}).build());
//Add Text
printables.add(new TextPrintable.Builder()
.setText("Hello World").setCharacterCode(DefaultPrinter.Companion.getCHARCODE_PC1252())
.setNewLinesAfter(1)
.build()
);
//Custom Text
printables.add(new TextPrintable.Builder()
.setText("Hello World custom text")
.setLineSpacing(DefaultPrinter.Companion.getLINE_SPACING_60())
.setAlignment(DefaultPrinter.Companion.getALIGNMENT_CENTER())
.setEmphasizedMode(DefaultPrinter.Companion.getEMPHASIZED_MODE_BOLD())
.setUnderlined(DefaultPrinter.Companion.getUNDERLINED_MODE_ON())
.setNewLinesAfter(1)
.build());
printing.print(printables);
}
private void changePairAndUnpair() {
if (Printooth.INSTANCE.hasPairedPrinter()){
btnPairUnpair.setText(new StringBuilder("Dissocia").append(Printooth.INSTANCE.getPairedPrinter().getName().toString()));
} else {
btnPairUnpair.setText("Associa Stampante");
}
}
@Override
public void connectingWithPrinter() {
Toast.makeText(this, "Connesione alla stampante", Toast.LENGTH_SHORT).show();
}
@Override
public void connectionFailed(String s) {
Toast.makeText(this, "CFailerd: " +s, Toast.LENGTH_SHORT).show();
}
@Override
public void onError(String s) {
Toast.makeText(this, "Error: " +s, Toast.LENGTH_SHORT).show();
}
@Override
public void onMessage(String s) {
Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
}
@Override
public void printingOrderSentSuccessfully() {
Toast.makeText(this, "Ordine mandato alla stampante", Toast.LENGTH_SHORT).show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == ScanningActivity.SCANNING_FOR_PRINTER && resultCode == Activity.RESULT_OK)
initPrinting();
changePairAndUnpair();
}
private void initPrinting() {
if(!Printooth.INSTANCE.hasPairedPrinter())
printing = Printooth.INSTANCE.printer();
if (printing != null)
printing.setPrintingCallback(this);
}
}
在这里我实例化打印齿的ApplicationPrintActivity中:
public class ApplicationPrintActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Printooth.INSTANCE.init(this);
}
}
最佳答案
不是Android开发人员,但这似乎是一种误解。PrintActivity
直接扩展AppCompatActivity
,它不会“绕过” ApplicationPrintActivity#onCreate
,因此根本不会调用Paper.init
。
编辑PrintActivity#onCreate
以包括该呼叫。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Printooth.INSTANCE.init(this);
setContentView(R.layout.activity_print);
initView();
}