简体   繁体   English

添加OnItemSelectedListener时应用程序崩溃

[英]app crashes when adding OnItemSelectedListener

I recently made an update to the iOS version of an app I made and I want to put that same functionality into the Android version. 我最近对我制作的应用的iOS版本进行了更新,我想将相同的功能添加到Android版本中。 Basically, it takes user input of a year, which they select from a list and then the year previous to that is entered into the placeholder text of text field I specify. 基本上,它需要用户输入年份,然后从列表中选择年份,然后将其之前的年份输入到我指定的文本字段的占位符文本中。 When I looked up how to do the same functionality in Android, I found out I needed an OnItemSelectedListener. 当我查找如何在Android中执行相同的功能时,我发现我需要一个OnItemSelectedListener。 However, that is when trouble arises. 但是,那是当麻烦出现时。 After adding the necessary code, and importing AdapterView into my Activity, my app keeps crashing on first launch. 添加必要的代码并将AdapterView导入我的Activity后,我的应用在首次启动时一直崩溃。 Since I am not sure where it keeps crashing, here is a link to my app's github page 由于我不确定它在哪里继续崩溃,这是我应用程序的github页面的链接

Github Experimental branch Github的实验分支

The code on the master branch works fine, so this is really strange. master分支上的代码工作正常,所以这确实很奇怪。

Since the files are hard to decipher, here is the code I used to apply the OnItemSelectedListener: 由于文件难以解密,因此以下是我用于应用OnItemSelectedListener的代码:

// give Spinner a listener for new functionality to work
selection.setOnItemSelectedListener(new OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> parentView, 
                               View selectedItemView, int position, long id) {
        // get year selection for use with new functionality
        int iyear = Integer.parseInt(selection.getSelectedItem().toString()); 
        balance.setHint(R.string.balance + " from 12/31/" + pyear.getPrevYear(iyear)); 
    }
    // create empty method
    public void onNothingSelected(AdapterView<?> parentView) {}
});

Make sure the var balance is not null at the time of setHint. 确保setHint时var balance不为null。 It could be that the id in findViewById is not right. 可能是findViewById中的ID不正确。 Make sure that R.id.amount is a view inside of R.layout.main. 确保R.id.amount是R.layout.main内部的视图。

I still don't see the LogCat which is the most vital piece of information when tracking down an error... 追踪错误时,我仍然看不到LogCat是重要的信息...

So these are my guesses: 所以这些是我的猜测:

  • I don't see where you define an adapter for selection so I assume that you do this in the XML. 我看不到在何处定义用于selection的适配器,因此我假设您在XML中进行此操作。 Have you confirmed that selection.getSelectedItem().toString() is a valid String for Integer.parseInt() ? 您是否已确认selection.getSelectedItem().toString()Integer.parseInt()的有效字符串?

    If you have access to the array (call it selectionArray ) you can simply use: 如果可以访问该数组(称为selectionArray ),则可以简单地使用:

     ... + pyear.getPrevYear(selectionArray[position]); 
  • R.string.balance is an integer that references the String that you want. R.string.balance是一个整数,它引用您想要的String。 To display the String itself you want to use getString(R.string.balance) or probably getResources().getString(R.string.balance) . 要显示String本身,您想使用getString(R.string.balance)或可能使用getResources().getString(R.string.balance)

  • Verify none of your variables are null: selection , balance , and pyear . 确认您的所有变量都不为空: selectionbalancepyear

To explain the first point a little more: 为了进一步解释第一点:

  1. Create a class variable for the array and the String: 为数组和字符串创建一个类变量:

     int[] choicesArray; 
  2. Initialize it in onCreate(); 在onCreate()中初始化它;

     choicesArray = getResources().getIntArray(R.array.choices_array); 
  3. In OnItemSelectedListener use: 在OnItemSelectedListener中使用:

     balance.setHint(getResources().getString(R.string.balance) + " from 12/31/" + (choicesArray[position] - 1)); 

I went through and copied things from the master branch and things started working without crashing. 我遍历并复制了master分支中的内容,并且一切开始正常运行而没有崩溃。 Now, I am having issues with the hint, but since this question on why it kept crashing, I am not sure I can keep the question open. 现在,我对提示有疑问,但是由于这个关于它为什么持续崩溃的问题,我不确定是否可以继续讨论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM