2016-05-03 9 views
0

ヘルプ!これは簡単なUdacityコースの外で私の最初のアプリです、優しくしてください。クラッシュする私はonLocationChangedと思われるが、バグが何であるか分からない

私は正確に一度、GPSをしたいです。私のアプリのユーザーはgpsのリフレッシュは必要ありません。それにもかかわらず、私はonLocationChangedリスナーが正確なGPS読み取りを得るために必要であると信じています。

それはクラッシュします。 MainActivityでは、MyLocationListenerクラスを呼び出した後に2番目のToastをヒットできます。これはMainActivityのgetGPSにバグがあることを示唆しています。しかし、私はどこに見えません。私はリスナーがMainActivityをGPSの検索中も続けることを許していると思いますか?自分の携帯電話上の

GPSは、私はテスターのアプリをダウンロードして取り組んでいます。私は外見を得て、私のY-前部に家を残した。ここで

はcrashalyticsからのスタックトレースです。

http://crashes.to/s/54d65de993b

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference 
    at android.widget.Toast.<init>(Toast.java:101) 
    at android.widget.Toast.makeText(Toast.java:258) 
    at com.example.android.carfinder.MyLocationListener.onLocationChanged(MyLocationListener.java:28) 
    at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:281) 
    at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:210) 
    at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:226) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5254) 
    at java.lang.reflect.Method.invoke(Method.java) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

ので質問、

1. what's causing the crash? 
    2. When I've got a gps, how do I turn the listener off, till the button is re-pressed? 
    3. Why doesn't progressDialog show while it searches for gps? 

MainActivity、getGPSはonClickイベントでアクティブにします

public void getGPS(View view) { 
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    getSystemService(Context.LOCATION_SERVICE); 

    if(displayGpsStatus() == false) { 
     Log.i("getGPS", "GPS turned off"); 
     return; 
    } 
    // GPS is working 
    //progressBar.setVisibility(View.VISIBLE); 
    ProgressDialog progressDialog = ProgressDialog.show(this, "Getting GPS lock", "Not much longer love"); 

    Log.i("getGPS", "trying for new co-ords"); 
    LocationListener locationListener = new MyLocationListener(); 
    try { 
     Toast.makeText(this, "activating locationListener", Toast.LENGTH_SHORT).show(); 
     locationManager.requestLocationUpdates(
       LocationManager.GPS_PROVIDER, 0, 0, locationListener); //5000, 10 
     Log.i("getGPS", "just returned from MyLocationListener Class"); 
    } 
    catch (SecurityException e) { 
     Log.i("getGPS", "no permission to use GPS"); 
     progressBar.setVisibility(View.GONE); 
     Toast.makeText(this, "Did you block GPS permission on install?", Toast.LENGTH_LONG).show(); 
     progressDialog.dismiss(); 
     return; 
    } 
    Toast.makeText(this, "continuing, is address and time updated?", Toast.LENGTH_LONG).show(); 
    // Got GPS so hide the progress bar 
    //progressBar.setVisibility(View.GONE); 
    progressDialog.dismiss(); 

    // update the date and address views 
    //saveDateTime(); 
    //updateAddressView(); 
    return; 
} 

MyLocationListener:

public class MyLocationListener extends MainActivity implements LocationListener { 
    String TAG = "MyLocationListener Class"; 

@Override 
public void onLocationChanged(Location loc) { 
    Log.i("onLocationChanged", "in outside class"); 

    Toast.makeText(
      getBaseContext(), 
      "Location changed: Lat: " + loc.getLatitude() + " Lng: " 
        + loc.getLongitude(), Toast.LENGTH_SHORT).show(); 
    String longitude = "Longitude: " + loc.getLongitude(); 
    Log.i(TAG, longitude); 
    String latitude = "Latitude: " + loc.getLatitude(); 
    Log.i(TAG, latitude); 
    // TODO need to check latitude and longitude accessible from MainActivity 

    /*------- To get city name from coordinates -------- */ 
    // from here we're reverse geo-coding to get the address 
    String address = null; 
    String city = null; 
    Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault()); 
    List<Address> addresses; 
    try { 
     addresses = gcd.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1); 
     if (addresses.size() > 0) { 
      //System.out.println(addresses.get(0).getLocality()); 
      address = addresses.get(0).getAddressLine(0); 
      city = addresses.get(0).getLocality(); 
     } 
    } 
    catch (IOException e) { 
     e.printStackTrace(); 
     Log.i("MyLocationListener", "No address found"); 
    } 
    String savedAddress = address + ", " + city; 
    if(address == null && city == null) { 
     savedAddress = "couldn't fathom address, tap view on map to check"; 
    } 
    TextView addressTextView = (TextView) findViewById(R.id.approxAddress); 
    addressTextView.setText(savedAddress); 
    // save it to permanent memory 
    SharedPreferences.Editor mEditor = mPrefs.edit(); 
    mEditor.putString("savedAddress", savedAddress).apply(); 
    mEditor.putString("savedLongitude", longitude).apply(); 
    mEditor.putString("savedLatitude", latitude).apply(); 

    Log.i("MyLocationListener", "SAVED address, gps: "); 
} 

@Override 
public void onProviderDisabled(String provider) { 
    Toast.makeText(getBaseContext(), "MyLocListener: GPS disabled DOH!", Toast.LENGTH_LONG).show(); 
} 

@Override 
public void onProviderEnabled(String provider) { 
    Toast.makeText(getBaseContext(), "MyLocListener: GPS enabled :)", Toast.LENGTH_LONG).show(); 
} 

@Override 
public void onStatusChanged(String provider, int status, Bundle extras) { 
    Toast.makeText(getBaseContext(), "MyLocListener: GPS status changed?", Toast.LENGTH_LONG).show(); 
} 
} 
+1

クラッシュログとはなんですか? – Nathanael

+0

progressBarの初期化はどこですか? – Opiatefuchs

+0

クラッシュログはどのように入手できますか?私のPCは家を出ることができないので、私の電話は私のデバッガに接続できません。 progressBarをonCreateで初期化しました。私はそれをProgressDialogに変更しました。なぜなら、これはxmlをつぶすことを避けるため、ちょうど上に浮かぶからです。しかし、それは表示されません、grrr。 –

答えて

1

エラーがToast.makeTextへの呼び出しから来ています。私の推測では、アクティビティが作成される前にToast(とそれを使ってgetBaseContext)を使用しようとしているということです。その時点でコンテキストはまだ存在しません。

Log.i呼び出しによって、あなたのToast.makeText通話を交換してみてください。それがエラーを取り除き、トーストが必要な場合は、Toast.makeTextを呼び出す前にコンテキストがヌルでないことを確認してください。

+1

正しいコンテキストを使用していることを確認するために、アプリケーションベースのコンテキストをパラメータで渡すことができるMyLocationListenerのコンストラクタを作成することをお勧めします。 –

+0

ねえ、これは素晴らしい提案です。はい、間違いなくgetBaseContext()です。すべてのトーストを削除しましたが、ジオコーダーオブジェクトを作成するときにgetBaseContextを1回コールしました。 "Geocoder gcd =新しいジオコーダー(this.getBaseContext()、Locale.getDefault());"コンストラクタにコンテキストを渡してみましたが、MyLocationListenerオブジェクトを作成しようとすると、「新しいMyLocationListener(this);」が嫌いですまたは "getApplicationContext()"のような任意のバリエーションです。 Grrrr。私はもっ​​と学ぶ必要があると思う。ご協力いただきありがとうございます :) –

関連する問題