簡體   English   中英

Android 充氣錯誤 class<unknown> 加載高圖時</unknown>

[英]Android error inflating class <unknown> while loading highchart

我正在嘗試通過Dialog加載highcharts 下面是我的代碼

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>

分段

 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: 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 我堅持下去。 怎樣才能擺脫這個問題?

任何幫助將不勝感激。

似乎是包裝 Activity 上下文的Dialog class 和庫的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;
    }
}

展開的 function 是從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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM