有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

蓝牙处理器中的java微调器

我在蓝牙处理程序中使用微调器时遇到问题。文本视图和复选框工作正常,但spinner会执行 例外情况:java.lang.NullPointerException: Attempt to invoke virtual method 'void 安卓.widget.Spinner.setAdapter(安卓.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);

                    安卓.util.Log.d(TAG, "handleMessage: ReadMessage Odczyt2: " + sbprint);
                    data = sbprint.split(",");
                    try {
                        安卓.util.Log.d(TAG, "handleMessage: Data 0: " + data[0]);
                        安卓.util.Log.d(TAG, "handleMessage: Data 1: " + data[1]);
                        安卓.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();
                                    安卓.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);
                                安卓.util.Log.d(TAG, "handleMessage: PWM Lewy: " + data[4]);
                                tvPWMlewy.setText("PWM Lewy: " + data[4]);
                                tvPWMprawy.setText("PWM Prawy: " + data[6]);

                        }
                    } catch (Exception e) {
                        安卓.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) 个答案