在膨胀的视图上投射自定义视图

时间:2017-01-11 11:47:06

标签: android view casting

我有这样的布局

pie_chart.xml

<?xml version="1.0" encoding="utf-8"?>
<com.github.mikephil.charting.charts.PieChart
    android:layout_width="match_parent"
    android:id="@+id/piechart"
    android:layout_height="200dp"
    android:background="#F00"
    xmlns:android="http://schemas.android.com/apk/res/android" />

我试图将PieChart类型转换为View I inflated。我正在使用MPAndroidchart绘制图表。 PieChart的父类扩展了VieGroup,所以我假设Cast上的Casting PieChart类型应该是正确的,但是这给了我一个错误。

导致问题的代码:

   @Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_overview, container, false);

    View pieView =  View.inflate(AppContext.context, R.layout.pie_chart,(ViewGroup)view);
    PieChart pieChart= (PieChart) pieView;

    return view;
}

logcat的:

java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to com.github.mikephil.charting.charts.PieChart
                                                                   at com.teltonika.rms.fragment.OverviewFragment.onCreateView(OverviewFragment.java:31)
                                                                   at android.support.v4.app.Fragment.performCreateView(Fragment.java:2184)
                                                                   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1298)
                                                                   at android.support.v4.app.FragmentManagerImpl.moveFragmentsToInvisible(FragmentManager.java:2323)
                                                                   at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2136)
                                                                   at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2092)
                                                                   at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1998)
                                                                   at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:709)
                                                                   at android.os.Handler.handleCallback(Handler.java:739)
                                                                   at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                   at android.os.Looper.loop(Looper.java:158)
                                                                   at android.app.ActivityThread.main(ActivityThread.java:7229)
                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

3 个答案:

答案 0 :(得分:6)

你要

  

java.lang.ClassCastException:android.widget.LinearLayout不能   施加到com.github.mikephil.charting.charts.PieChart

当您尝试将一种数据类型的Object转换为另一种数据类型时,Java会抛出 ClassCastException

问题从

开始
 PieChart pieChart= (PieChart) pieView; // Don't do this

试试这种方式

View pieView =  View.inflate(AppContext.context, R.layout.pie_chart,(ViewGroup)view);
PieChart pieChart= (PieChart) pieView.findViewById(R.id.piechart); 

答案 1 :(得分:4)

经过一些研究后我发现,如果我为View.inflate()方法提供root View,它将返回带有我的膨胀视图的新根视图,但如果我为View.inflate()方法添加null将仅返回我的膨胀视图。

E.G

这将返回Linearlayout,因为view根视图是LinearLayout

LinearLayout pieView =  View.inflate(AppContext.context, R.layout.pie_chart,(ViewGroup)view);

但是如果我添加null,它会膨胀View I&#39; m。对于这种情况,它的饼图

PieChart pieView =  View.inflate(AppContext.context, R.layout.pie_chart,null);

答案 2 :(得分:0)

将您的代码更新为:

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_overview, container, false);

    View pieView =  View.inflate(AppContext.context, R.layout.pie_chart,(ViewGroup)view);
    PieChart pieChart= (PieChart) pieView.findViewById(R.id.piechart);

    return view;
}

您只是将片段视图转换为PieChart,因为您收到此错误。