2017-08-10 7 views
1

J'essaie de communiquer avec USB sur android en utilisant un flux d'entrée-sortie, en utilisant le constructeur si j'utilise le gestionnaire individuellement, il communique sans problème, mais si j'utilise un commun constructeur, il se bloque l'application en disant espoir null pointer exception que je fais une erreur de gaffe mais je ne sais pas où je l'ai fait cette erreurUtilisation de constructeur pour appeler un gestionnaire d'une activité d'une autre classe

et le code comme suit

public class BasicAccessoryDemo extends Activity implements View.OnClickListener { 
    Usb_Communciation usbCom = new Usb_Communciation(this, getIntent()); 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     Button mycontrol, close_command; 
     mycontrol = (Button) findViewById(R.id.send_command); 
     mycontrol.setOnClickListener(this); 
     close_command = (Button) findViewById(R.id.close_command); 
     close_command.setOnClickListener(this); 
    } 

    @Override 
    public void onStart() { 
     super.onStart();  
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 
    } 

    public void onClick(View view) { 
     switch (view.getId()) { 
      case R.id.send_command: 
       byte[] commandPacket = new byte[2]; 
       commandPacket[0] =0x12; 
       commandPacket[1] =0x34;  
       usbCom.Send_message(commandPacket); 
       break; 
      case R.id.close_command: 
       byte[] commandPackets = new byte[2]; 
       commandPackets[0] = 0; 
       commandPackets[1] = 0; 
       usbCom.Send_message(commandPackets); 
       break; 
     } 
    }  
} 

et classe de communication

public class Usb_Communciation { 
    public final static int USBAccessoryWhat = 0; 
    public int firmwareProtocol = 0; 
    public static USBAccessoryManager accessoryManager; 
    public static String TAG = "MICROCHIP"; 
    public static final int APP_CONNECT = (int) 0xAE; 
    public boolean deviceAttached = false; 

    public Usb_Communciation(Context mContext, Intent intent) { 
     accessoryManager = new USBAccessoryManager(handler, USBAccessoryWhat); 
     accessoryManager.enable(mContext, intent); 
    } 


    public void Send_message(byte[] data) { 
     try { 
      accessoryManager.write(data); 
     } catch (Exception e) { 
      Log.d(TAG, 
        "USBAccessoryManager:write():IOException: arasu " 
          + e.toString()); 
      e.printStackTrace(); 
     } 
    } 

    public Handler handler = new Handler() { 
     @Override 
     public void handleMessage(Message msg) { 
      byte[] commandPacket = new byte[64]; 
      byte[] WriteValue = new byte[2]; 

      switch (msg.what) { 

        //Something inside 

      } //switch 
     } //handleMessage 
    }; //handler 

    public int getFirmwareProtocol(String version) { 
     String major = "0"; 
     int positionOfDot; 
     positionOfDot = version.indexOf('.'); 
     if (positionOfDot != -1) { 
      major = version.substring(0, positionOfDot); 
     } 
     return new Integer(major).intValue(); 
    } 
} 

et la méthode activer le gestionnaire accessoire USB classe

public RETURN_CODES enable(Context context, Intent intent) { 
     //something inside 
    } 

et l'erreur montre de la part de l'activité et le même constructeur sur la classe usb_Communcation

Usb_Communciation usbCom = new Usb_Communciation(this, getIntent()); 
+0

Initialise 'usbCom = nouvelle Usb_Communciation (this, getIntent());' dans la méthode onCreate() '. Dans la partie de la déclaration déclarent seulement la variable Usb_Communciation usbCom; ' – Piyush

+0

encore il plantait –

Répondre

1

Essayez ceci:

Usb_Communciation usbCom; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    usbCom = new Usb_Communciation(this, getIntent()); 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    Button mycontrol, close_command; 
    mycontrol = (Button) findViewById(R.id.send_command); 
    mycontrol.setOnClickListener(this); 
    close_command = (Button) findViewById(R.id.close_command); 
    close_command.setOnClickListener(this); 
} 
+0

ne plante pas maintenant, mais ne communique pas il prend beaucoup de temps pour le chargement de l'application. –