gpt4 book ai didi

java - Paper.init 未被调用

转载 作者:行者123 更新时间:2023-12-02 09:32:39 25 4
gpt4 key购买 nike

我正在开发 Android Studio 应用程序,在最后的工作中,我实现了一个尝试通过蓝牙打印机打印内容的类。我使用 printooth 库,但是当需要执行此新 Activity 时,它给了我这个异常(exception):

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 中,我实例化了 printooth:

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();
}

关于java - Paper.init 未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57823276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com