为什么java.lang.IllegalArgumentException?

时间:2014-01-30 09:47:35

标签: java android sqlite

我有一个方法,当提供ID作为参数时,返回具有该ID的那个人的仪表读数的集合。

public List<VMeterReadings> GetIndividualReading(int id){
     List<VMeterReadings> individual = new ArrayList<VMeterReadings>();
        String selectQuery = " SELECT " + READINGDATEVIEW + "," + CurrentREADING + ","+ PrevREADINGDATE +","+ prevREADING +","+ UNITs+ ","+ RATE + ","+ AMOUNT + " FROM "
                           + VIEW_DETAILEDREADING + " Where " + KEY_ID + " = "+id + "  Order by " + READINGDATE + " DESC ";

        SQLiteDatabase db = this.getReadableDatabase();     
        Cursor c = db.rawQuery(selectQuery, null);
     if(c.moveToFirst()){

        do{
            VMeterReadings t =new VMeterReadings();                 
            try {
                t.setReadingDate(fm.parse(c.getString(c.getColumnIndex(READINGDATE))));
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                Log.e("parsing Reading date",e.toString());
            }
            t.setCurrentReading(c.getDouble(c.getColumnIndex(CurrentREADING)));
            try {
                t.setPreviousReadingDate(fm.parse(c.getString(c.getColumnIndex(PrevREADINGDATE))));
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                Log.e("parsing current reading",e.toString());
            }
            t.setPreviousMeterReading(c.getDouble(c.getColumnIndex(prevREADING)));
            t.setUnits(c.getDouble(c.getColumnIndex(UNITs)));
            t.setRate(c.getDouble(c.getColumnIndex(RATE)));
            t.setAmount(c.getDouble(c.getColumnIndex(AMOUNT)));
            individual.add(t);
        } while(c.moveToNext());
        }
     c.close();
     db.close();
     return individual;
}

我的目标是能够从一个将在其构造函数中提供ID的类调用该方法。我创建了类,它扩展了BaseAdapter。 这是因为我想将返回数据加载到自定义布局文件中。

我制作了xml文件。

这是扩展基本适配器的类。

package com.example.metermanager;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import meter.manager.helper.MyDatabaseAdapter;
import meters.model.VMeterReadings;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class IndividualReadings extends BaseAdapter {

    @SuppressWarnings("unused")
    private Context context;
    private int id;
    MyDatabaseAdapter db;
    private static String DB_NAME="meterapp.sqlite";    
    private List<VMeterReadings> readings =new ArrayList<VMeterReadings>();
    DecimalFormat df = new DecimalFormat("#,###,###,###");
    SimpleDateFormat fm =new SimpleDateFormat("dd/MM/yyyy");
public IndividualReadings(Context context1, int myid) {     
        this.context=context1;
        this.id=myid;
        MyDatabaseAdapter db= new  MyDatabaseAdapter( context1,DB_NAME);
        readings=db.GetIndividualReading(myid);
    }



    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return readings.size();
    }

    @Override
    public Object getItem(int index) {
        // TODO Auto-generated method stub
        return getItem(index);
    }

    @Override
    public long getItemId(int index) {
        // TODO Auto-generated method stub
        return index;
    }

    @Override
    public View getView(int index, View view, ViewGroup parent) {
        try{

            if (view ==null){
                LayoutInflater inflater=
                        LayoutInflater.from(parent.getContext());
                      view =inflater.inflate(R.layout.individual_reading,parent,false);               

            }       

            VMeterReadings reading =readings.get(index);    
            // testing whether data returned.
            //correct data returned
            Log.e("readingDtae",fm.format(reading.getReadingDate()));
            Log.e("Reading",df.format(reading.getCurrentReading()));
            Log.e("PreviousReadingDate",fm.format(reading.getPreviousReadingDate()));
            Log.e("PreviousReading",df.format(reading.getPreviousMeterReading()));


            TextView readingdateTextView =(TextView)
                    view.findViewById(R.id.textView8);

            readingdateTextView.setText(fm.format(reading.getReadingDate()));

            TextView currentreadingTextView =(TextView)
                    view.findViewById(R.id.textView9);      
            currentreadingTextView.setText( Double.toString((reading.getCurrentReading())));


            TextView previoustreadingdateTextView =(TextView)
                    view.findViewById(R.id.textView10);     
            previoustreadingdateTextView.setText(df.format((reading.getPreviousReadingDate())));

             TextView previousmeterReading =(TextView)
                        view.findViewById(R.id.textView11);     
             previousmeterReading.setText(Double.toString((reading.getPreviousMeterReading())));

             TextView units =(TextView)
                        view.findViewById(R.id.textView12);     
                 units.setText(Double.toString((((reading.getUnits())))));      

                 TextView rate =(TextView)
                            view.findViewById(R.id.textView13);     
                     rate.setText(Double.toString(reading.getRate()));                       
                         TextView amountTextView =(TextView)
                                    view.findViewById(R.id.textView14);     
                             amountTextView.setText(df.format((reading.getAmount()))); 


            }catch(Exception e)
            {
                Log.e("Error loading data in listbox",e.toString());
            }
            return view;
    }
}

在构造函数

public IndividualReadings(Context context1, int myid) {     
        this.context=context1;
        this.id=myid;
        MyDatabaseAdapter db= new  MyDatabaseAdapter( context1,DB_NAME);
        readings=db.GetIndividualReading(myid);
    }

我希望调用代码提供参数myid,该类在调用方法时应该使用它来获取仪表读数的集合......

我在spinner控件中调用类的示例如下。微调器是一个租户列表。代码获取所选租户的ID并将其作为iD提供给类使用以获取集合并填充xml文件以供显示。

public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        try{
            int pos = spinner.getSelectedItemPosition();
            // get the tenant selected  in the list
            TenantList tenant=(TenantList)readings.get(pos);
            // get the ID
            int id =tenant.get_id();
            // instantiate the get individual readings class and pass the id as the parameter
            IndividualReadings individualreadings;  
            individualreadings= new IndividualReadings(this,id);
            ListView listview =(ListView)findViewById(R.id.reading_list);           
            listview.setBackgroundColor(color.holo_orange_dark);    

            listview.setAdapter(individualreadings);
            } catch(Exception e)
            {
                Log.e("Spinner Error", e.toString());
            }

metere读数显示在listview控件中。

我得到了我在方法getView中的IndivualReadings类中提到的错误(它应该用数据填充自定义视图)。 关于如何解决问题的任何想法?

我到目前为止已经过测试,发现id参数已正确传递并返回正确的数据。

但我不知道为什么我在getView方法中得到了这个错误。

罗纳德

这是Logcat条目

