Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!

Joshua Harris

Ranch Hand
+ Follow
since Apr 27, 2015
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
2
Received in last 30 days
0
Total given
8
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Joshua Harris

Dirk Lucas wrote:Hello!

I am currently trying to create a minor android app. Hoping to learn from it while doing it. Now I stumbled upon the following problem: I wan't to make a new activity. Like the one in the third codeblock. However, this activity should show a listview with 3 listview-buttons, one for minimal, medium and maximum.

To clarify: the user will get a listview (this one works already) than select one button from this listview and he goes to the next activity with another Listview which has the 3 problematic buttons filled with the variables maximum, medium and minimal.

Problem is, I do not now how to get this done. I am still learning and I do not see how I can extract these 3 parameters from the items in the List<FoodTimes> and showcase ehm. Mostly because the toString method in the FoodTimes class returns a name. How the heck do I make it so that this class returns OR the name OR minimal, medium & maximum (without me getting a list of hashcodes instead of Strings) ?

I have created a class which provides and arraylist:



a class which defines the items in the list:



And a class which shows the first parameter of the FoodTimes objects in a listview:



And lastly my attempt: --> which does not work because I do not see how I can get the right data in the String array (and I doubt if I am even in the right direction. I hope you understand what I am trying to ask as I feel my head is wuzzling and things aren't super clear atm:P Thanks in advance for any help!



I don't 100% know what it is you're trying to do, but I understand that you're trying to allow the user to press one button from a listview (Which you already have working), which then takes them to another listview with 3 buttons which have written on them the 3 variables (maximum, medium and minimal)?

If I'm correct, then all you need to do to pass the variables to the new activity which displays the 3 buttons is use an intent.

Just do something like this:



then in your activity which has the three buttons you need to call getIntent() to obtain the variables.

so something like this



and just do the same for the other variables too.

Make sure you call getIntent() within the onCreate method. You can declare the variables as instance variables, but when you want to assign the variables using getIntent() make sure you do that within the onCreate method.

Once you have the variables you just need to add the code to make the buttons display them.

I'm not sure whether I answered your question or not but I hope this helped lol.
5 years ago

Maneesh Godbole wrote:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference


Check the getIntent() for null



I realise my mistake now. I was calling the getIntent() method outside of the onCreate method.

As soon as I moved the code calling getIntent() inside the onCreate method it's all working fine. Stupid mistake lol.
5 years ago

Maneesh Godbole wrote:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference


Check the getIntent() for null



Well I just added this code instead:



and it seems to be working now... I don't understand why?
5 years ago

Maneesh Godbole wrote:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference


Check the getIntent() for null



Oh right, what do you mean? Sorry I'm a very beginner at android. How do I check getIntent for null?
5 years ago

Maneesh Godbole wrote:The first step to debug this would be to confirm the value being set is actually not null. Have you tried checking what is the value at this point?



I just edited the code to give me log tags, here they are:

09-08 12:09:55.196 1362-1362/com.example.testing_load D/LOG: class com.example.testing_load.TierOneListView﹕ VALUE OF name IS: ENGINE
09-08 12:09:55.196 1362-1362/com.example.testing_load D/LOG: class com.example.testing_load.TierOneListView﹕ VALUE OF id IS: 3

so the id string is definitely not null.
5 years ago
Hello,

So I'm simply trying to pass a string to another activity using intent.putExtra(name, value); but for some reason the activity receiving the intent is saying it's a null reference. I have no idea why, because I've basically just copied the exact same code from a different activity which is performing the same task correctly.

Here is my code:





and here is the code for where the intent is received:



here is the error msg: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference
at com.example.testing_load.TierTwoListView.<init>(TierTwoListView.java:33)

(java line 33 is line 8 in the code snippet i provided)

as I said earlier I am using basically the exact same code to perform this task in other activities and it's working fine. It's probably a very simple and stupid mistake but I'd be grateful if someone could point out why the error is occurring.
5 years ago

Jules Aglo wrote:There were quite a few issues that I found.

First the object you are getting back is a JSONArray not a JSONObject.

Then you initializing your String[] to 0. Which means you're going to go out of bounds every time you add something to it.
This is a good place to learn about Arrays https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Then to answer your question. In onPostExecute you need to add the items to your list then notify your adapter that items have been added.

I would probably use RxObservable instead of AsyncTasks.
RxJava
RxAndroid


Here's a working example. Its uses Butterknife.
Its not perfect but its a start.



thanks for that, that reply was really helpful. Managed to get it working in the end.
5 years ago
Hello,

So I'm trying to achieve the very simple task of, as the title suggests, downloading some JSON data then just putting it in a ListView, however I just can't seem to make it work.

I am trying to download the data from here: http://www.webfleetsystems.co.uk/wbfleet/defects/getdefectlist.ashx

I've been looking through the documentation for Asynctask and things like that for a while but somehow I just still can't manage to achieve this simple task.

Here is my code for the class which downloads the information:



and the code for the class which is supposed to display the listview:



How do I correctly bind the data I downloaded to the listview to make it display the information?

I know there's probably tons of things I'm doing wrong but if someone could help me out I'd be really grateful. I know this is probably a noob question but I currently suck at android development.

edit: I forgot to add the code for the class which downloads the JSON data so here it is:

5 years ago

Tim Cooke wrote:Have a read through the Java Tutorial for the Switch Statement paying particular to the function and placement of the break; statement.



thank you. I can't believe that after like 30 mins of trying to figure this out I didn't even think to look at the documentation for switch statements.
5 years ago
oops I should've posted this in the android forum, my bad
5 years ago
Code being activated when it shouldn't be

I'm a beginner at android development and I can't for the life of my figure out why this is happening. Basically, I have this code:


But whenever I push the "NO" button, activating the "case R.id.NO" section of code, the code does execute but then for some reason the "case R.id.YES" section of code is also activated despite me never even pressing the YES button. I mean once I press the "NO" button, a new activity is supposed to start so it shouldn't be possible to press the YES button anyway. Why is this happening? I added some log tags within the code and I can show them here:
08-25 21:36:37.517 7267-7267/com.example.testing_load D/LOG: class com.example.testing_load.DefectEntry﹕ CASE: NO
08-25 21:36:37.527 7267-7267/com.example.testing_load D/LOG: class com.example.testing_load.DefectEntry﹕ CASE: YES

So according to the log tags I am pressing the YES button 0.01 of a second after I hit the NO button, but obviously this is not the case.

Here is the code for the entire class:


thanks for any help in advance.
5 years ago

Bear Bibeault wrote:org.json is not Gson. Gson's base package is com.google.gson.

Are you compiling from the command line or within an IDE? Likely, whichever is not configured to include the jar file in the classpath.



I tried both import com.google.gson.Gson; and import com.google.gson.GsonBuilder; but as I mentioned it said the packages do not exist. The website I'm coding on is doing the compiling I presume.

Bear Bibeault wrote:Did the library you choose come with documentation? If not, choose another.

For example, here is the Gson user guide.



Yeah as I said I cloned the gson repository but I can't seem to import it into the website. It just says the packages do not exist.
this is my code so far



I don't think you need to use gson or anything like that because otherwise the website should have mentioned it. My code doesn't work because it can't seem to find the JSON object "max." How do I convert the massive string which you get from http://api.openweathermap.org/data/2.5/forecast/daily?q=94043&mode=json&units=metric&cnt=7 into something useful which I can actually use in my code? because right now it just seems like the compiler can't make any use of that string

Bear Bibeault wrote:An IDE is just a fancy editor. You can use any Java library without an IDE.



Could you just walk me through what to do? I cloned the gson github repository but I'm still really stuck

I know I probably need to parse the json string I have but I'm not really sure how to do that or what to do after