【问题标题】:Using constructor to call a handler to a activity from another class使用构造函数将处理程序调用到另一个类的活动
【发布时间】:2017-08-10 05:44:54
【问题描述】:

我正在尝试使用输入输出流在 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());

【问题讨论】:

  • onCreate() 方法中初始化usbCom = new Usb_Communciation(this, getIntent());。在声明部分只声明变量Usb_Communciation usbCom;
  • 还是崩溃了

标签: java android constructor inputstream outputstream


【解决方案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);
}

【讨论】:

  • 现在没有崩溃,但没有通信它需要大量时间来加载应用程序。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-12
  • 1970-01-01
  • 2012-06-22
  • 1970-01-01
  • 2014-08-01
  • 2011-03-24
  • 1970-01-01
相关资源
最近更新 更多