01-30 12:41:20.782: D/dalvikvm(387): GC_EXTERNAL_ALLOC freed 88K, 52% free 2622K/5379K, external 515K/517K, paused 102ms
01-30 12:41:20.802: E/Database(387): close() was never explicitly called on database 'data/data/com.example.metermanager/databases/meterapp.sqlite' 
01-30 12:41:20.802: E/Database(387): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
01-30 12:41:20.802: E/Database(387):    at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1847)
01-30 12:41:20.802: E/Database(387):    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:820)
01-30 12:41:20.802: E/Database(387):    at meter.manager.helper.MyDatabaseAdapter.openDataBase(MyDatabaseAdapter.java:191)
01-30 12:41:20.802: E/Database(387):    at meter.manager.helper.MyDatabaseAdapter.<init>(MyDatabaseAdapter.java:102)
01-30 12:41:20.802: E/Database(387):    at com.example.metermanager.IndividualReadings.<init>(IndividualReadings.java:32)
01-30 12:41:20.802: E/Database(387):    at com.example.metermanager.MainActivity.onItemSelected(MainActivity.java:105)
01-30 12:41:20.802: E/Database(387):    at android.widget.AdapterView.fireOnSelected(AdapterView.java:871)
01-30 12:41:20.802: E/Database(387):    at android.widget.AdapterView.access$200(AdapterView.java:42)
01-30 12:41:20.802: E/Database(387):    at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:837)
01-30 12:41:20.802: E/Database(387):    at android.os.Handler.handleCallback(Handler.java:587)
01-30 12:41:20.802: E/Database(387):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-30 12:41:20.802: E/Database(387):    at android.os.Looper.loop(Looper.java:123)
01-30 12:41:20.802: E/Database(387):    at android.app.ActivityThread.main(ActivityThread.java:3683)
01-30 12:41:20.802: E/Database(387):    at java.lang.reflect.Method.invokeNative(Native Method)
01-30 12:41:20.802: E/Database(387):    at java.lang.reflect.Method.invoke(Method.java:507)
01-30 12:41:20.802: E/Database(387):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-30 12:41:20.802: E/Database(387):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-30 12:41:20.802: E/Database(387):    at dalvik.system.NativeStart.main(Native Method)
01-30 12:41:22.862: E/readingDtae(387): 20/01/2014
01-30 12:41:22.862: E/Reading(387): 150
01-30 12:41:22.872: E/PreviousReadingDate(387): 10/01/2014
01-30 12:41:22.872: E/PreviousReading(387): 100
01-30 12:41:22.872: E/Error loading data in listbox(387): java.lang.IllegalArgumentException
01-30 12:41:22.903: E/readingDtae(387): 10/01/2014
01-30 12:41:22.903: E/Reading(387): 100
01-30 12:41:22.903: E/PreviousReadingDate(387): 01/01/2014
01-30 12:41:22.903: E/PreviousReading(387): 0
01-30 12:41:22.912: E/Error loading data in listbox(387): java.lang.IllegalArgumentException
01-30 12:41:22.923: E/readingDtae(387): 20/01/2014
01-30 12:41:22.933: E/Reading(387): 150
01-30 12:41:22.933: E/PreviousReadingDate(387): 10/01/2014
01-30 12:41:22.933: E/PreviousReading(387): 100
01-30 12:41:22.943: E/Error loading data in listbox(387): java.lang.IllegalArgumentException
01-30 12:41:23.063: V/Path(387): data/data/com.example.metermanager/databases/
01-30 12:41:23.063: V/name(387): meterapp.sqlite
01-30 12:41:23.063: E/CheckPath(387): data/data/com.example.metermanager/databases/meterapp.sqlite
01-30 12:41:23.242: D/Exits(387): Db Exist
01-30 12:41:23.412: E/readingDtae(387): 20/01/2014
01-30 12:41:23.412: E/Reading(387): 160
01-30 12:41:23.412: E/PreviousReadingDate(387): 10/01/2014
01-30 12:41:23.423: E/PreviousReading(387): 60
01-30 12:41:23.423: E/Error loading data in listbox(387): java.lang.IllegalArgumentException
01-30 12:41:23.452: E/readingDtae(387): 10/01/2014
01-30 12:41:23.462: E/Reading(387): 60
01-30 12:41:23.462: E/PreviousReadingDate(387): 01/01/2014
01-30 12:41:23.462: E/PreviousReading(387): 0
01-30 12:41:23.472: E/Error loading data in listbox(387): java.lang.IllegalArgumentException
01-30 12:41:23.483: E/readingDtae(387): 20/01/2014
01-30 12:41:23.483: E/Reading(387): 160
01-30 12:41:23.502: E/PreviousReadingDate(387): 10/01/2014
01-30 12:41:23.502: E/PreviousReading(387): 60
01-30 12:41:23.502: E/Error loading data in listbox(387): java.lang.IllegalArgumentException
01-30 12:41:23.523: E/readingDtae(387): 20/01/2014
01-30 12:41:23.523: E/Reading(387): 160
01-30 12:41:23.523: E/PreviousReadingDate(387): 10/01/2014
01-30 12:41:23.532: E/PreviousReading(387): 60
01-30 12:41:23.532: E/Error loading data in listbox(387): java.lang.IllegalArgumentException

1 个答案:

答案 0 :(得分:0)

previoustreadingdateTextView.setText(df.format((reading.getPreviousReadingDate())));

您正在日期应用Decimalformat。