了强制关闭的创建活动,如果GPS关闭

问题描述:

可能重复:
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

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

你以线

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

这告诉你location为空。 location设置在上面的行上:

location = locationManager.getLastKnownLocation(provider);

docsLocationManager.getLastKnownLocation()状态:

如果提供的是当前被禁用,则返回null。

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

+0

如何预测提供者是否被禁用。我的电话网络提供商现在还活着。但它仍然返回空: -/ – wdyz

+0

解决!我的问题几分钟前解决,检查我的问题,我已经添加了如何解决它:) – wdyz