2011-07-31 61 views
0

可能重複:
Got force close on create Activity了強制關閉的創建活動,如果GPS關閉

朋友

請幫助我。

我已經在我的應用清單上設置了我的活動。

我的源代碼是:

package com.widy.ayopuasa; 

    import java.io.IOException; 
    import java.util.Calendar; 
    import java.util.List; 
    import java.util.Locale; 

    import com.widy.ayopuasa.service.FormatWaktu; 
    import com.widy.ayopuasa.service.IbadahService; 
    import com.widy.ayopuasa.service.PerhitunganShalat; 
    import com.widy.ayopuasa.service.Variabel; 

    import android.app.Activity; 
    import android.app.NotificationManager; 
    import android.content.Context; 
    import android.content.Intent; 
    import android.content.SharedPreferences; 
    import android.location.Address; 
    import android.location.Criteria; 
    import android.location.Geocoder; 
    import android.location.Location; 
    import android.location.LocationManager; 
    import android.os.Bundle; 
    import android.util.Log; 
    import android.widget.TextView; 

    public class JadwalShalat extends Activity { 

public static final String JADWAL = "jadwal"; 
public static final String JAM_SUBUH = "jamSubuh"; 
public static final String MENIT_SUBUH = "menitSubuh"; 

public static int NOTIF_ID = 100; 

private int hari; 
private int bulan; 
private int tahun; 
private double zonaWaktu; 
public String metode = ""; 

private float sudutSubuh; 
private float sudutIsya; 
private int mazhab; 
// private PerhitunganShalat shalat; 

LocationManager locationManager; 
IbadahService updateService = null; 
Location location; 
PerhitunganShalat shalat; 
FormatWaktu subuh; 
FormatWaktu dzuhur; 
FormatWaktu ashar; 
FormatWaktu maghrib; 
FormatWaktu isya; 

SharedPreferences sharedPreferences; 

@Override 
public void onCreate(Bundle bundle) { 
    super.onCreate(bundle); 
    setContentView(R.layout.jadwal_shalat); 

    Intent svc = new Intent(this, IbadahService.class); 
    stopService(svc); 

    NotificationManager mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

    mgr.cancel(NOTIF_ID); 

    // Mengambil text View di layout 
    TextView waktuSubuh = (TextView) findViewById(R.id.label_subuh_jam); 
    TextView waktuZuhur = (TextView) findViewById(R.id.label_zuhur_jam); 
    TextView waktuAshar = (TextView) findViewById(R.id.label_ashar_jam); 
    TextView waktuMaghrib = (TextView) findViewById(R.id.label_maghrib_jam); 
    TextView waktuIsya = (TextView) findViewById(R.id.label_isya_jam); 

    // inisialisasi tanggal sekarang 
    final Calendar c = Calendar.getInstance(); 
    hari = c.get(Calendar.DAY_OF_MONTH); 
    bulan = c.get(Calendar.MONTH) + 1; 
    tahun = c.get(Calendar.YEAR); 
    zonaWaktu = Variabel.getZonaWaktu(); 

    // inisialisasi Lokasi 
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

    // kriteria penggunaan lokasi 
    Criteria criteria = new Criteria(); 
    criteria.setAccuracy(Criteria.ACCURACY_COARSE); 
    criteria.setPowerRequirement(Criteria.POWER_LOW); 
    criteria.setBearingRequired(false); 
    criteria.setSpeedRequired(false); 
    criteria.setCostAllowed(true); 

    String provider = locationManager.getBestProvider(criteria, true); 
    location = locationManager.getLastKnownLocation(provider); 

    double lat= location.getLatitude(); 
    double lon= location.getLongitude(); 
    double alt= location.getAltitude(); 

    //double lat=-6.166666667; 
    //double lon=106.85; 
    //double alt=50; 
    /** 
    * Untuk menampilkan lokasi user berada pada judul jadwal shalat 
    * 
    */ 

    String namaKota; 
    String alamat = null; 
    TextView tv ; 
    tv = (TextView) findViewById(R.id.label_judul); 

    if (location!= null){ 
     Geocoder gc = new Geocoder(this, Locale.getDefault()); 

     try{ 
      List<Address> addresses= gc.getFromLocation(lat, lon, 1); 
      StringBuilder sb= new StringBuilder(); 
      if(addresses.size()>0){ 
       Address address= addresses.get(0); 

       sb.append(address.getLocality()); 
      } 
      alamat = sb.toString(); 
      tv.setText(alamat +", "+hari + "-"+bulan +"-"+ tahun); 
     } catch (IOException e) {} 
    }else{ 
     tv.setText(hari + "/"+bulan +"/"+ tahun); 
    } 



    /** 
    * Untuk mengambil nilai metode yang telah ditentukan oleh user 
    * 
    * @params metode merupakan tempat menyimpan sementara 
    */ 
    metode = Pengaturan.getMetodePerhitungan(this); 
    char a = metode.charAt(0); 
    switch (a) { 
    case '0': { 
     sudutSubuh = 20; 
     sudutIsya = 18; 
    } 
     break; 

    case '1': { 
     sudutSubuh = 18; 
     sudutIsya = 18; 
    } 
     break; 

    case '2': { 
     sudutSubuh = 15; 
     sudutIsya = 15; 
    } 
     break; 

    case '3': { 
     sudutSubuh = 17; 
     sudutIsya = 18; 
    } 
     break; 

    case '4': { 
     sudutSubuh = 18; 
     sudutIsya = (float) 18.5; 
    } 
     break; 

    case '5': { 
     sudutSubuh = (float) 17.5; 
     sudutIsya = (float) 19.5; 
    } 
     break; 
    } 

    /** 
    * Untuk mengambil nilai mazhab yang sudah ditentukan oleh user 
    */ 
    String mzb = Pengaturan.getMazhab(this); 
    char m = mzb.charAt(0); 
    mazhab = m == '0' ? 1 : 2; 

    // Menghitung variabel 
    shalat= new PerhitunganShalat(lat,lon, alt, zonaWaktu, hari, 
    bulan, tahun); 

    shalat.setSudutSubuh(sudutSubuh); 
    shalat.setSudutIsya(sudutIsya); 
    shalat.setMazhab(mazhab); 

    // Untuk mengkonfersi hasil perhitungan dalam format jam. 
    FormatWaktu subuh = new FormatWaktu(shalat.getWaktuSubuh()); 
    FormatWaktu dzuhur = new FormatWaktu(shalat.getWaktuZuhur()); 
    FormatWaktu ashar = new FormatWaktu(shalat.getWaktuAshar()); 
    FormatWaktu maghrib = new FormatWaktu(shalat.getWaktuMaghrib()); 
    FormatWaktu isya = new FormatWaktu(shalat.getWaktuIsya()); 

    waktuIsya.setText("" + isya.getJam() + ":" + isya.getMenit() + ":" 
      + isya.getDetik()); 
    waktuMaghrib.setText("" + maghrib.getJam() + ":" + maghrib.getMenit() 
      + ":" + maghrib.getDetik()); 
    waktuSubuh.setText("" + subuh.getJam() + ":" + subuh.getMenit() + ":" 
      + subuh.getDetik()); 
    waktuZuhur.setText("" + dzuhur.getJam() + ":" + dzuhur.getMenit() + ":" 
      + dzuhur.getDetik()); 
    waktuAshar.setText("" + ashar.getJam() + ":" + ashar.getMenit() + ":" 
      + ashar.getDetik()); 

    // untuk menyimpan waktu shalat agar dapat dipanggil di KuplukService 

    SharedPreferences jadwal = getSharedPreferences(JADWAL, 0); 
    SharedPreferences.Editor editor = jadwal.edit(); 
    editor.clear(); 

    editor.putInt("hari", hari); 
    editor.putInt("bulan", bulan); 
    editor.putInt("tahun", tahun); 
    editor.putInt("jSubuh", subuh.getJam()); 
    editor.putInt("mSubuh", subuh.getMenit()); 
    editor.putInt("jZuhur", dzuhur.getJam()); 
    editor.putInt("mZuhur", dzuhur.getMenit()); 
    editor.putInt("jAshar", ashar.getJam()); 
    editor.putInt("mAshar", ashar.getMenit()); 
    editor.putInt("jMaghrib", maghrib.getJam()); 
    editor.putInt("mMaghrib", maghrib.getMenit()); 
    editor.putInt("jIsya", isya.getJam()); 
    editor.putInt("mIsya", isya.getMenit()); 
    editor.commit(); 

    // cek keluaran 
    Log.d("AyoIbadah", "sudut subuh: " + shalat.getSudutSubuh()); 
    Log.d("AyoIbadah", "sudut isya: " + shalat.getSudutIsya()); 
    Log.d("AyoIbadah", "metode: " + metode); 
    Log.d("AyoIbadah", "mazhab: " + mazhab); 
    Log.d("AyoIbadah", "zonawaktu: " + zonaWaktu); 
    Log.d("AyoIbadah", "Jd LOkasi: " + shalat.getJdLokasi()); 
    Log.d("AyoIbadah", "Delta: " + shalat.getDelta()); 
    Log.d("AyoIbadah", "ET: " + shalat.getET()); 
    Log.d("k", "subuh= " + shalat.getWaktuSubuh()); 
    Log.d("AyoIbadah", "zuhur= " + shalat.getWaktuZuhur()); 
    Log.d("AyoIbadah", "Ashar= " + shalat.getWaktuAshar()); 
    Log.d("AyoIbadah", "maghrib= " + shalat.getWaktuMaghrib()); 
    Log.d("k", "isya= " + shalat.getWaktuIsya()); 

    // Inisiasi service 

    startService(svc); 
} 

@Override 
public void onResume() { 
    super.onResume(); 
    metode = Pengaturan.getMetodePerhitungan(this); 
    NotificationManager mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    mNM.cancel(IbadahService.NOTIF_ID); 
} 

@Override 
public void onStop() { 
    super.onStop(); 
    Intent svc = new Intent(this, IbadahService.class); 
    startService(svc); 
} 
} 

