I have a method leftup which has an onClick listener from a button. The purpose of method leftup is to do the following:
Get an array from resources xml
(Which contains names of my other activities)
Turn array into an array list
Remove a string from that array
(The current class name)
Convert the list back into an array
Get a random value from that array
Execute method goToActivity with parameter of that random value
I have spent hours on this task so far, because the logic of the code seems fine. The problem is, when I run the code, I get the infamous error:
Unfortunately, "App_Name" has stopped
Here is my code for method onClick and goToActivity:
Here is my array in the resources:
By the way, string activity state is the name of the activity :
I am sure the exception is in the method leftup, as none of the Toasts get executed, and the error happens immediately as I click on the button.
Thank you so much for all your help, I really appreciate it.
A fellow programmer,
{Rich}
Stack trace:
https://gist.github.com/anonymous/dcd63f51289aaf1a1933
Here is the stack trace because I couldn't format it, this is the git hub page.
I figured it out! After three long hours of tweaking the code, I finally got it! The arrayList had to be a linkedList. Thats it! Because, arrayList's aren't as flexible, and so one cannot remove or add items to them. I hope this answer helps, and saves you three long hours!
java.lang.UnsupportedOperationException
java.lang.reflect.InvocationTargetException
are the exceptions that happen on this issue. Just make sure to use a more flexible container. :)
{Rich}
Related
so I'm just getting started in Java/Android Development and currently working on an assignment which I've gotten to work the way I want it to except for one part. So in this app you are supposed to be able to add "sandwiches" to an order, and then have your order presented below. I have the user adding sandwiches through a ListView with clickable options, as that's the idea, but then when presenting the order I want to store and present all the clicks, i.e. all the mackor that "have been ordered" in the TextView below the ListView, however I only manage to store and present one selection at a time, and when another one is clicked, the previous one is replaced. I've been trying to read up on this and I'm thinking a for or do-while loop should probably be used for this? However I did try a couple different versions of that without getting it to work, so here I am.
So tl;dr everything is working the way I want it to except for the selections just "replacing" eachother instead of adding up - how do I add a String/object to the ArrayList everytime a user selects an option in the ListView instead of having it just switching these? I was advised to use ArrayList to store these selections, but right now it's not of much use as it's just holding one at a time lol. I'm sure there are a lot of other things not done ideally in this program as well but yeah. Any advice on how to get this to work properly would be much appreciated! I figured my XML shoulnd't be necessary to include as that's not the issue but I gladly will if needed.
So I am trying to create an array list of objects for a class project an I've looked around the internet for answers to this question but I don't really understand what is incorrect in my code. I am new to coding so it is probably something really simple (it is probably Ready to program seeing as it is very not normal to put it lightly)! So I am trying to create an ArrayList of the object subscribers and whenever I try to create a normal ArrayList with ArrayList sub = new ArrayList(); Ready to program is fine but when I do ArrayList<subscriber> arraylist = new ArrayList<subscriber>(); it highlights both the < saying ( expected instead of this token and on the ( it says Invalid ShiftExpression. I have no idea how to fix these errors, also this is the whole code that could be affected by this:
static boolean done = false;
ArrayList<subscriber> arraylist = new ArrayList<subscriber>();
On line 2 of the pasted code (static boolean done = false;), there is another error caused by the object array. Due to this error the semicolon is highlighted with the syntax error of [ expected instead of this token. I am extremely confused with this last one and would like to know if this problem is to do with my code or with Ready to Program (probably my code). And before people ask, no I am not using HSA and I'm trying to learn proper java (I've seen lots of people hating on the buggy mess that is HSA and I am trying to move away from it.
Edit: It turns out it is a bug with Ready to Program. Sorry for wasting your guys' time!
For this to work you would have to have another class called subscriber. You can't just make up a word and put it in the angle brackets of the ArrayList. For example
ArrayList<Integer> list = new ArrayList<>()
Integer is not a made up word, it is an internal class object that that is used by the arraylist.
As for your second error i suspect you are not doing this code inside a method, and intstead just doing it outside of the class.
Overall tho it is very hard to see what you are doing via your description and two lines of code you have posted.
This is just a bug in Ready to Program and not in Java. I tried the exact same code in Visual Studio Code and it worked perfectly fine.
I had been using Netbeans since I started programming and every time I wanted something done (downloading CSV from online, scan a file and paste it into another file, etc.), I just create a JButton and put and make it an action event, it is very easy with NetBeans GUI since i can just right click on the button and click Action Performed, and all the codes will be written for me.
That worked very well until a week ago, when one method can contain up to 10,000 lines of codes (I am not joking). So that's 1 month of work down the drain and I decided to start from scratch. Regardless, the 1 month helped since it was my first program and I did not even understand what a "method" or "class" is. (Irrelevant but funny, my previous code contains 18,000 lines and it is all written on one page because I have no idea how to call another method from another class).
Now I know that if I wanted to call a method from another class, I can just write: test.calculation() to access the method calculation() from test's class.
But now I ran into another problem, it seems like the purpose of calling another method is to get the return value of that method. I am currently working on writing a method which will run different things depending on the integer, and I have a feeling that it will goes up to 100+. The if statement would look like this:
if(ID == 1){do task number 1;}
if(ID == 2){do task number 2;}
....
if(ID == 100){do task 100;}
I am on task number 1, which involves downloading files from websites and editing them, the code for task number 1 is around 120 lines.
I am planning on making a new Java Class which does specifically that task, so instead of having 120 lines of code I can just have one code, something like this:
if(ID == 1){call class/method to do task};
But I am running into a few problems that I believe does not solve the root cause, some errors include non static cannot be referenced in static etc...
All I wanted task number 1 to do is to download a file and edit it. Anything is appreciated.
Thanks in advance.
If I write code sequentially, then after I enter some method call and opening parenthesis, I get a hint after delay:
if I discard it or return to the place later, I see no any hint:
How to invoke it without retyping the code?
Ctrl+P will give you a pop-up with the method parameters. Usefully, the parameter corresponding to your cursor position will be highlighted and will change as you move back and forth through the parameters.
Intellij provides a lot of assistance. See here for a summary of what's available.
I faced the same condition in the earlier days.
No need to retype the complete code but just retype the comma(,) intellij is intelligent enough to guess the next variable value and suggest you better. I used this many times so far and made me to save my time in all instances.
Hope i was useful.
On Mac the shortcut is ⌘ (Command) + P.
It works fine.
I'm doing a standard SQL query in Android:
String selection = "SELECT phraseA, phraseB FROM TableXYZ";
Cursor c1;
c1 = myDbHelper.myDataBase.rawQuery(selection,null);
c1.moveToNext();
while(!c1.isLast()){
toplist.add("Phrase: "+c1.getString(0)+" "+c1.getString(1);
c1.moveToNext();
}
c1.close();
The table is very small and the number of returned stirngs is less than 40. Toplist is an ArrayList. It is put into a ListView.To the best of my knowledge this should show me 40ish strings. Now, what happens is the strings are shown but then another load of empty ListView fields come up. I have no idea how. I thought my iteration might be wrong, but I can't find fault with it.
Aren't you skipping the last result always? I think you need: while(!c1.isAfterLast()) to keep from skipping the last item.
Also this line has a syntax error:
toplist.add("Phrase: "+c1.getString(0)+" "+c1.getString(1);
you are missing a ")" character.
As for your question, you aren't showing enough code to really see what the problem is. Are you sure you aren't adding any empty values to the ArrayList at some other point in your code? All you are showing here is how you are populating an array list, not how you are populating a ListView. Show your ListView code.
It would also be fairly trivial to attach the debugger to see what your ArrayList contains after this step, to see if the error is in this part of your code or somewhere else. Or for that matter even printing the ArrayList size to LogCat would help in debugging this.