当我选择单选按钮时,我的应用程序崩溃了

时间:2016-07-28 06:28:26

标签: android arraylist radio-button indexoutofboundsexception

我有3个单选按钮,每个单选按钮执行一些操作,使用此单选按钮我正在过滤结果E.g为RB1为DATE-WISE RB2为客户名,RB3为Productname。

以上两个单选按钮工作正常(RB2和RB3),但当我选择RB3(DATE-WISE)时,我的应用程序崩溃了。

07-28 11:47:53.243 13244-13244/com.example.Minal E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.Minal, PID: 13244
java.lang.ArrayIndexOutOfBoundsException: length=0; index=-1
    at java.util.ArrayList.get(ArrayList.java:310)
    at com.example.Minal.Purchase_Pending$1.onCheckedChanged(Purchase_Pending.java:180)
    at android.widget.RadioGroup.setCheckedId(RadioGroup.java:173)
    at android.widget.RadioGroup.-wrap0(RadioGroup.java)
    at android.widget.RadioGroup$CheckedStateTracker.onCheckedChanged(RadioGroup.java:351)
    at android.widget.CompoundButton.setChecked(CompoundButton.java:159)
    at android.widget.CompoundButton.toggle(CompoundButton.java:115)
    at android.widget.RadioButton.toggle(RadioButton.java:76)
    at android.widget.CompoundButton.performClick(CompoundButton.java:120)
    at android.view.View$PerformClick.run(View.java:21147)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

我的Logcat:

environments {
    test {
        grails.logging.jul.usebridge = true
        grails.config.locations = ["file:${userHome}/.grails/${appName}-config-TEST.groovy"]
    }
    development {
        grails.logging.jul.usebridge = true
        grails.config.locations = ["file:${userHome}/.grails/${appName}-config-DEV.groovy"]
    }
    production {
        grails.logging.jul.usebridge = false
        grails.config.locations = ["file:${userHome}/.grails/${appName}-config-PROD.groovy"]
    }
}

2 个答案:

答案 0 :(得分:2)

在您的情况下,原因是cus_name.getSelectedItemPosition()返回-1(它不是listItem的索引)并且您的listItem大小为0。 因此,在获取阻止ArrayIndexOutOfBoundsException之类的值

之前,您应该检查它们
if(cus_name.getSelectedItemPosition() > -1 && cus_name.getSelectedItemPosition() < listItems.size()){
      cus_names = listItems.get(cus_name.getSelectedItemPosition());
}

希望这个帮助

答案 1 :(得分:1)

你在错误的位置写下了这一行,所以每当你得到空列表时,你的应用程序就会崩溃,

import UIKit

class ViewController: UIViewController {
    var datePickerView  : UIDatePicker = UIDatePicker()
    var timePickerView  : UIDatePicker = UIDatePicker()
    @IBOutlet weak var dateField: UITextField!
    @IBOutlet weak var timeField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        datePickerView.datePickerMode = .Date
        dateField.inputView = datePickerView
        datePickerView.addTarget(self, action: #selector(ViewController.handleDatePicker(_:)), forControlEvents: .ValueChanged)
        handleDatePicker(datePickerView)

        timePickerView.datePickerMode = .Time
        timeField.inputView = timePickerView
        timePickerView.addTarget(self, action: #selector(ViewController.handleTimePicker(_:)), forControlEvents: .ValueChanged)
        handleTimePicker(timePickerView)
    }

    func handleDatePicker(sender: UIDatePicker) {
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateStyle = .MediumStyle
        dateField.text = dateFormatter.stringFromDate(sender.date)
        dateField.resignFirstResponder()
    }

    func handleTimePicker(sender: UIDatePicker) {
        let timeFormatter = NSDateFormatter()
        timeFormatter.timeStyle = .ShortStyle
        timeField.text = timeFormatter.stringFromDate(sender.date)
        timeField.resignFirstResponder()
    }
}

因此,请尝试以下代码,不要在onle中使用所有三个条件,

cus_names = listItems.get(cus_name.getSelectedItemPosition());