java - Androidplot错误膨胀类

标签 java android android-inflate

我正在尝试按照此处的 androidplot 教程 http://androidplot.com/docs/quickstart/

我不知道如何修复此错误,因为它无法从我的 xml 文件中扩充类。我是 Android 应用程序编程新手,所以这可能是一个明显的错误。

错误

02-18 05:11:40.187: E/AndroidRuntime(14925): FATAL EXCEPTION: main 02-18 05:11:40.187: E/AndroidRuntime(14925):
        java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.simplexyplotactivity/com.example.simplexyplotactivity.MainActivity}:
        android.view.InflateException: Binary XML file line #5: Error
inflating class fragment 02-18 05:11:40.187: E/AndroidRuntime(14925):
            at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2136)
        02-18 05:11:40.187: E/AndroidRuntime(14925):    at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2174)
        02-18 05:11:40.187: E/AndroidRuntime(14925):    at
android.app.ActivityThread.access$700(ActivityThread.java:141) 02-18
        05:11:40.187: E/AndroidRuntime(14925):  at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1267)
        02-18 05:11:40.187: E/AndroidRuntime(14925):    at
android.os.Handler.dispatchMessage(Handler.java:99) 02-18
        05:11:40.187: E/AndroidRuntime(14925):  at
android.os.Looper.loop(Looper.java:137) 02-18 05:11:40.187:
        E/AndroidRuntime(14925):    at
android.app.ActivityThread.main(ActivityThread.java:5059) 02-18
        05:11:40.187: E/AndroidRuntime(14925):  at
java.lang.reflect.Method.invokeNative(Native Method) 02-18
        05:11:40.187: E/AndroidRuntime(14925):  at
java.lang.reflect.Method.invoke(Method.java:511) 02-18 05:11:40.187:
        E/AndroidRuntime(14925):    at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
        02-18 05:11:40.187: E/AndroidRuntime(14925):    at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555) 02-18
        05:11:40.187: E/AndroidRuntime(14925):  at
dalvik.system.NativeStart.main(Native Method) 02-18 05:11:40.187:
        E/AndroidRuntime(14925): Caused by: android.view.InflateException:
        Binary XML file line #5: Error inflating class fragment 02-18
        05:11:40.187: E/AndroidRuntime(14925):  at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
        02-18 05:11:40.187: E/AndroidRuntime(14925):    at
android.view.LayoutInflater.rInflate(LayoutInflater.java:746) 02-18
        05:11:40.187: E/AndroidRuntime(14925):  at
android.view.LayoutInflater.inflate(LayoutInflater.java:489) 02-18
        05:11:40.187: E/AndroidRuntime(14925):  at
android.view.LayoutInflater.inflate(LayoutInflater.java:396) 02-18
        05:11:40.187: E/AndroidRuntime(14925):  at
android.view.LayoutInflater.inflate(LayoutInflater.java:352) 02-18
        05:11:40.187: E/AndroidRuntime(14925):  at
com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:260)
        02-18 05:11:40.187: E/AndroidRuntime(14925):    at
android.app.Activity.setContentView(Activity.java:1893) 02-18
        05:11:40.187: E/AndroidRuntime(14925):  at
com.example.simplexyplotactivity.MainActivity.onCreate(MainActivity.java:28)
        02-18 05:11:40.187: E/AndroidRuntime(14925):    at
android.app.Activity.performCreate(Activity.java:5058) 02-18
        05:11:40.187: E/AndroidRuntime(14925):  at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
        02-18 05:11:40.187: E/AndroidRuntime(14925):    at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
        02-18 05:11:40.187: E/AndroidRuntime(14925):    ... 11 more 02-18
        05:11:40.187: E/AndroidRuntime(14925): Caused by:
        android.app.Fragment$InstantiationException: Unable to instantiate
fragment com.androidplot.xy.XYPlot: make sure class name exists, is
public, and has an empty constructor that is public 02-18
        05:11:40.187: E/AndroidRuntime(14925):  at
android.app.Fragment.instantiate(Fragment.java:584) 02-18
        05:11:40.187: E/AndroidRuntime(14925):  at
android.app.Fragment.instantiate(Fragment.java:552) 02-18
        05:11:40.187: E/AndroidRuntime(14925):  at
