Question

SOLVED THANK YOU VERY MUCH EVERYONE

Helle, and first of all thanks for clicking on my question. I've made an app thay gets a json file of the weather. It then parses this JSON file to get the weather variables like temperature etc. It displays those variables in textView's. The problem is that when I press the button (on my emulator) it says "Unfortunaly, (app name) has stopped working". Below you can find the code I used to make all this. Help will be appreciated. If you need any more files like activity_main.xml, strings.xml,... just ask.

Thank you for helping THANK YOU EVERYONE FOR HELPING, I THINK THERE ARE 3 MORE ERROR TO BE SOLVED, BUT I DON'T UNDERSTAND THESE ERROR LOGS, I JUST STARTED CODING, THANK YOU EVERYONE FOR THE HELP (YOU CAN FIND THEM AT THE BOTTOM OF THE POST

MainActivity.java:

package com.example.thelexapp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Random;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final Button button = (Button) findViewById(R.id.button2);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Perform action on click   
            try {
                getURLforweather();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });

} 

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;

    if (location != null) {

        TextView text = (TextView) findViewById(R.id.textView1);
        text.setTextColor(Color.parseColor("#808080"));
        double longitude = location.getLongitude();
        double latitude = location.getLatitude();
        String longitudestring = String.valueOf(longitude);
        String latitudestring = String.valueOf(latitude);

        String url =     "http://api.worldweatheronline.com/free/v1/weather.ashx?q="
                + longitudestring
                + ","
                + latitudestring
                +   "&format=json&num_of_days=1&key=hv3rd4u49qdmf6q3hp5apy6b";


        parseJson(url);
String temp_C = null;
        String windspeedKmph = null;
        String weatherDesc = null;

        TCWSKMPH(temp_C, windspeedKmph);
        WD(weatherDesc);

    }
}

public void parseJson (String url) {
    DefainputStream = null;
    String result = null;
    try {
        HttpResponse response = httpclient.execute(httppost);           
        HttpEntity entity = response.getEntity();

        in
        String line = null;
        while ((line = reader.readLine()) != null)
        {
            sb.append(line + "\n");
        }
        result = sb.toString();
    } catch (Exception e) { 


    storeJSONVariables(result);

}

public void 
            try {
                JSONObject oneObject = jArrayCurrent.getJSONObject(i);
                JSONArray jArrayWeatherDesc =   jObject.getJSONArray("weatherDesc");
                String temp_C = oneObject.getString("temp_C");
                String windspeedKmph = oneObject.getString("windspeedKmph");

                   // Pulling items from the array
                        String weatherDesc = twoObject.getString("value");  

                        WD(weatherDesc);

                    } catch (JSONException e) {

                    }


public void TCWSKMPH(String temp_C, String windspeedKmph) {

    if(temp_C != null || windspeedKmph != null) {
         RwindspeedKmph.nextInt(max - min + 1) + min;

        temp_C = getString(a);
        windspeedKmph = getString(b);
    }

    TextView temptext = (TextView)findViewById(R.id.temperature);
    TextView sowtext = (TextView)findViewById(R.id.windspeed);
    temptext.setText("TEMPERATURE: " + temp_C);
    sowtext.setText("WINDSPEED: " + windspeedKmph);
}

public void WD(String weatherDesc) {
    String descriptionofweather = weatherDesc;

LogCat:

03-01 11:57:40.760: E/AndroidRuntime(2471):     at com.example.thelexapp.MainActivity.TCWSKMPH(MainActivity.java:190)
03-01 11:57:40.760: E/AndroidRuntime(2471):     at com.example.thelexapp.MainActivity.getURLforweather(MainActivity.java:92)
03-01 11:57:40.760: E/AndroidRuntime(2471):     at com.example.thelexapp.MainActivity$1.onClick(MainActivity.java:42)
Was it helpful?

Solution

With max=0 and min=20, int a and/or int b becomes a negative number-> n<=0 in the LogCat.

Also,
1. In emulator, you will have to pass location lat-long values. Through either Eclipse-DDMS or pass values directly in code (for the purpose of testing the rest of your code.)
for-"..cause i don't know how to use GPS in emulator, so it returns null"
2. In your current code, in the else part of if(location!=null),

final TextView textViewToChange = (TextView) findViewById(R.id.textView1);  
textViewToChange.setText("Unable to get weather information.");  
TextView text = (TextView) findViewById(R.id.textView1);  

Same ...id.textView1 for both- text and textViewToChange

To convert int to String:
String a = String.valueOf(int value here);

check: http://www.tutorialspoint.com/java/java_string_valueof.htm

OTHER TIPS

The LogCat shows that there's an error here (line 173 of MainActivity.java):

Caused by: java.lang.IllegalArgumentException: n <= 0: -19
03-01 11:46:33.080: E/AndroidRuntime(2369):     at java.util.Random.nextInt(Random.java:175)
03-01 11:46:33.080: E/AndroidRuntime(2369):     at com.example.thelexapp.MainActivity.TCWSKMPH(MainActivity.java:173)

In particular the input parameter of the method "nextInt" is negative

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top