异常NumberFormatException ListView OnItemClickListener

时间:2014-06-29 18:12:03

标签: java android eclipse listview double

嘿:)我想点击我的ListView项目但是我得到了这些异常

Thread [<1> main] (Suspended (exception NumberFormatException)) 
<VM does not provide monitor information>   
StringToReal.invalidReal(String, boolean) line: 63  
StringToReal.parseDouble(String) line: 248  
Double.parseDouble(String) line: 295    
Double.valueOf(String) line: 332    
MainActivity$5.onItemClick(AdapterView, View, int, long) line: 719  
ListView(AdapterView).performItemClick(View, int, long) line: 299   
ListView(AbsListView).performItemClick(View, int, long) line: 1282  
ListView.performItemClick(View, int, long) line: 4450   
AbsListView$PerformClick.run() line: 3174   
AbsListView$3.run() line: 3925  
Handler.handleCallback(Message) line: 733   
ViewRootImpl$ViewRootHandler(Handler).dispatchMessage(Message) line: 95 
Looper.loop() line: 157 
ActivityThread.main(String[]) line: 5872    
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
Method.invoke(Object, Object...) line: 515  
ZygoteInit$MethodAndArgsCaller.run() line: 858  
ZygoteInit.main(String[]) line: 674 
NativeStart.main(String[]) line: not available [native method]  

这是我的代码,它提供了例外:

 void registerListClickCallback() {
    ListView myList = (ListView) findViewById(R.id.listViewFave);
    myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View viewClicked, 
                int position, long idInDB) {
            Cursor cursor = myDBAF.getRow(idInDB);
            if (cursor.moveToFirst()) {
                long idDB = cursor.getLong(DBAdapter.COL_ROWID);
                int Kalo = cursor.getInt(DBAdapter.COL_KALO);
                int Hydrate = cursor.getInt(DBAdapter.COL_HYDRATE);
                int Fat = cursor.getInt(DBAdapter.COL_FAT);
                int Protein = cursor.getInt(DBAdapter.COL_PROTEIN);
                 //int to double

                KalorienenVerzehrt=KalorienenVerzehrt+(double)Kalo;
                KalorienenUebrig=KalorienenUebrig-(double)Kalo;
                HydrateG=HydrateG+(double)Hydrate;
                FetteG=FetteG+(double)Fat;
                EiweisseG=EiweisseG+(double)Protein;

谢谢你的帮助:)  抱歉我的坏Englsih;)

1 个答案:

答案 0 :(得分:0)

摆脱崩溃

在try-catch中解析你的解析方法,捕获NumberFormatException。如果发生异常,请通知用户他们输入了无效值,并且不继续进行计算

try {
    Float value = Float.valueOf(text);

    if (value == null || value == 0f) {
        answer.setText("Zero");
    }
} catch (NumberFormatException e) {
    answer.setText("Invalid input");
}