2012-02-11 1 views
0

プログラムでLocation location.getLatitude()/ Location location.getLongitudeメソッドを使用できるようにしたい場合は、上記のaとbの値を返します私は、aとbを、getLongitudeとgetLatitudeが提供するdouble値を返すメソッドか、実際のget関数自体のどちらかで置き換えると、エラーが出ます。Google Places APIを使用すると、LocationクラスはNull Pointer Exceptionをスローします

public void onClick(View v) { 

    requesturl = "https://maps.googleapis.com/maps/api/place/search/json?" + 
    "location=" + a + "," + b + "&radius=6000&" + "types=bank&sensor=false&key=AIzaSyDXNlHRDZnWW0T0tvBUjpyA8k2K9sjS2cM";  

    HHand(requesturl); 


} 

aとbがgetLatitude()関数とgetLongitude()関数から値を取得できるようにするにはどうすればよいですか?コード

public class Freedom extends ActivityGroup implements LocationListener { 
/** Called when the activity is first created. 
* @return */ 

String keystring=""; 
String requesturl; 
LocationManager locman; 
Location location; 

//Awesome  
public String convertStreamToString(InputStream is) { 
    return new Scanner(is).useDelimiter("\\A").next(); 
} 



public void HHand(String a) { 


    //HTTP Request Processing for URL 
     HttpClient client=new DefaultHttpClient(); 
     StringBuilder builder=new StringBuilder(a); 
     builder.append(URLEncoder.encode(keystring)); 
     HttpPost post=new HttpPost(a); 

     try { 
      org.apache.http.HttpResponse response=client.execute(post); 
      HttpEntity entity=response.getEntity(); 


      if (entity != null) { 
       InputStream is = entity.getContent(); 
       String val = convertStreamToString(is); 
       Log.e("", val); 

       } 


      } 


      catch (ClientProtocolException e) { 
       e.printStackTrace(); 
      } 

      catch (IOException e) { 
       e.printStackTrace(); 
      } 


} 


@Override 
public void onCreate(Bundle savedInstanceState) { 


    super.onCreate(savedInstanceState);   
    setContentView(R.layout.main); 
    locman = (LocationManager) this.getSystemService(LOCATION_SERVICE); 



} 



protected void onResume() { 
    super.onResume(); 
    locman.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, this); 
    } 


public boolean onCreateOptionsMenu(Menu menu) { 
    super.onCreateOptionsMenu(menu); 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.btmmnu, menu); 
    return true; 
} 

protected Dialog onCreateDialog(int id) { 

     final Dialog dialog = new Dialog(this); 
     OnClickListener txtlstn = new OnClickListener() { 
      public void onClick(View v) { 
       dismissDialog(0); 
      } 
     }; 

     switch(id) { 
     case 0:       

      ... 
     } 
     return dialog; 
} 




public boolean onOptionsItemSelected(MenuItem item) { 

    switch (item.getItemId()) { 
    case R.id.About:     
     showDialog(0); 
     return true; 
    case R.id.Guidance: 

     final Button Travel = (Button) findViewById(R.id.travel); 
     Travel.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       requesturl = "https://maps.googleapis.com/maps/api/place/search/json?" + 
          "location=" + location.getLatitude() + "," + location.getLongitude() + "&radius=6000&" + 
          "types=car_rental&sensor=false&key=AIzaSyDXNlHRDZnWW0T0tvBUjpyA8k2K9sjS2cM"; 
         HHand(requesturl); 


      } 
     }); 

答えて

0

あなたはlocationがnullであることを示唆しているlocation.getLatitude()またはlocation.getLongitude()からNullPointerExceptionを、取得している場合:編集

locationはどこから来たのか、メソッド呼び出しがabの返り値であることがわかっているところですが、これが原因です。おそらくインスタンス変数をどこかに隠しているローカル変数がありますか?例:

public class Foo 
{ 
    private Location location; 

    public Foo(double latitude, double longitude) 
    { 
     // Careful - this declares a new *local* variable, so it's not 
     // changing the value of this.location 
     Location location = new Location(); 
     location.setLatitude(latitude); 
     location.setLongitude(longitude); 
     ... 
    } 

    public void bang() 
    { 
     // This will throw, as location is still null 
     double x = location.getLatitude(); 
    } 
} 
+0

私はそれを使用して解決しようとしました:場所の場所=新しい場所();新しいオブジェクトには、必要とは思わないパラメータが必要です。私のロケーションロケーションはグローバルで、メインクラスの冒頭に定義されています。 – user1204072

+0

@ user1204072:値はどこに割り当てられていますか?関連するコードを見ずにお手伝いするのは非常に難しいことです。 –

+0

私はいくつかのコードを追加しました – user1204072

関連する問題