1

整个文件就在下面,但是问题代码在这个代码下面。

.xml 文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.mycompany.tenbuttons.DisplaySurveyData">

    <com.github.mikephil.charting.charts.BarChart
        android:id="@+id/chart"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

活动代码:

package com.mycompany.tenbuttons;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;

import java.util.ArrayList;


public class DisplaySurveyData extends ActionBarActivity {

    Intent intent = getIntent();
    private final int nMadValue = intent.getIntExtra(PredictionActivity.NMAD_KEY, 0);
    private final int mMadValue = intent.getIntExtra(PredictionActivity.MMAD_KEY, 0);
    private final int vMadValue = intent.getIntExtra(PredictionActivity.VMAD_KEY, 0);

    BarChart nBarChart = (BarChart) findViewById(R.id.chart);

    ArrayList<BarEntry> valsSurv1 = new ArrayList<BarEntry>(){ //heights
        {
            add(new BarEntry(nMadValue, 0));
            add(new BarEntry(mMadValue, 1));
            add(new BarEntry(vMadValue, 2));
        }
    };

    BarDataSet setSurv1 = new BarDataSet(valsSurv1, "Difficulty level");

    ArrayList<BarDataSet> survData = new ArrayList<BarDataSet>(){ //# of bars
        {
            add(setSurv1);
        }
    };

    ArrayList<String> xVals = new ArrayList<String>(){
        {
            add("Not Mad");
            add("Defenses activated");
            add("Mr. Hyde antidote needed");
        }
    };

    BarData dataSurv = new BarData(xVals, survData);
    nBarChart.setData(dataSurv);
    nBarChart.invalidate();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_survey_data);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_display_survey_data, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}

问题代码:这些符号无法解析setData()invalidate()

nBarChart.setData(dataSurv);
nBarChart.invalidate();

显然,Android Studio 显示导入语句import com.github.mikephil.charting.data.Entry;未被使用,但根据文档,它位于 Entry 类中。所以我在这里不知所措。所有编译器显示的是error: <identifier> expected.

4

1 回答 1

0

看来您还没有了解 Android 编程的基础知识。

您只需将所有内容声明为成员变量,并尝试在您的活动方法范围之外调用图表对象上的方法。

移动提到的 2 行

nBarChart.setData(dataSurv);
nBarChart.invalidate();

另外,移动这条线

   BarChart nBarChart = (BarChart) findViewById(R.id.chart);

onCreate(...)您的活动方法。

于 2015-03-13T15:25:21.663 回答