當我開始我的應用程序,顯示錯誤日誌: 07-31 14:49:39.734:ERROR/AndroidRuntime(4163):java.lang中:通過引起的。的NullPointerException 07-31 14:49:39.734:ERROR/AndroidRuntime(4163):在com.widy.ayopuasa.JadwalShalat.onCreate(JadwalShalat.java:99)

在線路99我的代碼是:

double lat= location.getLatitude(); 
    double lon= location.getLongitude(); 
    double alt= location.getAltitude(); 

任何人,你能告訴我什麼是問題,以及如何解決它? :)

在此先感謝。

更新: 我已經解決了部隊密切同:

String provider = locationManager.getBestProvider(criteria, true); 
    if (provider != null) { 
     location = locationManager.getLastKnownLocation(provider); 
     lat = location.getLatitude(); 
     lon = location.getLongitude(); 
     alt = location.getAltitude(); 
    }else{ 
     location = null; 
     lat=-6.166666667; 
     lon=106.85; 
     alt=50; 
    } 

我不知道這是最好的做法還是壞one..ane建議,朋友?

更新並解決!

準確!

我已將Class MyLocationListener添加到我的源代碼中。

並設置這樣的類MyLocationListener位置:

public class MyLocationListener implements LocationListener { 

public static double latitude; 
public static double longitude; 
public static double altitude; 

public void onLocationChanged(Location loc) 
{ 
    loc.getLatitude(); 
    loc.getLongitude(); 
    latitude=loc.getLatitude(); 
    longitude=loc.getLongitude(); 
    altitude=loc.getAltitude(); 
} 

在我的onCreate我已經加入:再一次接近

