Android错误膨胀类< unknown>加载海图时 [英] Android error inflating class <unknown> while loading highchart

查看:80
本文介绍了Android错误膨胀类< unknown>加载海图时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 Dialog 加载 highcharts .下面是我的代码

I am trying to load highcharts via Dialog. Below is my code

成绩

实现'com.highsoft.highcharts:highcharts:9.0.1'

XML

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:scrollbars="none">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:orientation="vertical"
    android:padding="10dp">

    <com.highsoft.highcharts.core.HIChartView
        android:id="@+id/goly_gauge"
        android:layout_width="match_parent"
        android:layout_height = "350dp"

        />

    <com.highsoft.highcharts.core.HIChartView
        android:id="@+id/golm_gauge"
        android:layout_width="match_parent"
        android:layout_height="350dp"
        />

      <Button
        android:id="@+id/ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="OK" />
</LinearLayout>

</ScrollView>

片段

 suggestion_list.setOnItemClickListener((parent, view, position, id) -> {
        String selectedFromList = (String) (suggestion_list.getItemAtPosition(position));
        showPreFilledData(selectedFromList);
        Log.e(TAG, arraylist.get(position));
    });

private void showPreFilledData(String string) {

    final Dialog dialog = new Dialog(getActivity());
    dialog.setContentView(R.layout.product_sales_layout);// error comes at this point
    dialog.setTitle("Product Sales");
    Button ok;
    ok = (Button) dialog.findViewById(R.id.ok);

    switch (string) {
        case "A2A 100 MG TABS":
            GolYgauge(55.1);
            GolMgauge(32.9);
            break;
        case "AQUA-VIT SACHET":
            GolYgauge(45.8);
            GolMgauge(22.7);
            break;
        case "BRONCOPHYLINE SYRUP":
            GolYgauge(65.7);
            GolMgauge(42.4);
            break;
    }

    ok.setOnClickListener(v -> dialog.dismiss());
    Window window = dialog.getWindow();
    window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT);
    dialog.show();

}

当我在列表视图中单击一个项目时,我想打开一个布局并想在量规内显示数据.但是我出错了

When I click on an item in my list view I want to open a layout and want to display data inside a gauge. But I am getting error

android.view.InflateException:com.thumbsol.accuratemobileassetsmanagament:layout/product_sales_layout中的二进制XML文件第15行:com.thumbsol.accuratemobileassetsmanagament:layout/product_sales_layout中的二进制XML文件第15行:膨胀类

android.view.InflateException: Binary XML file line #15 in com.thumbsol.accuratemobileassetsmanagament:layout/product_sales_layout: Binary XML file line #15 in com.thumbsol.accuratemobileassetsmanagament:layout/product_sales_layout: Error inflating class

第15行是< com.highsoft.highcharts.core.HIChartView .我坚持下去.如何摆脱这个问题?

The line number 15 is <com.highsoft.highcharts.core.HIChartView. I am stuck to it. How can get rid from this issue?

任何帮助将不胜感激.

推荐答案

似乎是包装活动上下文的 Dialog 类和库的 HIChartView 无法组合的组合这种包装的上下文.我强行解开上下文,然后您的代码开始工作.如何:

Seems like a combination of Dialog class wrapping the Activity context and the library's HIChartView not able to process this wrapped context. I force-unwrapped the context and then your code started working. How to:

使用处理包裹上下文的自定义实现扩展 HIChartView :

Extend the HIChartView with a custom implementation handling the wrapped context:

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.util.AttributeSet;

import com.highsoft.highcharts.core.HIChartView;

public class CustomHIChartView extends HIChartView {

    public CustomHIChartView(Context context) {
        super(unwrap(context));
    }

    public CustomHIChartView(Context context, AttributeSet attributeSet) {
        super(unwrap(context), attributeSet);
    }

    // unwrap the context until I get the original passed-in Activity context
    private static Activity unwrap(Context context) {
        while (!(context instanceof Activity) && context instanceof ContextWrapper) {
            context = ((ContextWrapper) context).getBaseContext();
        }
        return (Activity) context;
    }
}

展开功能是从 https://stackoverflow.com/a/51640906/12321475 复制而来的,您可以在还阅读了为什么有时会包装基本上下文.

The unwrapping function is copied from https://stackoverflow.com/a/51640906/12321475, there you can also read on why the base context are sometimes being wrapped.

修改布局以使用自定义实现:

Modify your layout to use the custom implementation:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    android:scrollbars="none">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="10dp">

        <your.package.name.CustomHIChartView
            android:id="@+id/goly_gauge"
            android:layout_width="match_parent"
            android:layout_height = "350dp"

            />

        <your.package.name.CustomHIChartView
            android:id="@+id/golm_gauge"
            android:layout_width="match_parent"
            android:layout_height="350dp"
            />

        <Button
            android:id="@+id/ok"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="OK" />
    </LinearLayout>

</ScrollView>

这篇关于Android错误膨胀类&lt; unknown&gt;加载海图时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