我正在制作一个 android 天气应用程序,它从第三方 API 获取数据并向用户呈现 5 天的天气数据。每当我尝试运行我的应用程序时,它都会崩溃。任何帮助,将不胜感激。
主要活动-
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];
private String[] pictureLink = new String[5];
private Bitmap[] imageCases = new Bitmap[5];
private ImageView iv1, iv2, iv3, iv4, iv5;
/** 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);
iv1 = (ImageView) findViewById(R.id.iv1);
iv2 = (ImageView) findViewById(R.id.iv2);
iv3 = (ImageView) findViewById(R.id.iv3);
iv4 = (ImageView) findViewById(R.id.iv4);
iv5 = (ImageView) findViewById(R.id.iv5);
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<2;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();
}
for(int i=0;i<5;i++){
imageCases[i] = new Image().execute(pictureLink[i]).get();
}
iv1.setImageBitmap(imageCases[0]);
iv2.setImageBitmap(imageCases[1]);
iv3.setImageBitmap(imageCases[2]);
iv4.setImageBitmap(imageCases[3]);
iv5.setImageBitmap(imageCases[5]);
Toast.makeText(getBaseContext(), string, Toast.LENGTH_SHORT).show();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@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();
}}
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.action_settings:
Intent aboutIntent = new Intent(MainActivity.this, Options.class);
startActivity(aboutIntent);
return true;
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@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"?>
<ScrollView
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="20dip"
android:orientation="vertical" >
<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>
<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>
<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>
<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>
<ImageView
android:id="@+id/iv1"
android:layout_width="100dp"
android:layout_height="100dp"
android:contentDescription="Image for climate"
android:maxHeight="100dp"
android:maxWidth="100dp" />
<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>
<ImageView
android:id="@+id/iv2"
android:layout_width="100dp"
android:layout_height="100dp"
android:contentDescription="Image for climate"
android:maxHeight="100dp"
android:maxWidth="100dp" />
<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>
<ImageView
android:id="@+id/iv3"
android:layout_width="100dp"
android:layout_height="100dp"
android:contentDescription="Image for climate"
android:maxHeight="100dp"
android:maxWidth="100dp" />
<TextView
android:id="@+id/day3"
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/d4"
android:textSize="20sp" >
</TextView>
<ImageView
android:id="@+id/iv4"
android:layout_width="100dp"
android:layout_height="100dp"
android:contentDescription="Image for climate"
android:maxHeight="100dp"
android:maxWidth="100dp" />
<TextView
android:id="@+id/day4"
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/d5"
android:textSize="20sp" >
</TextView>
<ImageView
android:id="@+id/iv5"
android:layout_width="100dp"
android:layout_height="100dp"
android:contentDescription="Image for climate"
android:maxHeight="100dp"
android:maxWidth="100dp" />
<TextView
android:id="@+id/day5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="yyyyyyyyy"
android:textSize="20sp" >
</TextView>
</LinearLayout>
</ScrollView>
LogCat 输出
10-17 22:26:33.834: D/ActivityThread(29954): setTargetHeapUtilization:0.25
10-17 22:26:33.834: D/ActivityThread(29954): setTargetHeapIdealFree:8388608
10-17 22:26:33.834: D/ActivityThread(29954): setTargetHeapConcurrentStart:2097152
10-17 22:26:34.014: I/System.out(29954): Provider network has been selected.
10-17 22:26:34.705: W/dalvikvm(29954): threadid=1: thread exiting with uncaught exception (group=0x41e90438)
10-17 22:26:34.705: E/AndroidRuntime(29954): FATAL EXCEPTION: main
10-17 22:26:34.705: E/AndroidRuntime(29954): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.checkweather/com.example.checkweather.MainActivity}: java.lang.NullPointerException
10-17 22:26:34.705: E/AndroidRuntime(29954): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
10-17 22:26:34.705: E/AndroidRuntime(29954): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
10-17 22:26:34.705: E/AndroidRuntime(29954): at android.app.ActivityThread.access$700(ActivityThread.java:143)
10-17 22:26:34.705: E/AndroidRuntime(29954): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
10-17 22:26:34.705: E/AndroidRuntime(29954): at android.os.Handler.dispatchMessage(Handler.java:99)
10-17 22:26:34.705: E/AndroidRuntime(29954): at android.os.Looper.loop(Looper.java:137)
10-17 22:26:34.705: E/AndroidRuntime(29954): at android.app.ActivityThread.main(ActivityThread.java:4950)
10-17 22:26:34.705: E/AndroidRuntime(29954): at java.lang.reflect.Method.invokeNative(Native Method)
10-17 22:26:34.705: E/AndroidRuntime(29954): at java.lang.reflect.Method.invoke(Method.java:511)
10-17 22:26:34.705: E/AndroidRuntime(29954): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
10-17 22:26:34.705: E/AndroidRuntime(29954): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
10-17 22:26:34.705: E/AndroidRuntime(29954): at dalvik.system.NativeStart.main(Native Method)
10-17 22:26:34.705: E/AndroidRuntime(29954): Caused by: java.lang.NullPointerException
10-17 22:26:34.705: E/AndroidRuntime(29954): at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
10-17 22:26:34.705: E/AndroidRuntime(29954): at org.json.JSONTokener.nextValue(JSONTokener.java:94)
10-17 22:26:34.705: E/AndroidRuntime(29954): at org.json.JSONObject.<init>(JSONObject.java:154)
10-17 22:26:34.705: E/AndroidRuntime(29954): at org.json.JSONObject.<init>(JSONObject.java:171)
10-17 22:26:34.705: E/AndroidRuntime(29954): at com.example.checkweather.MainActivity.onCreate(MainActivity.java:103)
10-17 22:26:34.705: E/AndroidRuntime(29954): at android.app.Activity.performCreate(Activity.java:5179)
10-17 22:26:34.705: E/AndroidRuntime(29954): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
10-17 22:26:34.705: E/AndroidRuntime(29954): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
10-17 22:26:34.705: E/AndroidRuntime(29954): ... 11 more