When i run my app it crash with this phrase ( sorry for bad English )
I tried a lot to resolve this problem but i cant
gridLayout.getChildAt return null as the message says.
Try changing your condition with gridLayout.getchildCount()-1
You are looping until the gridlayout childCount and array indices start at 0 in kotlin.
So if you have an array of 10 elements, to access the first element you index it like array[0], for the second element you use array[1] and so on. So if you try to access array[10] that means you are trying to get the 11th element which doesn't exist as your array has a total of 10 elements. The last element in this example array is at index 9 so you access it by calling array[9].
In your case by looping until gridLayout.getChildCount(), when the loop reaches the last element it will try to access an element that doesn't exist in the array.
So you should loop until gridLayout.getChildCount() - 1 because that's the correct index for the last element.
Related
I'm working on a 3 track step sequencer, and the spinners for sample selection are causing me some trouble. The following code is executed when a saved pattern is loaded to set the correct samples.
for ((index, j) in spinners.withIndex()){
val item = loadingPattern[(48+index)]
Log.i("item", item.toString())
Log.i("int", item.toInt().toString)
j.setSelection(item.toInt())
}
The array "spinners" contains 3 spinners with 5 entries each. "loadingPattern" is a string that contains the pattern information. The last three characters are integers corresponding to the spinner position. When the spinner selections are as the following:
Spinner 1: Selected item index 0
Spinner 2: Selected item index 3
Spinner 3: Selected item index 4
The log "item" prints exactly these values. The log "int" however prints 48+index, so in this case 48, 50, 52. Since the toInt() function is also called on the value parsed to the .setSelection() function, this exception is triggered:
java.lang.ArrayIndexOutOfBoundsException: length=2; index=48
If anyone has an idea why this is happening, I would be very happy to know. Thank you all so much! I love this communtiy!
edit:
If the pattern is programmed like this:
typical pattern
The "patternString" is the following:
101010101010101010000000100000001000100010001000420
active steps are represented by 1, the others by 0. The last 3 digits are the spinner positions. Maybe this will help :)
Edit 2:
I just found this question: I have String with numbers when getting the char from string it shows '0'48 in kotlin. How to get the char alone in kotlin
And there they say that the ASCII value of 0 is 48. This means that toInt() probably returns the ASCII value of 0. But why is that and how can I get just the normal integer?
I found it out!
As stated in this article, the toInt() function only works for strings and indeed returns the ASCII value of chars. The correct way of doing this is using Character.getNumericValue().
I am having a hard time with a piece of code that to my confidence it should work without a doubt but that is not the case.
public static void clearUnits() {
try{
for (int i = 1; i <= 3; i++) {
Log.d("S.G.E", inventoryPriceUnitsList.get(i).toString());
inventoryPriceUnitsList.remove(i);
recipePriceUnitList.remove(i);
}
}catch (Exception e){
e.printStackTrace();
}
}
The purpose of this code is to run through an array list of 4 elements and remove all elements after the first element. I know this is very basic and am sorry for wasting your time but I just needed someone else to look at this because I just don't understand why it's behaving like this. the result of this code is supposed to leave an array with one element (element 0) but instead, it leaves that and also the 3rd element. I also log all elements that are supposed to be removed and it shows up properly.
The problem is that when you remove an element from the array, the array shifts. So let's say in first round you remove the first element, then element 1 becomes element 0, and 2 becomes 1. Now on the next round you are removing the new element 1, which was the original 2, but the original 1 remains at position 0.
The simple solution is to iterate backwards, that way you are always removing elements past the point that you are at. For example
for (int i = 3; i >= 0; i--)
will work fine.
I have a problem with the INDEX IN LIST method of App Inventor
which always is returning 0 (zero).
Abraham GetzlerPower User
Instead of using the list from csv table block,
use the split at \n block
on your incoming file text.
Since it is just a single column of text delimited
by new line (\n) characters, that will
turn it into a list for you.
by Abraham Getzler at google forum
I have an activity with 4 TextView elements with ids of Mon1, Mon2, Mon3, Mon4.
Is it possible to create a loop in the MainActivity.java code where I can perform, for example, a setText action on each of the 4 ids without having to list them out one-by-one.
ie. Mon*X*.setText=""; (where X is a value from 1 to 4).
I guess to take this one step further, if the ids were actually Mon1, Mon2, Mon3, Mon4, Tue1, Tue2, Tue3, Tue4, Wed1 .........Sun1, Sun2,Sun3, Sun4. Could a loop be created to not only change the number 1..4 but also use an array for the Mon, Tue, Wed etc.
The end result being some sort of loop that can do setText on ALL the ids that I need rather than 28 individual setText commands.
You could do something like:
TextView Mon1; //and do whatever with it
TextView Mon2; //And so on
TextView[] tv = {Mon1, Mon2, Mon3, /*etc*/}
int i = 0;
void doSomething(){while(i<=/*number of TextViews*/){tv[i].setText("BLAH");i++;}}
I hope this helped :D
Is it possible to create a loop in the MainActivity.java code where I
can perform, for example, a setText action on each of the 4 ids
without having to list them out one-by-one.
Yup. Use an array.
To take it another step further, use another array. It's what they're made for.
(By array, I mean an ArrayList, HashMap, dictionary, array, or any other data structure like that).
I am currently using the latest verion of Adobe Flash Builder to create a Mobile Application. For the application one feature is to allow the users to bookmark content an this is done by storing the id of the object to be Bookmarked into an SQLite db on the device. This part has been done successfully and they are stored fine.
Now what I want to do is to pull back the bookmarked id's from the database and pass them to a WebService call which needs to be made to an External Database. When I retrieve the Bookmark id's from the local database they are contained within object, I now need to find a way to take the id's from the database objects in the ArrayCollection and store them in a new array that will be passed to the WebService, as the webservice is expecting an Array of Int's and not Objects. Below is the code I created to see if the object items are within the array list of objects:
private function loop():void
{
var index:int;
for( index = 0; index < compsCollection.length; index++ )
{
trace( "Element " + index + " is " + compsCollection[index].comp_id );
}
}
Now when I test the app all seems fine and the trace statement returns the following:
Element 0 is 91
Element 1 is 9
Element 2 is 9
Element 3 is 9
Element 4 is 9
Element 5 is 9
Element 6 is 9
Element 7 is 282
Element 8 is 282
Element 9 is 282
Element 10 is 282
Element 11 is 282
Element 12 is 282
However I then tried to populate the Int values for each of the objects into a new array using the code below:
var ids:Array;
var index:int;
for( index = 0; index < compsCollection.length; index++ )
{
trace( "Element " + index + " is " + compsCollection[index].comp_id );
ids.push(compsCollection[index].comp_id);
}
}
However when I run this code I get this error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
This error occurs on line:
ids.push(compsCollection[index].comp_id);
I do not understand why i am getting this error, can anyone please help?? Thanks
While I don't know anything about Adobe Flash Builder, generally you have to instantiate an array before using it.
var ids:Array = [];
maybe?? (As RIAstar suggested)