2014-01-16 81 views
0

我得到天氣信息在我的應用程序,把它的工作。現在,我得到一個空指針異常,我不知道爲什麼,特別是因爲它在工作,我沒有改變任何這段代碼。Android應用程序崩潰與NullPointerException異常

package com.kentuckyfarmbureau.kyfb; 

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.location.Location; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.view.KeyEvent; 
import android.view.View; 
import android.view.inputmethod.EditorInfo; 
import android.view.inputmethod.InputMethodManager; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.TextView.OnEditorActionListener; 

public class WeatherLocation extends Activity 
{ 
    EditText locationText; 
    TextView label; 
    Button getWeather; 
    String enteredText; 
    String url = "http://api.worldweatheronline.com/free/v1/weather.ashx?q=%s&format=json&num_of_days=5&key=37a5fj42xpyptvjgkhrx5rwu"; 
    String newURL; 
    String currentLocationText; 
    LocationManager lm; 
    Location location; 
    double longitude; 
    double latitude; 
    String longString; 
    String latString; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.weatherlocation); 

     locationText = (EditText) findViewById(R.id.locationTextView); 
     label = (TextView) findViewById(R.id.label); 
     getWeather = (Button) findViewById(R.id.showWeather); 

     locationText.setText("Current Location"); 

     lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
     location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
     longitude = location.getLongitude(); 
     latitude = location.getLatitude(); 
     longString = String.valueOf(longitude); 
     latString = String.valueOf(latitude); 
     currentLocationText = (latString + "+" + longString); 
     enteredText = currentLocationText; 

     newURL = String.format(url, enteredText); 

     locationText.setOnEditorActionListener(new OnEditorActionListener() 
     { 
      @Override 
      public boolean onEditorAction(TextView v, int actionId, KeyEvent event) 
      { 
       boolean handled = false; 
       if (actionId == EditorInfo.IME_ACTION_DONE) 
       { 
        if(locationText.getText().toString().equals("Current Location")) 
        { 
         lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
         location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
         longitude = location.getLongitude(); 
         latitude = location.getLatitude(); 
         longString = String.valueOf(longitude); 
         latString = String.valueOf(latitude); 
         currentLocationText = (latString + "+" + longString); 
         enteredText = currentLocationText; 
        } 
        else 
        { 
         enteredText = locationText.getText().toString(); 
         enteredText = enteredText.replaceAll(" ", "+"); 
        } 
        System.out.println(enteredText); 

        // hide the virtual keyboard 
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
        imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 
               InputMethodManager.RESULT_UNCHANGED_SHOWN); 

        newURL = String.format(url, enteredText); 
        System.out.println("Formatted URL: " + newURL); 
        handled = true; 
       } 

       return handled; 
      } 
     }); 

     // Get Weather button 
     getWeather.setOnClickListener(new View.OnClickListener() 
     { 
      public void onClick(View v) 
      { 
       Intent weather = new Intent(WeatherLocation.this, Weather.class); 
       weather.putExtra("INTENT_KEY_URL", newURL); 
       weather.putExtra("CURRENT_LOCATION", locationText.getText().toString()); 
       startActivity(weather); 
      } 
     }); 
    } 
} 

這個問題似乎是48行,longitude = location.getLongitude();

+4

然後'getLastKnownLocation'返回'null'。如果提供者被禁用或最近未被使用,則返回'null'。 –

回答

1

如果線48引起的問題,那麼很有可能你的 位置是空的。如果在android文檔中提到的禁用提供程序的情況下調用getLastKnownLocation(),則這可能爲null。

0

我通過添加位置監聽器固定這一點。

final LocationListener locationListener = new LocationListener() 
    { 


    @Override 
      public void onLocationChanged(Location currentLocation) 
      { 
       latitude = currentLocation.getLatitude(); 
       longitude = currentLocation.getLongitude(); 
      } 
      public void onProviderDisabled(String provider) 
      { 

      } 
      public void onProviderEnabled(String provider) 
      { 

      } 
      public void onStatusChanged(String provider, int status, Bundle extras) 
      { 

      } 
     }; 

並添加:

lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100, 1, locationListener); 
相關問題