我正在致力于实现打印收据的应用程序。 我使用 Printooth 库来实现此实现,但遇到了 2 个无法继续的问题。 我有如下所示的按钮,当我单击它时,首先搜索并让您与蓝牙设备配对,然后当配对时再次单击,打印一些文本。 问题有 2 个,按顺序排列有:
`E/ActivityThread:活动 com.mazenrashed.printooth.ui.ScanningActivity 泄露了最初在此处注册的 IntentReceiver com.mazenrashed.printooth.utilities.Bluetooth$3@dd8369f。您是否错过了对 unregisterReceiver() 的调用? android.app.IntentReceiverLeaked:活动 com.mazenrashed.printooth.ui.ScanningActivity 泄露了最初在此处注册的 IntentReceiver com.mazenrashed.printooth.utilities.Bluetooth$3@dd8369f。您是否错过了对 unregisterReceiver() 的调用?
---------崩溃开始 E/AndroidRuntime:致命异常:main 进程:com.example.testaapplication0209,PID:3667 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void com.mazenrashed.printooth.utilities.Printing.print(java.util.ArrayList)”
完整代码:
package com.example.testaapplication0209;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.mazenrashed.printooth.Printooth;
import com.mazenrashed.printooth.data.printable.Printable;
import com.mazenrashed.printooth.data.printable.RawPrintable;
import com.mazenrashed.printooth.data.printable.TextPrintable;
import com.mazenrashed.printooth.data.printer.DefaultPrinter;
import com.mazenrashed.printooth.ui.ScanningActivity;
import com.mazenrashed.printooth.utilities.Printing;
import com.mazenrashed.printooth.utilities.PrintingCallback;
import java.util.ArrayList;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
public class PrintActivity extends AppCompatActivity {
private Printing printing = null;
private PrintingCallback printingCallback = null;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_print);
if (Printooth.INSTANCE.hasPairedPrinter())
printing = Printooth.INSTANCE.printer();
initViews();
initListeners();
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!Printooth.INSTANCE.hasPairedPrinter())
startActivityForResult(new Intent(PrintActivity.this, ScanningActivity.class), ScanningActivity.SCANNING_FOR_PRINTER);
else
printText();
}
});
}
private void initListeners() {
if (printing!=null && printingCallback==null) {
Log.d("xxx", "initListeners ");
printingCallback = new PrintingCallback() {
public void connectingWithPrinter() {
Toast.makeText(getApplicationContext(), "Connecting with printer", Toast.LENGTH_SHORT).show();
Log.d("xxx", "Connecting");
}
public void printingOrderSentSuccessfully() {
Toast.makeText(getApplicationContext(), "printingOrderSentSuccessfully", Toast.LENGTH_SHORT).show();
Log.d("xxx", "printingOrderSentSuccessfully");
}
public void connectionFailed(@NonNull String error) {
Toast.makeText(getApplicationContext(), "connectionFailed :"+error, Toast.LENGTH_SHORT).show();
Log.d("xxx", "connectionFailed : "+error);
}
public void onError(@NonNull String error) {
Toast.makeText(getApplicationContext(), "onError :"+error, Toast.LENGTH_SHORT).show();
Log.d("xxx", "onError : "+error);
}
public void onMessage(@NonNull String message) {
Toast.makeText(getApplicationContext(), "onMessage :" +message, Toast.LENGTH_SHORT).show();
Log.d("xxx", "onMessage : "+message);
}
};
Printooth.INSTANCE.printer().setPrintingCallback(printingCallback);
}
}
private void initViews() {
if (Printooth.INSTANCE.getPairedPrinter()!=null)
Toast.makeText(getApplicationContext(), (Printooth.INSTANCE.hasPairedPrinter())?("Un-pair "+ Printooth.INSTANCE.getPairedPrinter().getName()):"Pair with printer", Toast.LENGTH_SHORT).show();
}
private void printText() {
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: Jagadeesh")
.setCharacterCode(DefaultPrinter.Companion.getCHARCODE_PC1252())
.setNewLinesAfter(1)
.build());
printables.add((new TextPrintable.Builder())
.setText(" Hello World : été è à '€' içi Bò Xào Coi Xanh")
.setCharacterCode(DefaultPrinter.Companion.getCHARCODE_PC1252())
.setNewLinesAfter(1)
.build());
//Custom Text
printables.add((new TextPrintable.Builder())
.setText("Hello World")
.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);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d("xxx", "onActivityResult "+requestCode);
if (requestCode == ScanningActivity.SCANNING_FOR_PRINTER && resultCode == Activity.RESULT_OK) {
initListeners();
}
initViews();
}
}
我还有一个应用程序类,并且在manifest.xml中得到所有许可
现在谁能解决这个问题吗?
`
您设置了应用程序课程吗?如果不: 在主包中定义一个应用程序类(ApplicationClass.java)并初始化 Printooth 引擎,如下所示;
public class ApplicationClass extends Application {
@Override
public void onCreate() {
super.onCreate();
// Use class name (ApplicationClass.this) instead of "this" reference
// I experienced a failure using "this" reference
Printooth.INSTANCE.init(ApplicationClass.this);
}
}