Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将arraylist存储到数组中时出现异常_Java_Android_Exception_Pie Chart - Fatal编程技术网

Java 将arraylist存储到数组中时出现异常

Java 将arraylist存储到数组中时出现异常,java,android,exception,pie-chart,Java,Android,Exception,Pie Chart,我需要为我的应用程序使用chartengine创建饼图,在那里我需要为类别及其费用绘制饼图。我需要在饼图中获取类别,所以我从数据库中将类别放入arraylist,然后将它们放入数组中 现在它给了一个例外 10-29 21:41:22.720 878-878/com.example.username.weddingplanning E/AndroidRuntime﹕ 致命异常:主 java.lang.RuntimeException:无法启动活动组件信息{com.example.username.

我需要为我的应用程序使用chartengine创建饼图,在那里我需要为类别及其费用绘制饼图。我需要在饼图中获取类别,所以我从数据库中将类别放入arraylist,然后将它们放入数组中

现在它给了一个例外

10-29 21:41:22.720 878-878/com.example.username.weddingplanning E/AndroidRuntime﹕ 致命异常:主 java.lang.RuntimeException:无法启动活动组件信息{com.example.username.weddingplanning/com.example.username.weddingplanning.pie}: java.lang.ArrayStoreException:类型为的源[0] com.example.username.weddingplanning.category不能存储在中 java.lang.String[]类型的目标数组 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2250)上 位于android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2304) 在android.app.ActivityThread.access$700(ActivityThread.java:152) 在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284) 位于android.os.Handler.dispatchMessage(Handler.java:99) 位于android.os.Looper.loop(Looper.java:176) 位于android.app.ActivityThread.main(ActivityThread.java:5299) 位于java.lang.reflect.Method.Invokenactive(本机方法) 位于java.lang.reflect.Method.invoke(Method.java:511) 在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)上 位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 在dalvik.system.NativeStart.main(本机方法) 原因:java.lang.ArrayStoreException:com.example.username.weddingplanning.category类型的源[0]无法存储在中 java.lang.String[]类型的目标数组 位于java.lang.System.arraycopy(本机方法) 在java.util.ArrayList.toArray(ArrayList.java:519) 位于com.example.username.weddingplanning.pie.onCreate(pie.java:33) 位于android.app.Activity.performCreate(Activity.java:5326) 位于android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2213)上 位于android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2304) 在android.app.ActivityThread.access$700(ActivityThread.java:152) 在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284) 位于android.os.Handler.dispatchMessage(Handler.java:99) 位于android.os.Looper.loop(Looper.java:176) 位于android.app.ActivityThread.main(ActivityThread.java:5299) 位于java.lang.reflect.Method.Invokenactive(本机方法) 位于java.lang.reflect.Method.invoke(Method.java:511) 在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)上 位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 在dalvik.system.NativeStart.main(本机方法)

这是我的DBhelper类

public class DBhelper extends SQLiteOpenHelper {

    static final String DATABASE = "wedding9.db";
    static final int VERSION = 9;
    static final String TABLE1 = "Category";
    static final String TABLE2 = "Budget";
    static final String TABLE3 = "Expenses";

    static final String C_ID = "_id";
    static final String Name = "name";
    static final String B_ID = "_id";
    static final String Description = "description";
    static final String Amount = "amount";

    public static final String ID1 = "_id";
    public static final String DATE_T1 = "date1";
    public static final String CATEGORY = "category";
    public static final String DETAIL = "detail";
    public static final String AMOUNT1 = "amount1";
    public static final String STATUS = "status";
    public static final String EX_YEAR = "exyear";
    public static final String EX_MONTH = "exmonth";

    public DBhelper(Context context) {
        super(context, DATABASE, null, VERSION);
    }

