首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么AnyChart返回空对象引用异常?

为什么AnyChart返回空对象引用异常?
EN

Stack Overflow用户
提问于 2019-11-25 04:22:27
回答 3查看 1.1K关注 0票数 0

我已经尝试了几个教程来使用AnyChart和MPAndroidChart创建一个简单的饼图:

但是,当我运行上述所有示例时,都会得到一个异常:

代码语言:javascript
复制
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.anychart.AnyChartView$JsListener com.anychart.AnyChartView.getJsListener()' on a null object reference
        at com.anychart.APIlib.addJSLine(APIlib.java:27)
        at com.anychart.charts.Pie.<init>(Pie.java:34)
        at com.anychart.AnyChart.pie(AnyChart.java:130)
        at com.mpereira.savingstrackerapp.Activities.TestActivity.setupPieChart(TestActivity.java:37)
        at com.mpereira.savingstrackerapp.Activities.TestActivity.onCreate(TestActivity.java:33)

我相应地添加了依赖项,下面是xml代码:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    tools:context=".Activities.TestActivity">

    <com.anychart.AnyChartView
        android:id="@+id/any_chart_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</RelativeLayout>

下面是活动的代码:(来自https://www.youtube.com/watch?v=qWBA2ikLJjU)

代码语言:javascript
复制
public class TestActivity extends AppCompatActivity {

    private static String TAG = "MainActivity";
    AnyChartView anyChartView;
    String[] months = {"Jan", "Feb", "Mar"};
    int[] earnings = {500, 800, 350};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "onCreate: starting to create chart");
        anyChartView = (AnyChartView) findViewById(R.id.any_chart_view);

        setupPieChart();
    }

    void setupPieChart(){
        Pie pie = AnyChart.pie();
        List<DataEntry> dataEntries = new ArrayList<>();
        for (int i=0;i<months.length; ++i){
            dataEntries.add(new ValueDataEntry(months[i], earnings[i]));
        }
        pie.data(dataEntries);
        anyChartView.setChart(pie);
    }
}

这是截图:异常

我知道创建对象通常需要关键字new,但是根据教程https://github.com/AnyChart/AnyChart-Android/wiki/Getting-started,我们在Pie pie = AnyChart.pie();中不需要关键字

感谢任何关于如何修复它的提示。

EN

回答 3

Stack Overflow用户

发布于 2019-11-25 04:48:40

根据https://www.anychart.com/technical-integrations/samples/android-charts/教程,您的布局代码应该如下所示

代码语言:javascript
复制
  <com.anychart.anychart.AnyChartView
    android:id="@+id/any_chart_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
/>

你也这么说过

代码语言:javascript
复制
<com.anychart.AnyChartView
    android:id="@+id/any_chart_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

这是不正确的,这就是为什么要获得空指针异常。

票数 2
EN

Stack Overflow用户

发布于 2019-11-26 03:05:07

检查您的XML (您使用的是相对布局)、在主活动中导入、正在使用的API级别,并确保根据Gettings开始指南正确添加了Gettings开始库。您的主要活动代码是好的,我已经成功地建立了基于您的代码的图表。

结果如下:

xml文件内容如下:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.anychart.AnyChartView
        android:id="@+id/any_chart_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

将您的xml与上面提供的xml进行比较。

票数 0
EN

Stack Overflow用户

发布于 2020-08-17 20:43:05

根据这个https://www.youtube.com/watch?v=qWBA2ikLJjU)

不需要添加强制转换AnyChart视图

代码语言:javascript
复制
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "onCreate: starting to create chart");
        anyChartView = findViewById(R.id.any_chart_view);

        setupPieChart();
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59025210

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档