蓝牙处理程序中的微调器

时间:2018-04-13 21:25:13

标签: java android bluetooth android-bluetooth

我在蓝牙处理程序中使用微调器时遇到问题。 TextViews和CheckBoxes完美地工作,但是spinner执行 异常:java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a null object reference

处理程序代码:

 /**
 * The Handler that gets information back from the BluetoothChatService
 */
private final Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        FragmentActivity activity = getActivity();
        switch (msg.what) {
            case Constants.MESSAGE_STATE_CHANGE:
                switch (msg.arg1) {
                    case BluetoothChatService.STATE_CONNECTED:
                        setStatus(getString(R.string.title_connected_to, mConnectedDeviceName));
                        mConversationArrayAdapter.clear();
                        break;
                    case BluetoothChatService.STATE_CONNECTING:
                        setStatus(R.string.title_connecting);
                        break;
                    case BluetoothChatService.STATE_LISTEN:
                    case BluetoothChatService.STATE_NONE:
                        setStatus(R.string.title_not_connected);
                        break;
                }
                break;
            case Constants.MESSAGE_WRITE:
                byte[] writeBuf = (byte[]) msg.obj;
                // construct a string from the buffer
                String writeMessage = new String(writeBuf);
                mConversationArrayAdapter.add("Me:  " + writeMessage);
                break;
            case Constants.MESSAGE_READ:
                byte[] readBuf = (byte[]) msg.obj;
                strIncom = new String(readBuf, 0, msg.arg1);
                sb.append(strIncom);                                                // append string

                int endOfLineIndex = sb.indexOf("\n");                            // determine the end-of-line
                if (endOfLineIndex > 0)
                {                                            // if end-of-line,
                    String sbprint = sb.substring(0, endOfLineIndex);               // extract string
                    sb.delete(0, sb.length());// and clear

                    // construct a string from the valid bytes in the buffer

                    mConversationArrayAdapter.add(mConnectedDeviceName + ":  " + sbprint);

                    android.util.Log.d(TAG, "handleMessage: ReadMessage Odczyt2: " + sbprint);
                    data = sbprint.split(",");
                    try {
                        android.util.Log.d(TAG, "handleMessage: Data 0: " + data[0]);
                        android.util.Log.d(TAG, "handleMessage: Data 1: " + data[1]);
                        android.util.Log.d(TAG, "handleMessage: Data 2: " + data[2]);
                       if(sbprint.endsWith("k") && sbprint.startsWith("s"))
                        {
                          //  if(data[1] == "c") {
                                    char[] czujniki = data[2].toCharArray();
                                    android.util.Log.d(TAG, "handleMessage: Data cz1: " + czujniki[1]);
                                   cbState(activity.findViewById(R.id.rbCLLP), czujniki[0]);
                                   cbState(activity.findViewById(R.id.rbCLPP), czujniki[1]);

                                    cbState(activity.findViewById(R.id.rbCPLB), czujniki[2]);
                                    cbState(activity.findViewById(R.id.rbCPLP), czujniki[3]);
                                    cbState(activity.findViewById(R.id.rbCPSR), czujniki[4]);
                                    cbState(activity.findViewById(R.id.rbCPPP), czujniki[5]);
                                    cbState(activity.findViewById(R.id.rbCPPB), czujniki[6]);

                            List<String> list = new ArrayList<>();
                            list.add("Program 1");
                            list.add("Program 2");

                            ArrayAdapter<String> my_adapter = new ArrayAdapter<String>(getContext(), R.layout.support_simple_spinner_dropdown_item, list);
                            my_adapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
                            nr_prog.setAdapter(my_adapter);
                                    nr_prog.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                                        @Override
                                        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                                        }

                                        @Override
                                        public void onNothingSelected(AdapterView<?> parent) {
                                        }
                                    });

                                TextView tvPWMlewy = activity.findViewById(R.id.pwm_lewy);
                                TextView tvPWMprawy = activity.findViewById(R.id.pwm_prawy);
                                android.util.Log.d(TAG, "handleMessage: PWM Lewy: " + data[4]);
                                tvPWMlewy.setText("PWM Lewy: " + data[4]);
                                tvPWMprawy.setText("PWM Prawy: " + data[6]);

                        }
                    } catch (Exception e) {
                        android.util.Log.d(TAG, "handleMessage: " + e);
                    }
                }
                break;
            case Constants.MESSAGE_DEVICE_NAME:
                // save the connected device's name
                mConnectedDeviceName = msg.getData().getString(Constants.DEVICE_NAME);
                if (null != activity) {
                    Toast.makeText(activity, "Connected to "
                            + mConnectedDeviceName, Toast.LENGTH_SHORT).show();
                }
                break;
            case Constants.MESSAGE_TOAST:
                if (null != activity) {
                    Toast.makeText(activity, msg.getData().getString(Constants.TOAST),
                            Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
};

OnViewCreated:

    @Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    mConversationView = view.findViewById(R.id.in);
    mOutEditText = view.findViewById(R.id.edit_text_out);
    mSendButton = view.findViewById(R.id.button_send);

    nr_prog = view.findViewById(R.id.spinner);
}

当选择微调器列表中的某些内容时,我想发送所选数据。如果没有选择接收数据到微调器列表。

感谢您的每一次鼓励。

0 个答案:

没有答案