加载 highchart 时 Android 错误膨胀类

2024-05-13

我正在尝试加载highcharts via Dialog。下面是我的代码

Gradle

implementation '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>

Fragment

 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();

}

当我点击我的项目时list view我想打开一个布局并希望在仪表内显示数据。但我收到错误

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

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

任何帮助将不胜感激。


看起来像是一个组合Dialog包装 Activity 上下文和库的类HIChartView无法处理这个包装的上下文。我强制解开上下文,然后您的代码开始工作。如何:

延长HIChartView使用处理包装上下文的自定义实现:

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 https://stackoverflow.com/a/51640906/12321475,您还可以在那里阅读为什么有时会包装基本上下文。

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

<?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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

加载 highchart 时 Android 错误膨胀类 的相关文章

随机推荐