在一个碎片中使用MPChartLibrary

5
我正在开发一个Android项目,尝试实现来自https://github.com/PhilJay/MPAndroidChart的MPAndroidChart库。我想在片段中实现它,但一直出错,我不知道为什么。
以下是我的活动。
public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        Fragment chartFragment = new ChartFragment();
        transaction.replace(R.id.fragmentContainer, chartFragment);
        transaction.commit();
    }
}

以下是我的活动布局。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include layout="@layout/toolbar" />
    <FrameLayout android:id="@+id/fragmentContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

以下是我的Fragment类
public class ChartFragment extends Fragment
{
    private LineChart mChart;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.graph, container, false);



        return v;
    }
}

以下是该片段的布局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.github.mikephil.charting.charts.LineChart
        android:id="@+id/lineChart1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</FrameLayout>

目前当我尝试在OnCreateView函数中填充视图时,应用程序会崩溃。

我收到的错误是:

java.lang.RuntimeException: 无法启动 activity ComponentInfo{com.MyCompany.ChartTest/com.MyCompany.ChartTest.MainActivity}: android.view.InflateException: Binary XML file line #5: Error inflating class com.github.mikephil.charting.charts.LineChart


有人名叫 Uncle-Leo 遇到了类似的异常,请参见 https://github.com/PhilJay/MPAndroidChart/issues/26;请清理库并查看依赖列表中是否有不同版本的 android-support-v4.jar。 - Mohammed Ali
谢谢,我已经检查了支持库使用的最新版本,并且没有其他库被包含进来。我已经删除了我的应用程序构建文件夹和库模块的构建文件夹并重新构建,但仍然出现相同的错误。 - Boardy
如果没有XML,会发生什么?您能够使用setContextView(new LineChart(this))或类似的东西吗? - Breadbin
我刚刚使用了你的代码,它很棒!你用的是哪个IDE?你是否已将jar文件导入libs文件夹,并使用“添加到构建路径”的方式将其添加到构建路径中? - Vigneshwaran Murugesan
5个回答

1
我真的看不出有什么问题。
示例项目 中使用的 Fragments 唯一的区别是它们对图表的宽度和高度使用了 wrap_content 而不是 match_parent
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.github.mikephil.charting.charts.LineChart
        android:id="@+id/lineChart1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</FrameLayout>

谢谢,我已经尝试将“match_parent”更改为“wrap_content”,但仍然出现相同的崩溃。 - Boardy
好的。你在项目中还使用其他的库吗? - Philipp Jahoda
我有一个自己的库,我在所有项目中都使用它,即支持库和appcompat支持库。 - Boardy
你的最低和目标SDK版本是什么?你是否使用了最新版本的JAR文件? - Philipp Jahoda
我的最小版本是15,目标是21。我认为我可能已经找到了问题所在。我有一个应用程序主题在我的自己的库中,并且清单设置了存储在我的库中的主题。我已经删除了我的库引用并将我的主题恢复为默认的Android主题,现在它可以工作了。 - Boardy
显示剩余2条评论

1
我已经确认了问题所在。
我的应用程序主题定义在一个库中,这个库被我的其他各种应用程序使用。但是不知何故,当在使用MPAndroidChartLib时从库中引用主题时,我会得到这个异常。如果我将主题样式从我的库中复制并粘贴到我的应用程序自己的样式文件中,并引用这个新文件,则可以正常工作。

我已经从库中删除了所有类型的资源,并将在下一个版本中发布更改。也许这可以解决问题。 - Philipp Jahoda
谢谢,期待尝试。 - Boardy

0

我曾经遇到过这个问题,最终找到了一个可行的解决方案。

线索是图表片段设计预览中的错误消息:

以下类无法实例化:
charting.charts.LineChart

异常详情
java.lang.ClassNotFoundException:
com.nineoldandroids.animation.ValueAnimator$AnimatorUpdateListener

com.nineoldandroids:library:2.4.+添加到我的项目的build.gradle文件中即可解决此问题:

dependencies {
compile 'com.android.support:support-v4:19.1.0'
compile 'joda-time:joda-time:2.5'
compile 'com.nineoldandroids:library:2.4.+'
compile files('libs/MPChart.jar') 
}

0

试试这个,可能会有帮助:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.github.mikephil.charting.charts.LineChart
        android:id="@+id/lineChart1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</FrameLayout>

这行代码:

xmlns:app="http://schemas.android.com/apk/res-auto"

很重要。

希望能有所帮助。


谢谢,但不幸的是没有成功,仍然得到相同的异常。 - Boardy

0
在onCreate()方法中添加以下代码对我很有帮助。
getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接