已解决非常感谢每个人
Helle,首先感谢点击我的问题。我已经制作了一个应用程序获取天气的json文件。然后它解析这个JSON文件以获取温度等天气变量。它在textView中显示这些变量。问题是,当我按下按钮(在我的模拟器上)时,它显示“不幸,(应用程序名称)已停止工作”。您可以在下面找到我用来制作所有这些的代码。帮助将不胜感激。如果您需要更多文件,例如activity_main.xml,strings.xml,...只需询问。
感谢您帮助感谢每个人提供帮助,我认为还有3个错误要解决,但我不理解这些错误日志,我只是开始编码,谢谢你们每个人的帮助(你们)可以在帖子的底部找到他们
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;
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)
答案 0 :(得分:0)
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”的输入参数为负
答案 1 :(得分:0)
max=0
和min=20
,int a
和/或int b
成为负数 - &gt; LogCat中的n<=0
。
另外,
1.在模拟器中,您必须传递位置lat-long值。通过Eclipse-DDMS或直接在代码中传递值(用于测试其余代码。)
for - “..因为我不知道如何在模拟器中使用GPS,所以它返回null”
2.在您当前的代码中,在else
的{{1}}部分,
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);
- ...id.textView1
和text
将int转换为String:
String a = String.valueOf(此处为int值);
检查:http://www.tutorialspoint.com/java/java_string_valueof.htm