Java Question how to be,with import . jar file

hears

Active Member
Licensed User
Longtime User
when make library,the project depend on .jar SDK file

in eclipse code import many VOID in this
.jar
i have finish make my library.
in B4A project, i must import this .jar file too?

how to import?
 

hears

Active Member
Licensed User
Longtime User
i have make libary ,but when use this libary have this erro:

B4X:
java.lang.NoClassDefFoundError: com.zj.usbsdk.UsbController

"usbsdk "in another SDK .jar file ,how to import this file in b4a project ?
 

hears

Active Member
Licensed User
Longtime User
i find this code put in eclips project,after copy .jar file to B4A library folder...

import anywheresoftware.b4a.BA.DependsOn;
@DependsOn(values={"usbsdk"})
i think .jar no problem now...
B4X:
package cn.com.zj.usbdemo;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.sql.Date;
import java.text.SimpleDateFormat;

import zj.com.customize.sdk.Other;
import com.zj.command.sdk.Command;
import com.zj.command.sdk.PrintPicture;
import com.zj.command.sdk.PrinterCommand;
import cn.com.zj.usbdemo.R;
import com.zj.usbsdk.UsbController;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.hardware.usb.UsbDevice;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.Toast;
import anywheresoftware.b4a.BA.ShortName;

import anywheresoftware.b4a.BA.DependsOn;



@ShortName("printdemo")
@DependsOn(values={"usbsdk"})

public class PrintDemo extends Activity implements OnClickListener{


i use this code get library again.
when use in b4a ,have this erro.
java.lang.NullPointerException

why ?


erro from this function

B4X:
import com.zj.usbsdk.UsbController;
 static UsbController  usbCtrl = null;

usbCtrl = new UsbController(this,mHandler);



public void connectusb (){
        usbCtrl.close();
        int  i = 0;
        for( i = 0 ; i < 8 ; i++ ){
            dev = usbCtrl.getDev(u_infor[i][0],u_infor[i][1]);
            if(dev != null)
                break;
        }

        if( dev != null ){
            if( !(usbCtrl.isHasPermission(dev))){
               
                usbCtrl.getPermission(dev);
            }else{
               
               
               
               
            }
        }

       
    }



@SuppressLint("HandlerLeak") private final  Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case UsbController.USB_CONNECTED:
                Toast.makeText(getApplicationContext(), getString(R.string.msg_getpermission),
                        Toast.LENGTH_SHORT).show();
                btnSend.setEnabled(true);
                btn_test.setEnabled(true);
                btnClose.setEnabled(true);
                btn_printA.setEnabled(true);
                btn_BMP.setEnabled(true);
                btn_ChoseCommand.setEnabled(true);
                btn_prtcodeButton.setEnabled(true);
                btn_prtsma.setEnabled(true);
                btn_prttableButton.setEnabled(true);
                Simplified.setEnabled(true);
                Korean.setEnabled(true);
                big5.setEnabled(true);
                thai.setEnabled(true);
                btn_conn.setEnabled(false);
                break;
            default:
                break;
            }
        }
    };
 
Last edited:

hears

Active Member
Licensed User
Longtime User
Don't wrap full activities. It will make things more complicated.


Can you post the full error message?


main_button7_click (java line: 995)
java.lang.IllegalStateException: System services not available to Activities before onCreate()
at android.app.Activity.getSystemService(Activity.java:4532)
at com.zj.usbsdk.UsbController.<init>(UsbController.java:40)
at cn.com.zj.usbdemo.PrintDemo.connectusb(PrintDemo.java:1039)
at b4a.example1.main._button7_click(main.java:995)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:4443)
at android.view.View$PerformClick.run(View.java:18443)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5009)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:811)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:627)
at dalvik.system.NativeStart.main(Native Method)

B4X:
BA myba;
        
         public void initialize(BA ba) // ba is a hidden parameter
            {
              myba = ba; //save it if you need to
              Activity a = myba.activity; // get the Activity
           h = new Handler(); // get a handler for the current thread 
            
            }
        
         public void connectusb (){

                //        onCreate(null, null);
//                usbCtrl.close();
                
                usbCtrl = new UsbController(this,h);
                u_infor = new int[8][2];
                u_infor[0][0] = 0x1CBE;
                u_infor[0][1] = 0x0003;
                u_infor[1][0] = 0x1CB0;
                u_infor[1][1] = 0x0003;
                u_infor[2][0] = 0x0483;
                u_infor[2][1] = 0x5740;
                u_infor[3][0] = 0x0493;
                u_infor[3][1] = 0x8760;
                u_infor[4][0] = 0x0416;
                u_infor[4][1] = 0x5011;
                u_infor[5][0] = 0x0416;
                u_infor[5][1] = 0xAABB;
                u_infor[6][0] = 0x1659;
                u_infor[6][1] = 0x8965;
                u_infor[7][0] = 0x0483;
                u_infor[7][1] = 0x5741;
//                int  i = 0;
//                for( i = 0 ; i < 8 ; i++ ){
//                    dev = usbCtrl.getDev(u_infor[i][0],u_infor[i][1]);
//                    if(dev != null)
//                        break;
//                }
        //
//                if( dev != null ){
//                    if( !(usbCtrl.isHasPermission(dev))){
//                       
//                        usbCtrl.getPermission(dev);
//                    }else{
//                       
//                       
//                       
                        
            //        }
                //}

                
            }
 

hears

Active Member
Licensed User
Longtime User
erro from here
usbCtrl = new UsbController(this,h);
 

hears

Active Member
Licensed User
Longtime User
The problem is that you wrapped the code as an activity. The only way to create activities is by starting them with StartActivity.

It will be simple to wrap a regular class instead.

it is work now .
usbCtrl = new UsbController(this,h);

i just add these code
B4X:
BA myba;
public void initialize(BA ba) // ba is a hidden parameter
         {
           myba = ba; //save it if you need to
           Activity a = myba.activity; // get the Activity
        h = new Handler(); // get a handler for the current thread
     
         }


and the erro code,i change to usbCtrl = new UsbController(myba.activity,h);


i understand now : in JAVA CODE "this" need change to BA.activity . "handler "need get from this initialize(BA ba) function
 
Last edited:
Top