2016-09-25 7 views
-2

こんにちは、私はAndroidプログラミングに興味があります。私はここでnull参照のエラーを取得し続けます。私はGoogle Map APIを使用し、azure webserviceを使用しています。私の質問が混乱していると私の文法もなぜそうですか? が私を助けてください、私は "butSearch" 方法 "TambahMap" ボタンをクリックして "SaringPeople" を実行しようとしています"java.lang.NullPointerException:仮想メソッドを呼び出そうとしています"というエラーが表示されます

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, LocationListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { 

private GoogleMap mMap; 
private double myLatitude, myLongitude; 
private String myUrl; 
private static Context context; 
private Button set; 
private EditText skillCari; 
private TextView latlng; 
private List<People> finalPeople = new ArrayList<>(); 
private List<People> myPeople = new ArrayList<>(); 
private double currentLatitude, currentLongitude; 
private GoogleApiClient mGoogleApiClient; 
private LocationRequest mLocationRequest; 
public Activity currentActivity = this; 
private double minLatitude, maxLatitude, minLongitude, maxLongitude; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_maps); 
    // Obtain the SupportMapFragment and get notified when the map is ready to be used. 
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 
    mapFragment.getMapAsync(this); 
    MapsActivity.context = getApplicationContext(); 

    mGoogleApiClient = new GoogleApiClient.Builder(this).addConnectionCallbacks(this).addOnConnectionFailedListener(this) 
      .addApi(LocationServices.API).build(); 

    mLocationRequest = LocationRequest.create().setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) 
      .setInterval(10 * 1000).setFastestInterval(1 * 1000); 
    latlng = (TextView) findViewById(R.id.Latlng); 
    RecyclerView myRecyle = (RecyclerView) findViewById(R.id.RecycleView); 
    myRecyle.setHasFixedSize(true); 
    myRecyle.setLayoutManager(new LinearLayoutManager(this)); 
    final AdapterCard myCard = new AdapterCard(); 
    myRecyle.setAdapter(myCard); 
    skillCari = (EditText) findViewById(R.id.skill_cari); 
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for ActivityCompat#requestPermissions for more details. 
     return; 
    } 
    final Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
    set = (Button) findViewById(R.id.butSearch); 
    set.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      myCard.clearData(); 
      BengService service = ServiceFactory.createRetrofitService(BengService.class, BengService.BENG_SERVICE); 
      service.getSkilledUser(skillCari.getText().toString()) 
        .subscribeOn(Schedulers.newThread()) 
        .observeOn(AndroidSchedulers.mainThread()) 
        .subscribe(new Subscriber<List<People>>() { 
         @Override 
         public void onCompleted() { 
         } 

         @Override 
         public void onError(Throwable e) { 
          Log.e("GithubDemo", e.getMessage()); 
          Log.e("GithubDemo", e.toString()); 
         } 

         @Override 
         public void onNext(List<People> peoples) { 
          finalPeople = SaringPeople(peoples, location.getLatitude(), location.getLongitude()); 
          myCard.addData(finalPeople); 
          TambahMap(mMap, finalPeople); 


         } 
        }); 
     } 
    }); 


} 

public ArrayList<People> SaringPeople(List<People> people, double userLatitude, double userLongitude) { 
    myLatitude = userLatitude; 
    myLongitude = userLongitude; 
    myPeople = people; 
    ArrayList<People> peopleSample = new ArrayList<>(); 
    double R = 6371; 
    double radius = 0.300; 
    double longitudeMin, longitudeMaks, latitudeMin, latitudeMaks; 
    longitudeMin = (userLongitude - Math.toDegrees(radius/R/Math.cos(Math.toRadians(userLatitude)))); 
    longitudeMaks = (userLongitude + Math.toDegrees(radius/R/Math.cos(Math.toRadians(userLatitude)))); 
    latitudeMaks = (userLatitude + Math.toDegrees(radius/R)); 
    latitudeMin = (userLatitude - Math.toDegrees(radius/R)); 
    for (People myPeople : people) { 
     if ((myPeople.getLatitude() <= latitudeMaks) && (myPeople.getLatitude() >= latitudeMin) && (myPeople.getLongitude() <= longitudeMaks) && (myPeople.getLongitude() >= longitudeMin)) { 
      peopleSample.add(myPeople); 
     } 
     else{} 
    } 
    return peopleSample; 

} 

public void TambahMap(GoogleMap googleMap, List<People> people) { 
    Bitmap urlImage; 
    for (People mypeople : people) { 
     urlImage = getBitmap(mypeople.getPhotoURL()); 
     LatLng haha = new LatLng(mypeople.getLatitude(), mypeople.getLongitude()); 
     googleMap.addMarker(new MarkerOptions().position(haha).title(mypeople.getFullName()).icon(BitmapDescriptorFactory.fromBitmap(urlImage))); 

    } 
} 

public Bitmap getBitmap(String imageUrl) { 
    try { 
     URL url = new URL(imageUrl); 
     HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
     connection.connect(); 
     InputStream input = connection.getInputStream(); 
     Bitmap bitmap = BitmapFactory.decodeStream(input); 
     return bitmap; 

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

    @Override 
public void onLocationChanged(Location location) { 
    currentLatitude = location.getLatitude(); 
    currentLongitude = location.getLongitude(); 
} 

@Override 
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 

} 

@Override 
public void onConnected(@Nullable Bundle bundle) { 
    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for ActivityCompat#requestPermissions for more details. 
     return; 
    } 
    Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
    if (location == null){ 
     LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
    } else { 
     currentLatitude = location.getLatitude(); 
     currentLongitude = location.getLongitude(); 
    } 
} 

@Override 
public void onConnectionSuspended(int i) { 

} 

..

イム私のログ

からこのエラーを取得
09-26 00:16:04.018 6337-6337/com.example.beng.bengtuak E/GithubDemo: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference 
09-26 00:16:04.018 6337-6337/com.example.beng.bengtuak E/GithubDemo: java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference 
09-26 00:16:16.386 6337-6337/com.example.beng.bengtuak E/GithubDemo: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference 
09-26 00:16:16.386 6337-6337/com.example.beng.bengtuak E/GithubDemo: java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference 
+0

申し訳ありませんが、スタックトレースがない場合、返信がありません –

+0

どうすればスタックトレースを使用できますか? ありがとう –

+0

あなたがAndroidスタジオを使用していると仮定すると、 "Android Monitor"と呼ばれるものがあるはずです.logcatというタブがあります。 –

答えて

0

googleclientから取得しているロケーションオブジェクトがnullであると表示されます

final Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 

その場所がnullので、位置情報の更新を要求した場合であればあなたがチェックする必要がありますgoogleclient

public void onConnected(@Nullable Bundle bundle) { 
     mLastLocation = LocationServices.FusedLocationApi .getLastLocation(mGoogleApiClient); 
    } 

のコールバックを追加します。

+0

おかげで私の質問@Adam に答えるために私が実装したhttp://blog.teamtreehouse.com/beginners-guide-location-androidステップバイステップガイドのURLを確認してくださいその方法が、まだ取得中にエラー 私は以前のスニペットコードでそれを示していません スニペットコードを編集して、すべてのアクティビティコードを表示できます ありがとう –

+0

nullpointerの詳細はこちらをご覧ください。 http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it –

関連する問題

 関連する問題