android.app.Activity.onCreateView(Activity.java:4706) 02-18
        05:11:40.187: E/AndroidRuntime(14925):  at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
        02-18 05:11:40.187: E/AndroidRuntime(14925):    ... 21 more 02-18
        05:11:40.187: E/AndroidRuntime(14925): Caused by:
        java.lang.ClassNotFoundException: Didn't find class
        "com.androidplot.xy.XYPlot" on path:
        /data/app/com.example.simplexyplotactivity-1.apk 02-18 05:11:40.187:
        E/AndroidRuntime(14925):    at
dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
        02-18 05:11:40.187: E/AndroidRuntime(14925):    at
java.lang.ClassLoader.loadClass(ClassLoader.java:501) 02-18
        05:11:40.187: E/AndroidRuntime(14925):  at
java.lang.ClassLoader.loadClass(ClassLoader.java:461) 02-18
        05:11:40.187: E/AndroidRuntime(14925):  at
android.app.Fragment.instantiate(Fragment.java:574) 02-18
        05:11:40.187: E/AndroidRuntime(14925):  ... 24 more

MainActivity.java

package com.example.simplexyplotactivity;
import java.util.Arrays;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.WindowManager;
import com.androidplot.xy.*;


/**
 * A straightforward example of using AndroidPlot to plot some data.
 */
public class MainActivity extends FragmentActivity
{
    private XYPlot plot;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        // fun little snippet that prevents users from taking screenshots
        // on ICS+ devices :-)
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);

        setContentView(R.layout.simple_xy_plot_example);
        setContentView(R.layout.simple_xy_plot_example);

        //initialize out XYPlot reference:
        plot = (XYPlot)findViewById(R.id.mySimpleXYPlot);

        //create a couple arrays of y-values to plot:
        Number[] series1Numbers = {1,8,5,2,7,4};
        Number[] series2Numbers = {4,6,3,8,3,10};

        //turn the above arrays into XYSeries':
        XYSeries series1=new SimpleXYSeries(
                Arrays.asList(series1Numbers),          //SimpleXYSeries takes a List so turn our array into a List
                SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, //Y_VALS_ONLY means use the element index as the x value
                "Series1");                             //Set the display title of the series

        //same as above
        XYSeries series2 = new SimpleXYSeries(Arrays.asList(series2Numbers), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "Series2");

        //creates a formatter to use for drawing a series using LineAndPointRenderer
        //and configure it from xml:
        LineAndPointFormatter series1Format = new LineAndPointFormatter();
        series1Format.setPointLabelFormatter(new PointLabelFormatter());
        series1Format.configure(getApplicationContext(), R.layout.line_point_formatter_with_plf1);

        //ass a new series' to the xyplot:
        plot.addSeries(series1, series1Format);

        //same as above:
        LineAndPointFormatter series2Format = new LineAndPointFormatter();
        series2Format.setPointLabelFormatter(new PointLabelFormatter());
        series2Format.configure(getApplicationContext(), R.layout.line_point_formatter_with_plf2);
        plot.addSeries(series2, series2Format);

        //reduce the number of range labels
        plot.setTicksPerRangeLabel(3);
        plot.getGraphWidget().setDomainLabelOrientation(-45);
    }
}

simple_xy_plot_example.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <fragment android:name="com.androidplot.xy.XYPlot"
        android:id="@+id/mySimpleXYPlot"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

</LinearLayout>

最佳答案

android.app.Fragment$InstantiationException: Unable to instantiate fragment com.androidplot.xy.XYPlot: make sure class name exists, is public, and has an empty constructor

它需要一个 Fragment,但 XYPlot 是一个 View(Group) 子类

所以你应该改变它

<View android:name="com.androidplot.xy.XYPlot"
        android:id="@+id/mySimpleXYPlot"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

关于java - Androidplot错误膨胀类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21852330/

相关文章:

java - IntelliJ Idea android application jakarta-commons-collections 多个dex文件定义

java - 带工具栏的 Android 抽屉式菜单

android - 从布局 android-support-v4 中膨胀 fragment 时出错

android - Google Map APIv2 实现,二进制 XML 文件行 #6 : Error inflating class fragment

java - "Object"不是这个 Realm 的架构的一部分

java - 验证战舰板,二维数组,如何检查板有效性的可能性?

java - Spring @Controller 异常处理程序和全局异常处理程序。如何调用两者

java - 这段用于 Android 的 Java 代码有什么问题?

Android将图标转换成另一个

android - android-getMenuInflater导致崩溃