我正在尝试制作一个简单的天气 android 应用程序。由于我是 android 新手,所以我在很多地方都面临着问题。而这一次我想我搞砸了我的主要活动和表现。我在这里粘贴整个代码。请在我出错的地方提供宝贵的建议。
主要活动
package com.example.checkweather;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.ExecutionException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements LocationListener {
private TextView latituteField;
private TextView longitudeField;
private LocationManager locationManager;
private TextView mylocation;
private TextView Entry1, Entry2, Entry3, Entry4, Entry5;
private JSONObject jobject1, jobject2, jarray, tempjobject;
private JSONArray temparray;
private String provider, string, city;
private String[] maximum = new String[5];
private String[] minimum = new String[5];
private String[] conditions = new String[5];
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
latituteField = (TextView) findViewById(R.id.TextView02);
longitudeField = (TextView) findViewById(R.id.TextView04);
mylocation = (TextView) findViewById(R.id.TextView06);
Entry1 = (TextView) findViewById(R.id.day1);
Entry2 = (TextView) findViewById(R.id.day2);
Entry3 = (TextView) findViewById(R.id.day3);
Entry4 = (TextView) findViewById(R.id.day4);
Entry5 = (TextView) findViewById(R.id.day5);
// Get the location manager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the locatioin provider -> use
// default
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
// Initialize the location fields
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
onLocationChanged(location);
} else {
latituteField.setText("Location not available");
longitudeField.setText("Location not available");
}
String result;
try {
result = new WeatherHttpClient().execute("http://api.wunderground.com/api/796da11422ba3dc2/forecast10day/q/"+city+".json").get();
try {
jobject1 = new JSONObject(result);
jarray = jobject1.getJSONObject("forecast");
jobject2 = jarray.getJSONObject("simpleforecast");
temparray = jobject2.getJSONArray("forecastday");
string = result;
for(int i=0;i<5;i++){
tempjobject= temparray.getJSONObject(i);
maximum[i] =tempjobject.getString("high");
minimum[i] = tempjobject.getString("low");
conditions[i] = tempjobject.getString("conditions");
}
JSONObject j1 = new JSONObject(maximum[0]);
JSONObject j2 = new JSONObject(minimum[0]);
JSONObject j3 = new JSONObject(maximum[1]);
JSONObject j4 = new JSONObject(minimum[1]);
JSONObject j5 = new JSONObject(maximum[2]);
JSONObject j6 = new JSONObject(minimum[2]);
JSONObject j7 = new JSONObject(maximum[3]);
JSONObject j8 = new JSONObject(minimum[3]);
JSONObject j9 = new JSONObject(maximum[4]);
JSONObject j10 = new JSONObject(minimum[4]);
Entry1.setText("Maximum" + j1.getString("fahrenheit")+"°F" + "Minimum" + j2.getString("fahrenheit")+"°F "+ conditions[0]);
Entry2.setText("Maximum" + j3.getString("fahrenheit")+"°F" + "Minimum" + j4.getString("fahrenheit")+"°F "+ conditions[1]);
Entry3.setText("Maximum" + j5.getString("fahrenheit")+"°F" + "Minimum" + j6.getString("fahrenheit")+"°F "+ conditions[2]);
Entry4.setText("Maximum" + j7.getString("fahrenheit")+"°F" + "Minimum" + j8.getString("fahrenheit")+"°F "+ conditions[3]);
Entry5.setText("Maximum" + j9.getString("fahrenheit")+"°F" + "Minimum" + j10.getString("fahrenheit")+"°F "+ conditions[4]);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/* Request updates at startup */
@Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(provider, 400, 1, this);
}
/* Remove the locationlistener updates when Activity is paused */
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
@Override
public void onLocationChanged(Location location) {
int lat = (int) (location.getLatitude());
int lng = (int) (location.getLongitude());
latituteField.setText(String.valueOf(lat));
longitudeField.setText(String.valueOf(lng));
Geocoder geoCoder1 = new Geocoder(getBaseContext(), Locale.getDefault());
try {
String add = "";
List<Address> addresses;
List<Address> fromLocation;
fromLocation = (List<Address>) geoCoder1.getFromLocation(lat,lng, 1);
addresses = fromLocation;
if (addresses.size() > 0)
{
for (int i=0; i<((android.location.Address) addresses.get(0)).getMaxAddressLineIndex();i++)
add += ((android.location.Address) addresses.get(0)).getAddressLine(i) + "\n";
}
mylocation.setText(String.valueOf(add));
Bundle extras = getIntent().getExtras();
if (extras != null) {
city = extras.getString("ZipCode");
}
else{
city = ((android.location.Address) addresses.get(0)).getPostalCode();
}
Toast.makeText(getBaseContext(), city, Toast.LENGTH_SHORT).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(this, "Enabled new provider " + provider,
Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(this, "Disabled provider " + provider,
Toast.LENGTH_SHORT).show();
}
}
XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dip"
android:orientation="horizontal" >
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginRight="5dip"
android:text="@string/Lati"
android:textSize="20sp" >
</TextView>
<TextView
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp" >
</TextView>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginRight="5dip"
android:text="@string/Longi"
android:textSize="20sp" >
</TextView>
<TextView
android:id="@+id/TextView04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp" >
</TextView>
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout3"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/TextView05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginRight="5dip"
android:text="@string/Locati"
android:textSize="20sp" >
</TextView>
<TextView
android:id="@+id/TextView06"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp" >
</TextView>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginRight="5dip"
android:paddingTop="15dip"
android:text="@string/d1"
android:textSize="20sp" >
</TextView>
<TextView
android:id="@+id/day1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="xxxxxxx"
android:textSize="20sp" >
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginRight="5dip"
android:paddingTop="15dip"
android:text="@string/d2"
android:textSize="20sp" >
</TextView>
<TextView
android:id="@+id/day2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="yyyyyyyyy"
android:textSize="20sp" >
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginRight="5dip"
android:paddingTop="15dip"
android:text="@string/d3"
android:textSize="20sp" >
</TextView>
<TextView
android:id="@+id/day3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="zzzzzzz"
android:textSize="20sp" >
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginRight="5dip"
android:paddingTop="15dip"
android:text="@string/d4"
android:textSize="20sp" >
</TextView>
<TextView
android:id="@+id/day4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="zzzzzsdfdszz"
android:textSize="20sp" >
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginRight="5dip"
android:paddingTop="15dip"
android:text="@string/d5"
android:textSize="20sp" >
</TextView>
<TextView
android:id="@+id/day5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fdsadfsdf"
android:textSize="20sp" >
</TextView>
</LinearLayout>
原木猫:
10-17 20:02:20.235: D/ActivityThread(17009): setTargetHeapUtilization:0.25
10-17 20:02:20.235: D/ActivityThread(17009): setTargetHeapIdealFree:8388608
10-17 20:02:20.245: D/ActivityThread(17009): setTargetHeapConcurrentStart:2097152
10-17 20:02:20.395: I/System.out(17009): Provider network has been selected.
10-17 20:02:20.745: W/dalvikvm(17009): threadid=1: thread exiting with uncaught exception (group=0x41e90438)
10-17 20:02:20.745: E/AndroidRuntime(17009): FATAL EXCEPTION: main
10-17 20:02:20.745: E/AndroidRuntime(17009): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.checkweather/com.example.checkweather.MainActivity}: java.lang.NullPointerException
10-17 20:02:20.745: E/AndroidRuntime(17009): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
10-17 20:02:20.745: E/AndroidRuntime(17009): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
10-17 20:02:20.745: E/AndroidRuntime(17009): at android.app.ActivityThread.access$700(ActivityThread.java:143)
10-17 20:02:20.745: E/AndroidRuntime(17009): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
10-17 20:02:20.745: E/AndroidRuntime(17009): at android.os.Handler.dispatchMessage(Handler.java:99)
10-17 20:02:20.745: E/AndroidRuntime(17009): at android.os.Looper.loop(Looper.java:137)
10-17 20:02:20.745: E/AndroidRuntime(17009): at android.app.ActivityThread.main(ActivityThread.java:4950)
10-17 20:02:20.745: E/AndroidRuntime(17009): at java.lang.reflect.Method.invokeNative(Native Method)
10-17 20:02:20.745: E/AndroidRuntime(17009): at java.lang.reflect.Method.invoke(Method.java:511)
10-17 20:02:20.745: E/AndroidRuntime(17009): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
10-17 20:02:20.745: E/AndroidRuntime(17009): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
10-17 20:02:20.745: E/AndroidRuntime(17009): at dalvik.system.NativeStart.main(Native Method)
10-17 20:02:20.745: E/AndroidRuntime(17009): Caused by: java.lang.NullPointerException
10-17 20:02:20.745: E/AndroidRuntime(17009): at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
10-17 20:02:20.745: E/AndroidRuntime(17009): at org.json.JSONTokener.nextValue(JSONTokener.java:94)
10-17 20:02:20.745: E/AndroidRuntime(17009): at org.json.JSONObject.<init>(JSONObject.java:154)
10-17 20:02:20.745: E/AndroidRuntime(17009): at org.json.JSONObject.<init>(JSONObject.java:171)
10-17 20:02:20.745: E/AndroidRuntime(17009): at com.example.checkweather.MainActivity.onCreate(MainActivity.java:94)
10-17 20:02:20.745: E/AndroidRuntime(17009): at android.app.Activity.performCreate(Activity.java:5179)
10-17 20:02:20.745: E/AndroidRuntime(17009): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
10-17 20:02:20.745: E/AndroidRuntime(17009): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
10-17 20:02:20.745: E/AndroidRuntime(17009): ... 11 more