1

我有以下代码可以使用我的 android 2.3.7 在我的 zebra mz 220 中打印:

public class Printing extends Activity {
ProgressDialog dialog;
ZebraPrinterConnection zebraPrinterConnection;
ZebraPrinter zebra;
String mac;

Handler handlerWorking = new Handler(){
    @Override
    public void handleMessage(Message msg){
        String returnedValue = (String)msg.obj;
        //textV.setText("Returned by thread Circle:" + returnedValue);
    }
};

Handler handlerDone = new Handler(){
    @Override
    public void handleMessage(Message msg){
        String returnedValue = (String)msg.obj;
        //textV.setText(returnedValue);
    }
};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Bundle bundle = this.getIntent().getExtras();
    String mac = bundle.getString("param1");
    this.mac = mac;

    System.out.println(mac);
    showDialog(0);
    //finish();
}


@Override 
public Dialog onCreateDialog(int id){
    final Runnable threadMethod = new Runnable() {
        public void run(){
            try{
                connect();
            }catch(Throwable e){

            }
            Message msg = handlerDone.obtainMessage(1, "DONE!!!");
            handlerDone.sendMessage(msg);
            dialog.dismiss();
        }
    };

    dialog = new ProgressDialog(this);
    dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    dialog.setMessage("Loading...");
    Thread thread = new Thread(null, threadMethod, "Thread1");
    thread.start();
    return dialog;
}

public void connect() {
    Looper.prepare();
    zebraPrinterConnection = null;
    zebraPrinterConnection = new BluetoothPrinterConnection(mac);

    try {
        zebraPrinterConnection.open();
    } catch (ZebraPrinterConnectionException e) {
        disconnect();
    }

    if (zebraPrinterConnection.isConnected()) {
        try {
            zebra = ZebraPrinterFactory.getInstance(zebraPrinterConnection);
            sendLabel();
        } catch (ZebraPrinterConnectionException e) {
            zebra = null;
            disconnect();
        } catch (ZebraPrinterLanguageUnknownException e) {
            zebra = null;
            disconnect();
        }
    }
    Looper.loop();
    Looper.myLooper().quit();
}

public void disconnect() {
    try {
        if (zebraPrinterConnection != null) {
            zebraPrinterConnection.close();
        }
    } catch (ZebraPrinterConnectionException e) {

    } 
}

private byte[] getLabel() {
    PrinterLanguage printerLanguage = zebra.getPrinterControlLanguage();
    byte[] configLabel = null;
    if (printerLanguage == PrinterLanguage.ZPL) {
        configLabel = "^XA^FO17,16^GB379,371,8^FS^FT65,255^A0N,135,134^FDTEST^FS^XZ".getBytes();
    } else if (printerLanguage == PrinterLanguage.CPCL) {
        String cpclConfigLabel = "! 0 200 200 50 1\r\n" + "ON-FEED IGNORE\r\n" + "T 5 0 0 0 Hola\r\n" + "PRINT\r\n";
        configLabel = cpclConfigLabel.getBytes();
    }
    return configLabel;
}

private void sendLabel() {
    try {
        byte[] configLabel = getLabel();
        zebraPrinterConnection.write(configLabel);
    } catch (ZebraPrinterConnectionException e) {

    } finally {
        disconnect();
    }
}

问题是我的 logCat 上出现了下一个错误:

06-12 11:08:03.327: E/AndroidRuntime(14323): FATAL EXCEPTION: main
06-12 11:08:03.327: E/AndroidRuntime(14323): java.lang.VerifyError: com.api.printer.zebra.Printing
06-12 11:08:03.327: E/AndroidRuntime(14323):    at java.lang.Class.newInstanceImpl(Native Method)
06-12 11:08:03.327: E/AndroidRuntime(14323):    at java.lang.Class.newInstance(Class.java:1409)
06-12 11:08:03.327: E/AndroidRuntime(14323):    at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
06-12 11:08:03.327: E/AndroidRuntime(14323):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1672)
06-12 11:08:03.327: E/AndroidRuntime(14323):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
06-12 11:08:03.327: E/AndroidRuntime(14323):    at android.app.ActivityThread.access$1500(ActivityThread.java:123)
06-12 11:08:03.327: E/AndroidRuntime(14323):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
06-12 11:08:03.327: E/AndroidRuntime(14323):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-12 11:08:03.327: E/AndroidRuntime(14323):    at android.os.Looper.loop(Looper.java:130)
06-12 11:08:03.327: E/AndroidRuntime(14323):    at android.app.ActivityThread.main(ActivityThread.java:3835)
06-12 11:08:03.327: E/AndroidRuntime(14323):    at java.lang.reflect.Method.invokeNative(Native Method)
06-12 11:08:03.327: E/AndroidRuntime(14323):    at java.lang.reflect.Method.invoke(Method.java:507)
06-12 11:08:03.327: E/AndroidRuntime(14323):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
06-12 11:08:03.327: E/AndroidRuntime(14323):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
06-12 11:08:03.327: E/AndroidRuntime(14323):    at dalvik.system.NativeStart.main(Native Method)

如果我注释掉 disconnect 方法、sendLabel 方法和 getLabel 方法,应用程序不会崩溃,但会出现下一个错误行:

06-12 11:13:35.207: E/dalvikvm(15869): Could not find class 'com.zebra.android.comm.BluetoothPrinterConnection', referenced from method com.api.printer.zebra.Printing.connect

我的引用库中有斑马库(ZSDK_API.jar),发生了什么?谢谢你的时间。

4

1 回答 1

1

根据您使用的 ADT 版本(如果您确实在使用 Eclipse),如果您使用的是最新版本,则将 ZSDK_API.jar 放在您的 libs/ 目录中就足够了。这应该被 Android 构建过程所接受。然后,jar 应该会自动显示在您的 Android 项目的 Android Dependencies 库中。

检查此 URL 以获取更多信息:http ://android.foxykeep.com/dev/how-to-fix-the-classdefnotfounderror-with-adt-17

于 2012-06-12T16:31:29.917 回答