Warning: file_put_contents(/datas/wwwroot/jiajiahui/core/caches/caches_template/2/default/show.php): failed to open stream: Permission denied in /datas/wwwroot/jiajiahui/core/libraries/classes/template_cache.class.php on line 55

Warning: chmod(): Operation not permitted in /datas/wwwroot/jiajiahui/core/libraries/classes/template_cache.class.php on line 56
Android应用程序崩溃与NullPointerException异常 - 源码之家

Android应用程序崩溃与NullPointerException异常

问题描述:

我得到天气信息在我的应用程序,把它的工作。现在,我得到一个空指针异常,我不知道为什么,特别是因为它在工作,我没有改变任何这段代码。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'。 –

如果线48引起的问题,那么很有可能你的 位置是空的。如果在android文档中提到的禁用提供程序的情况下调用getLastKnownLocation(),则这可能为null。

我通过添加位置监听器固定这一点。

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);