继续在方法中获得几个例外 - android studio

时间:2017-05-15 23:05:49

标签: android nullpointerexception textview indexoutofboundsexception findviewbyid

我一直在以下方法中获得几个例外(这是片段的一部分),我找不到解决方案。

该代码的意图是我通过蓝牙接收String,我想从中“提取”特定数据(温度测量),将接收的字节转换为int并将其显示在UI中的TextView元素上。 / p>

public void showCurrentTemperature(int id){

    byte[] receivedTempArray = new byte [18];
    receivedTempArray = MainActivity.received.getBytes();
    byte[] TempData = new byte [6];

    if (receivedTempArray[0] == 83 & receivedTempArray[1] == 84 & 
        receivedTempArray[2] == 71 & receivedTempArray[3] == 105 &
        receivedTempArray[6] == 116){

        TempData[0] = receivedTempArray[8];
        TempData[1] = receivedTempArray[9];
        TempData[2] = receivedTempArray[11];
        TempData[3] = receivedTempArray[12];

        String TempValueHex = new String(TempData);

        int TempInt = Integer.parseInt(TempValueHex, 16);
        TextView view = (TextView) getView().findViewById(id);

        view.setText(TempInt + "");
    }
}

第一个是nullpointerexception:

receivedTempArray = MainActivity.received.getBytes();

第二个是ArrayIndexOutOfBoundsException:length = 6;指数= 6。

打开整个片段后会抛出此异常,但是此方法中只有数组。

第三个例外是此行中的另一个nullpointerexception:     TextView view =(TextView)getView()。findViewById(id);

我猜这是造成“findViewByID”的原因。但我已经在onViewCreated中声明了该方法。那么为什么我一直得到一个nullpointerexception?

@Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    showCurrentTemperature(R.id.TemperatureTextView);
}

编辑: mainActivity中的“received”字符串在此处创建:

public static String received;
void receivedData(String data) {
    //Log.d(TAG, getString(R.string.connection_RX_data) + data);
    received = data;
    Fragment f = this.getSupportFragmentManager().findFragmentById(R.id.fragment_container);
    if (f == mRgbwFragment) {
        mRgbwFragment.receivedData(data);
    }
    if (f == mServiceFragment) {
        mServiceFragment.receivedData(data);
    }
}

并在此处创建数据字符串:

private void listenForData() {
    final Handler rxMessageHandler = new Handler();
    // Handler needed to access outer class from inner Thread
    mStopListenForData = false;             // stop inner Thread flag
    mDataInBuffer = new byte[256];          // temporary buffer
    mDataInBufferPosition = 0;              // position of last byte in the buffer
    Thread dataInThread = new Thread(       // Thread listens for incoming data
            new Runnable() {                // Uses inner classes
                public void run() {
                    while (!Thread.currentThread().isInterrupted() && !mStopListenForData) {
                        try {
                            int bytesAvailable = mInStream.available();
                            if (bytesAvailable > 0) {
                                byte[] packetBytes = new byte[bytesAvailable];
                                bytesAvailable = mInStream.read(packetBytes);
                                Log.d(TAG, bytesAvailable + "are available Bytes");
                                   final String data = new String(packetBytes); //create a sting from the received byte array
                                rxMessageHandler.post(
                                        new Runnable() {    // Process received String
                                            public void run() {
                                                Log.d(TAG, mActivity.getString(R.string.connection_RX_data) + data);
                                                mActivity.receivedData(data);
                                            }
                                        }
                                );
                            }
                        } catch (Exception e) {
                            Log.e(TAG, mActivity.getString(R.string.connection_error) + " listenForData runnable");
                            mStopListenForData = true;
                        }
                        synchronized (this) {
                            try {           // Wait a while before listening again
                                wait(mActivity.getResources().getInteger(R.integer.connection_wait_TXRX));
                            } catch (Exception e) {
                                Log.e(TAG, mActivity.getString(R.string.connection_error) + " while waiting");
                            }
                        }

                    }
                }
            }
    );
    dataInThread.start();
}

1 个答案:

答案 0 :(得分:0)

(对于你的最后一期)我猜是getView()是你的问题。在创建视图层次结构之后但在将其附加到父级之前调用onViewCreated。

  

onViewCreated - 在onCreateView(LayoutInflater,ViewGroup,Bundle)返回之后立即调用,但在将任何已保存的状态恢复到视图之前。这使得子类有机会在知道自己的视图层次结构已完全创建后进行初始化。但是,片段的视图层次结构此时不会附加到其父级。

我假设getView()是你创建的一个函数 - 但是如果它类似于:

getView (int position, View convertView, ViewGroup parent)

然后您尝试通过父级访问TextView。但是,在调用onViewCreated时,您的视图层次结构尚未附加到其父级。

解决方案?使用不涉及findViewByID父级的视图引用(也许在onCreateView中根据inflater的返回值创建一个类变量)