    public void onCreate(SQLiteDatabase db) {

        db.execSQL("CREATE TABLE " + TABLE1 + "(" + C_ID
                + " INTEGER PRIMARY KEY AUTOINCREMENT," + Name + " text unique not null)");

        db.execSQL("CREATE TABLE " + TABLE2 + "(" + B_ID
                + " INTEGER PRIMARY KEY AUTOINCREMENT," + Description + " text,"
                + Amount + " text, FOREIGN KEY (" + Description + ") REFERENCES " + TABLE1 + "(" + Name + "));");

        db.execSQL("CREATE TABLE " + TABLE3 + " ( "
                + ID1 + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                + DATE_T1 + " text, "
                + CATEGORY + " text, "
                + DETAIL + " text, "
                + STATUS + " text, "
                + EX_YEAR + " text, "
                + EX_MONTH + " text, "
                + AMOUNT1 + " text, FOREIGN KEY (" + CATEGORY + ") REFERENCES " + TABLE1 + "(" + Name + "));");


    }

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        db.execSQL("drop table " + TABLE1);
        onCreate(db);
    }


    public ArrayList<category> getCategories() {
        ArrayList<category> arrayList = new ArrayList<category>();
        SQLiteDatabase db = getReadableDatabase();
        Cursor c = db.query(DBhelper.TABLE1, null, null, null, null, null, null);
        while (c.moveToNext()) {
            category cat = new category(c.getInt(0), c.getString(1));
            arrayList.add(cat);

        }

        return arrayList;
    }

    public boolean checkIdExist(String name) {
        SQLiteDatabase db = getReadableDatabase();
        Cursor c = db.query(DBhelper.TABLE1, null, null, null, null, null, null);
        while (c.moveToNext()) {
            if (c.getString(1).equals(name))
                return false;
        }

        return true;
    }

    public double getTotalbudget() {
        SQLiteDatabase db = this.getWritableDatabase();
        String query = "SELECT SUM(Amount) FROM " + TABLE2;

        Cursor c = db.rawQuery(query, null);
        //Add in the movetofirst etc here? see SO
        c.moveToFirst();
        double i = 0;
        i = c.getDouble(0);

        return i;
    }

    public double getTotalexpenses() {
        SQLiteDatabase db = this.getWritableDatabase();
        String query = "SELECT SUM(AMOUNT1) FROM " + TABLE3;

        Cursor c = db.rawQuery(query, null);
        c.moveToFirst();
        double e = 0;
        e = c.getDouble(0);

        return e;
    }


    public boolean checkBudget(String cat) {
        SQLiteDatabase db = this.getReadableDatabase();
        String query = "SELECT e.category " +
                "FROM expenses e, budget b " +
                "WHERE e.category=b.description and e.category='" + cat +
                "' GROUP BY e.category " +
                "HAVING sum(amount1)>b.amount";

//        ArrayList<String> results = new ArrayList<String>();
        Cursor c = db.rawQuery(query, null);
        if (c.moveToNext()) {
            return true;
            //results.add(c.getString(0));
        }

        return false;
    }

    //get the category expenses
    public ArrayList<Expence> getCategoryExpences(String category) {
        ArrayList<Expence> expences = new ArrayList<Expence>();

        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int tmonth = c.get(Calendar.MONTH);
        int month = tmonth + 1;

        String query = "SELECT * FROM " + TABLE3 + " WHERE " + EX_YEAR + "='" + year + "' and " + EX_MONTH + "='" + month + "' and " + CATEGORY + "='" + category + "'";

        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.rawQuery(query, null);
        if (cursor.moveToFirst()) {
            do {
                Expence ex = new Expence();
                ex.setCategory(category);
                ex.setDate(cursor.getString(1));
                ex.setDescription(cursor.getString(3));
                ex.setStatus(cursor.getString(4));
                ex.setYear(cursor.getString(5));
                ex.setMonth(cursor.getString(6));
                ex.setAmount(cursor.getString(7));
                expences.add(ex);
            }
            while (cursor.moveToNext());
        } else {
            return null;
        }
        return expences;
    }


}
public类DBhelper扩展了SQLiteOpenHelper{
静态最终字符串数据库=“wedding9.db”;
静态最终int版本=9;
静态最终字符串TABLE1=“Category”;
静态最终字符串TABLE2=“预算”;
静态最终字符串TABLE3=“费用”;
静态最终字符串C_ID=“_ID”;
静态最终字符串Name=“Name”;
静态最终字符串B_ID=“_ID”;
静态最终字符串Description=“Description”;
静态最终字符串Amount=“Amount”;
公共静态最终字符串ID1=“\u id”;
公共静态最终字符串DATE_T1=“date1”;
公共静态最终字符串CATEGORY=“CATEGORY”;
公共静态最终字符串DETAIL=“DETAIL”;
公共静态最终字符串AMOUNT1=“AMOUNT1”;
公共静态最终字符串STATUS=“STATUS”;
公共静态最终字符串EX_YEAR=“exyear”;
公共静态最终字符串EX_MONTH=“exmonth”;
公共DBhelper(上下文){
super(上下文、数据库、null、版本);
}
public void onCreate(SQLiteDatabase db){
db.execSQL(“创建表”+TABLE1+”(“+C_ID
+“整型主键自动递增,”+Name+“文本唯一非空)”;
db.execSQL(“创建表”+TABLE2+”(“+B_ID
+整数主键自动递增,“+说明+”文本
+金额+”文本,外键(“+Description+”)引用“+TABLE1+”(“+Name+”);”;
db.execSQL(“创建表”+表3+”(“
+ID1+“整数主键自动递增,”
+日期_T1+“文本”
+类别+“文本,”
+详细信息+文本,“
+状态+“文本,”
+EX_YEAR+“文本”
+EX_月+“文本,”
+“数量1+”文本,外键(“+CATEGORY+”)引用“+TABLE1+”(“+Name+”);”;
}
public void onUpgrade(SQLiteDatabase db,int-oldVersion,int-newVersion){
db.execSQL(“drop table”+TABLE1);
onCreate(db);
}
公共阵列列表getCategories(){
ArrayList ArrayList=新的ArrayList();
SQLiteDatabase db=getReadableDatabase();
游标c=db.query(DBhelper.TABLE1,null,null,null,null,null);
while(c.moveToNext()){
类别cat=新类别(c.getInt(0),c.getString(1));
arrayList.add(cat);
}
返回数组列表;
}
公共布尔checkIdExist(字符串名称){
SQLiteDatabase db=getReadableDatabase();
游标c=db.query(DBhelper.TABLE1,null,null,null,null,null);
while(c.moveTo)
public class pie extends ActionBarActivity {


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pie);



        // Pie Chart Section Names


        ArrayList<category> bud = new DBhelper(this).getCategories();
        String[] code = bud.toArray(new String[bud.size()]);
        // Pie Chart Section Value
        double[] distribution = new double[code.length];
        for (int i=0; i<distribution.length; i++) {
            System.out.println(i);
            ArrayList<Expence> expences = new DBhelper(this).getCategoryExpences(code[i]);
            if(expences!=null){
                double total = 0.0;
                for (Expence ex : expences) {
                    total = total + Double.parseDouble(ex.getAmount());
                }
                distribution[i]=total;
            }
            else{
                distribution[i]=0.0;
            }
        }


        // Color of each Pie Chart Sections
        int[] colors = { Color.BLUE, Color.MAGENTA, Color.GREEN, Color.CYAN,Color.YELLOW,
                Color.RED };

        // Instantiating CategorySeries to plot Pie Chart
        CategorySeries distributionSeries = new CategorySeries("Expenses Graph");
        for (int i = 0; i < distribution.length; i++) {
            // Adding a slice with its values and name to the Pie Chart
            distributionSeries.add(code[i], distribution[i]);
        }

        // Instantiating a renderer for the Pie Chart
        DefaultRenderer defaultRenderer = new DefaultRenderer();
        for (int i = 0; i < distribution.length; i++) {
            SimpleSeriesRenderer seriesRenderer = new SimpleSeriesRenderer();
            seriesRenderer.setColor(colors[i]);
            seriesRenderer.setDisplayChartValues(true);
            // Adding a renderer for a slice
            defaultRenderer.addSeriesRenderer(seriesRenderer);
        }

        defaultRenderer.setChartTitle("Expenses Graph");
        defaultRenderer.setChartTitleTextSize(60);
        defaultRenderer.setLabelsTextSize(30);
        defaultRenderer.setLegendTextSize(30);
        defaultRenderer.setShowLegend(true);
        //defaultRenderer.setDisplayValues(true);
        defaultRenderer.setApplyBackgroundColor(true);
        defaultRenderer.setBackgroundColor(Color.BLACK);
        defaultRenderer.setLabelsColor(Color.WHITE);
        defaultRenderer.setZoomButtonsVisible(true);


        // Creating an intent to plot bar chart using dataset and multipleRenderer
        Intent intent = ChartFactory.getPieChartIntent(getBaseContext(), distributionSeries, defaultRenderer, "AChartEnginePieChartDemo");

        // Start Activity
        startActivity(intent);
    }



    }
String[] code = new String[bud.size()];
for (int i = 0; i < bud.size(); i++) {
    code[i] = bud.get(i).getCode();
}