2017-08-10 25 views
1

我想在Android上使用输入输出流,使用构造函数与USB通信,如果我单独使用处理程序,它没有任何问题通信,但如果我使用普通构造器崩溃的应用程序说空指针异常希望我做一些错误的错误,但不知道在哪里我做了错误使用构造函数来调用一个处理程序从另一个类的活动

,代码如下

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

和通信类

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

和USB附件管理器类实现方法

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

和错误显示了活动的一部分,在usb_Communcation类

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

初始化'usbCom = new Usb_Communciation(this,getIntent());'in'onCreate()'方法。在声明部分只声明变量'Usb_Communciation usbCom;' – Piyush

+0

仍然是崩溃 –

回答

1

相同构造试试这个:

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

现在不会崩溃,但不通信它需要大量的时间来加载应用程序。 –

相关问题