2013-07-07 2 views
0

Я исследовал это исключение Nullpointer. После googling я обнаружил, что большинство из них связано с нулевыми ссылками на элементы пользовательского интерфейса. Но у меня ничего нет. Я просто не могу понять причину нулевого указателя. Я прямо протестировал его на моем Android-телефоне под управлением 2.3.7. И да, я добавил разрешения в файл манифеста.null Указатель исключения при изменении местоположения

package com.GodTM.projectshoppers; 

    import android.location.Criteria; 
    import android.location.Location; 
    import android.location.LocationListener; 
    import android.location.LocationManager; 
    import android.location.LocationProvider; 
    import android.os.Bundle; 
    import android.app.Activity; 
    import android.content.Context; 
    import android.content.Intent; 
    import android.view.Menu; 
    import android.view.View; 
    import android.view.View.OnClickListener; 
    import android.widget.ImageButton; 
    import android.widget.ImageView; 
    import android.widget.Toast; 

    public class HomeActivity extends Activity { 

    private ImageButton get; 
    private ImageView quit,settings; 
    private LocationManager locMan; 
    public LocationListener LocLis; 
    public Location currLoc; 
    private double[] passloc; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_home); 
     get=(ImageButton)findViewById(R.id.homeGetButton); 
     settings = (ImageView)findViewById(R.id.homeSettingsButton); 
     quit = (ImageView)findViewById(R.id.homeExitButton); 

     Criteria crit=new Criteria(); 
     locMan = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 

     locMan.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

     LocLis = new LocationListener() { 

      @Override 
      public void onStatusChanged(String provider, int status, Bundle  extras) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void onProviderEnabled(String provider) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void onProviderDisabled(String provider) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void onLocationChanged(Location location) { 
       // TODO Auto-generated method stub 
       if(location != null){ 
        currLoc.setLatitude(location.getLatitude()); 
        currLoc.setLongitude(location.getLongitude()); 
       } 
      } 
     }; 

     //Criteria crit = new Criteria(); 

     if(currLoc !=null){ 
      passloc[0] = currLoc.getLatitude(); 
      passloc[1] = currLoc.getLongitude(); 
      Toast.makeText(getApplicationContext(), "Lat:" + currLoc.getLatitude() + "\nLong:" + currLoc.getLongitude(), Toast.LENGTH_SHORT).show(); 
     } 
     else{ 
      Toast.makeText(getApplicationContext(), "Not Available", Toast.LENGTH_SHORT).show(); 
     } 


     get.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       if(currLoc!=null){ 
        Intent i = new Intent(HomeActivity.this, Screen2.class); 
        i.putExtra("Loc", passloc); 
        startActivity(i); 
       } 
       else{ 
        Toast.makeText(getApplicationContext(), "Location Not Available", Toast.LENGTH_SHORT).show(); 
       } 
      } 
     }); 

     settings.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
//    Intent j = new Intent(this, screen4.class); 
//    startActivity(j); 
      } 
     }); 

     quit.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       finish();    
      } 
     }); 
    } 

    @Override 
    public void onResume(){ 
     super.onResume(); 
     locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER , 0, 0, LocLis); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.home, menu); 
     return true; 
    } 

} 

ответ

3

currLoc никогда не инициализируется. Поскольку расположение и currLoc оба Location вы можете изменить

if(location != null){ 
    currLoc.setLatitude(location.getLatitude()); 
    currLoc.setLongitude(location.getLongitude()); 
} 

с

currLoc = location; 
0

Вы не инициализировать passLoc. И вы вызываете getLastKnownLocation без сохранения самого местоположения.

Смежные вопросы