  mlocListener = new MyLocationListener(); 
     mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mlocListener); 
     lon = MyLocationListener.longitude; 
     lat = MyLocationListener.latitude; 
     alt = MyLocationListener.altitude; 

不強制:)

謝謝大家: )

+0

'locationManager.getLastKnownLocation(provider);'將在GPS關閉時返回null,所以'location = null'和nullpointer。您應該檢查,如果位置爲空,然後再使用它 – THelper

+0

對不起,我再也不會這樣做,我在這裏是新的:)謝謝 – wdyz

回答

1

location在您致電時仍爲空。

這從android documentation片段顯示瞭如何實例的偵聽器獲得位置更新:

// Define a listener that responds to location updates 
LocationListener locationListener = new LocationListener() { 
    public void onLocationChanged(Location location) { 
     // Called when a new location is found by the network location provider. 
     makeUseOfNewLocation(location); 
    } 

    public void onStatusChanged(String provider, int status, Bundle extras) {} 

    public void onProviderEnabled(String provider) {} 

    public void onProviderDisabled(String provider) {} 
    }; 

// Register the listener with the Location Manager to receive location updates 
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); 

你應該使用,只有當啓用了供應商的位置運行代碼;檢查是否至少有一個啓用提供商的呼叫:

public List<String> getProviders (Criteria criteria, boolean enabledOnly) 

enabledOnly = true

getProviders如果沒有啓用或訪問提供商返回null。

+0

恩,謝謝,但我怎麼能把這個代碼放在我的代碼?我仍然混淆了該代碼。謝謝 – wdyz

+0

簡單地說,把*雙lat = location.getLatitude()*和其他兩個以下的行內的* if(位置!= null){*塊,只是在下面的幾行;無論如何,它是更好的你閱讀文檔頁面我鏈接在我的回答 –

+0

謝謝,隊友。現在,力量關閉了,但我已經改變了我的代碼。請檢查朋友。任何建議? – wdyz

1

你以線

double lat= location.getLatitude();一個NullPointerException。

這告訴你location爲空。 location設置在上面的行上:

location = locationManager.getLastKnownLocation(provider);

docsLocationManager.getLastKnownLocation()狀態:

如果提供的是當前被禁用,則返回null。

所以,我懷疑位置提供商是禁用的。

+0

如何預測提供者是否被禁用。我的電話網絡提供商現在還活着。但它仍然返回空: -/ – wdyz

+0

解決!我的問題幾分鐘前解決,檢查我的問題,我已經添加了如何解決它:) – wdyz