MPAndroidchart mChart 在 onCreate 和 onResume 之间设置为 null

MPAndroidchart mChart set to null between onCreate and onResume

我正在尝试使用 MPAndroidChart 绘制实时数据图表,并遵循代码示例 here and here。过去我有静态数据图表。

按照第一个示例,我在 onCreate 方法中初始化全局声明的 mChart 变量。但是,当调用 onResume 时,mChart 变量再次设置为 NULL,我的 addEntry() 方法由于 NULL 异常而失败。

我需要做什么来防止 mChart 被设置为 NULL(XML 中的其他变量在 onCreate 中初始化并在 onResume 中保留它们的值)。

顺便说一句,此应用还有一个 BLE 服务 运行 来自 AndroidStudio BLE 示例的源代码。

这就是 mChart 在 onCreate 结束时的样子(包括 mDataField 以供比较)。

mChart = {LineChart@830034242904} "com.github.mikephil.charting.charts.LineChart{41e56d58 V.ED.... ......ID 0,0-0,0 #7f0b0066 app:id/chart}"
mDataField = {TextView@830035362136} "android.widget.TextView{41f68158 V.ED.... ......ID 0,0-0,0 #7f0b0068 app:id/data_value}"

mChart 声明为 class 私有变量:

private LineChart mChart;

我的 onCreate() 方法(根据 AndroidStudio 调试器,mChart 在该方​​法末尾有一个非 NULL 值):

   @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gatt_services_characteristics);
        final Intent intent = getIntent();
        mDeviceName = intent.getStringExtra(EXTRAS_DEVICE_NAME);
        mDeviceAddress = intent.getStringExtra(EXTRAS_DEVICE_ADDRESS);
        // Sets up UI references.
        ((TextView) findViewById(R.id.device_address)).setText(mDeviceAddress);
        mGattServicesList = (ExpandableListView) findViewById(R.id.gatt_services_list);
        mGattServicesList.setOnChildClickListener(servicesListClickListner);
        mConnectionState = (TextView) findViewById(R.id.connection_state);
        mDataField = (TextView) findViewById(R.id.data_value);
        getActionBar().setTitle(mDeviceName);
        getActionBar().setDisplayHomeAsUpEnabled(true);
        Intent gattServiceIntent = new Intent(this, BluetoothLeService.class);
        bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);

        // https://github.com/PhilJay/MPAndroidChart/wiki/Getting-Started
        // in this example, a LineChart is initialized from xml
        LineChart mChart = (LineChart) findViewById(R.id.chart);
        // mChart.setOnChartValueSelectedListener(this);
        // ---------------------------------------------------------
        // Set up chart formatting
        // ---------------------------------------------------------
        mChart.setDescription(new Description());
        mChart.setNoDataText("No Data Yet");
        // enable value highlighting
        mChart.setHighlightPerDragEnabled(true);
        mChart.setHighlightPerTapEnabled(true);
        mChart.setTouchEnabled(true);
        // enable scaling and dragging
        mChart.setDragEnabled(true);
        mChart.setScaleEnabled(true);
        mChart.setDrawGridBackground(false);
        // enable pinch zoom to avoid scaling x and y axis separately
        mChart.setPinchZoom(true);
        // alternative background color
        mChart.setBackgroundColor(Color.LTGRAY);

        LineData data = new LineData();
        data.setValueTextColor(Color.WHITE);

        // add data to line chart
        mChart.setData(data);

        // get legend object
        Legend l = mChart.getLegend();

        // customize legend
        l.setForm(Legend.LegendForm.LINE);
        l.setTextColor(Color.WHITE);

        XAxis x1 = mChart.getXAxis();
        x1.setTextColor(Color.WHITE);
        x1.setDrawGridLines(false);
        x1.setAvoidFirstLastClipping(true);

        YAxis y1 = mChart.getAxisLeft();
        y1.setTextColor(Color.WHITE);
        y1.setAxisMaxValue(120f);
        y1.setDrawGridLines(true);

        YAxis y12 = mChart.getAxisRight();
        y12.setEnabled(false);

        final Button emailButton = (Button) findViewById(R.id.button_email);
        emailButton.setOnClickListener(new View.OnClickListener(){
            public void onClick(View view){
                showEmailDialog();
            }
        } );
    }

还有,我的 onResume。根据调试器,mChart 在调用 super.onResume() 之前等于 NULL。

@Override
protected void onResume() {
    super.onResume();
    registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());
    if (mBluetoothLeService != null) {
        final boolean result = mBluetoothLeService.connect(mDeviceAddress);
        Log.d(TAG, "Connect request result=" + result);
    }
    // Simulate real time data
    new Thread( new Runnable(){
        @Override
        public void run(){
                // add 100 entries
                for(int i = 0; i<100; i++){
                    runOnUiThread(new Runnable(){
                        @Override
                        public void run() {
                            addEntry(); // Chart is notified of update in addEntry method
                        }
                    });
                    // pause between adds
                    try {
                        Thread.sleep(600);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
    }).start();

}

最后,这是我的 XML:

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2013 The Android Open Source Project
     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at
          http://www.apache.org/licenses/LICENSE-2.0
     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp">
    <LinearLayout android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp">
        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/label_device_address"
            android:textSize="18sp"/>
        <Space android:layout_width="5dp"
            android:layout_height="wrap_content"/>
        <TextView android:id="@+id/device_address"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="18sp"/>
    </LinearLayout>
    <com.github.mikephil.charting.charts.LineChart
        android:id="@+id/chart"
        android:layout_width="match_parent"
        android:layout_height="200sp" />
    <LinearLayout android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp">
        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/label_state"
            android:textSize="12sp"/>
        <Space android:layout_width="5dp"
            android:layout_height="wrap_content"/>
        <TextView android:id="@+id/connection_state"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/disconnected"
            android:textSize="12sp"/>
    </LinearLayout>
    <LinearLayout android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp">
        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/label_data"
            android:textSize="12sp"/>
        <Space android:layout_width="5dp"
            android:layout_height="wrap_content"/>
        <TextView android:id="@+id/data_value"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/no_data"
            android:textSize="12sp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:gravity="bottom|right"
        android:layout_centerInParent="true"
        android:weightSum="5"
        android:layout_alignParentBottom="true">
        <Button
            android:id="@+id/button_email"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:textAllCaps="true"
            android:maxLines="1"
            android:textSize="10sp"
            android:text="@string/button_email_name"/>
    </LinearLayout>
    <ExpandableListView android:id="@+id/gatt_services_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

您将其定义为 onCreate

中的局部变量
LineChart mChart = (LineChart) findViewById(R.id.chart);

将该行更改为

mChart = (LineChart) findViewById(R.id.chart);

因为mChart被定义为字段