【问题标题】:Weather app crashes on emulator天气应用程序在模拟器上崩溃
【发布时间】:2014-04-02 17:46:37
【问题描述】:

已解决非常感谢大家

Helle,首先感谢您点击我的问题。我制作了一个应用程序来获取天气的 json 文件。然后它解析这个 JSON 文件以获取天气变量,如温度等。它在 textView 中显示这些变量。问题是当我按下按钮(在我的模拟器上)时,它显示“不幸的是,(应用程序名称)已停止工作”。下面你可以找到我用来做这一切的代码。帮助将不胜感激。如果您需要更多文件,如 activity_main.xml、strings.xml 等,请询问。

感谢您的帮助感谢大家的帮助,我认为还有 3 个错误需要解决,但我不明白这些错误日志,我刚刚开始编码,谢谢大家的帮助(您可以在帖子底部找到他们

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)

【问题讨论】:

  • 你在哪里调用了这个 getPositionAndGetWheater() 方法?并发布您的 logcat 以便更好地理解
  • 在应用崩溃时查看堆栈跟踪以了解导致问题的原因会很有帮助。
  • 嗯,从来没有注意到我无处使用它,但是如果我按下按钮,它会运行 getweather(我认为)导致文本更改为“无法获取天气信息”(因为我没有不知道如何在模拟器中使用 GPS,所以返回 null)
  • @user3357521 尝试进入真实设备,但您的​​ Button onclick(...) 事件在哪里?你是一个真正的火箭程序员
  • 我没有真正的设备,我正在为朋友制作这个应用程序,我有一部 iphone:s

标签: android xml json parsing weather


【解决方案1】:

使用max=0min=20int a 和/或int b 变为负数-> LogCat 中的n<=0

另外,
1. 在模拟器中,您必须传递位置经纬度值。通过 Eclipse-DDMS 或直接在代码中传递值(用于测试其余代码。)
for-"..因为我不知道如何在模拟器中使用 GPS,所以它返回 null"
2.在你当前的代码中,在if(location!=null)else部分,

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

...id.textView1 相同,texttextViewToChange

将 int 转换为字符串:
String a = String.valueOf(这里是int值);

检查:http://www.tutorialspoint.com/java/java_string_valueof.htm

【讨论】:

  • 谢谢我解决了除了数字 1 之外的所有问题,我不知道如何传递 lat 和 long 值,我什至不知道 Eclipse-DDMS 在哪里是的,我会用谷歌搜索它并希望最好
  • 谢谢,我认为我传入了 long 和 lat 值,但随机 int 部分仍然存在一些错误,我将 max 更改为 20 并将 min 更改为 0 ,但我认为它在转换时遇到了麻烦int 到一个字符串...你知道任何解决方案吗?谢谢你的帮助;)
  • 我会接受你的答案,因为否则人们可能会认为它已经解决了:)
  • 所有错误都已解决,但它仍然需要随机值,而且我确定我在 Eclipse DDMS 中传入了 long 和 lat 值:s
【解决方案2】:

LogCat 显示这里有错误(MainActivity.java 的第 173 行):

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)

特别是“nextInt”方法的输入参数为负数

【讨论】:

    猜你喜欢
    • 2017-07-22
    • 2015-09-28
    • 2023-03-12
    • 2011-03-20
    • 2016-10-12
    • 2015-11-14
    • 2011-12-26
    • 1970-01-01
    • 2013-10-15
    相关资源
    最近更新 更多