2013-02-11 63 views
0

我遇到了一個問題,我無法弄清楚。我已經嘗試了很多方法,但沒有一個能讓我明白它們爲什麼不起作用。GPS onCreate問題

問題

每次我想要運行的應用程序,它的崩潰。在第43行,它說//在這裏發生崩潰。

我想要做的是,我有2個textView與十進制輸入。

Sry基因我抄錯了XML,但它仍然同樣的問題

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<EditText 
android:id="@+id/lat" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="My text edit this." > 
    <requestFocus /> 
</EditText> 

<EditText 
android:id="@+id/lng" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="My text edit this." 
/> 

這是主要

public class Starter extends Activity implements LocationListener { 
protected boolean _active = true; 
AlertDialog alertDialog; 
int progress = 0; 
Location_Finder loc; 
protected  int _splashTime = 2000; 
private   Thread splashTread; 
public LocationManager lm; 

EditText LatText = null; 
EditText LongText = null; 

double latitude = 18.9599990845; 
double longitude = 72.819999694; 

public void onCreate(Bundle paramBundle) 
    { 
     super.onCreate(paramBundle); 
     setContentView(R.layout.starter); 

     lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, this); 

     LatText = (EditText) findViewById(R.id.text_lat); 
     LongText = (EditText) findViewById(R.id.text_lng); 

     double currentLat = latitude; 
     double currentLong = longitude; 

        //Crash here 
     LatText.setText(Double.toString(currentLat)); 
     LongText.setText(Double.toString(currentLong)); 

     splashTread = new Thread() { 
      @Override 
      public void run() { 
       try {     
        synchronized(this) { 
         wait(_splashTime); 
        }     
       } catch(InterruptedException e) { 
        System.out.println("EXc=" + e); 
       } 
       finally {    

        startActivity(new Intent(Starter.this, LocationUpdater.class));     
        //stop(); 
        finish(); 
       } 
      } 
     };  
     splashTread.start(); 

     } 

/* This method is called when use position will get changed */ 
public void onLocationChanged(Location loc) { 
    if (loc != null) { 

    double lat = loc.getLatitude(); 
    double lng = loc.getLongitude(); 

    String lat_text = ""+ lat + ""; 
    String long_text = ""+ lng + ""; 

    LatText.setText(lat_text); 
    LongText.setText(long_text); 

    } 
} 



public void onProviderDisabled(String provider) { 
    // required for interface, not used 
} 

public void onProviderEnabled(String provider) { 
    // required for interface, not used 
} 

public void onStatusChanged(String provider, int status, Bundle extras) { 
    // required for interface, not used 
} 

protected boolean isRouteDisplayed() { 
    // TODO Auto-generated method stub 
    return false; 
} 
}  

LOG

02-11 13:59:16.499: D/AndroidRuntime(10702): Shutting down VM 
    02-11 13:59:16.499: W/dalvikvm(10702): threadid=1: thread exiting with uncaught    exception (group=0x41f592a0) 
    02-11 13:59:16.504: E/AndroidRuntime(10702): FATAL EXCEPTION: main 
    02-11 13:59:16.504: E/AndroidRuntime(10702): java.lang.RuntimeException: Unable to  start activity ComponentInfo{com.androidhive.jsonparsing/com.androidhive.jsonparsing.Starter}: java.lang.NullPointerException 
    02-11 13:59:16.504: E/AndroidRuntime(10702): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100) 
    02-11 13:59:16.504: E/AndroidRuntime(10702): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125) 
    02-11 13:59:16.504: E/AndroidRuntime(10702): at android.app.ActivityThread.access$600(ActivityThread.java:140) 
    02-11 13:59:16.504: E/AndroidRuntime(10702): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227) 
    02-11 13:59:16.504: E/AndroidRuntime(10702): at android.os.Handler.dispatchMessage(Handler.java:99) 
    02-11 13:59:16.504: E/AndroidRuntime(10702): at android.os.Looper.loop(Looper.java:137) 
    02-11 13:59:16.504: E/AndroidRuntime(10702): at android.app.ActivityThread.main(ActivityThread.java:4898) 
    02-11 13:59:16.504: E/AndroidRuntime(10702): at java.lang.reflect.Method.invokeNative(Native Method) 
    02-11 13:59:16.504: E/AndroidRuntime(10702): at java.lang.reflect.Method.invoke(Method.java:511) 
    02-11 13:59:16.504: E/AndroidRuntime(10702): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006) 
    02-11 13:59:16.504: E/AndroidRuntime(10702): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773) 
    02-11 13:59:16.504: E/AndroidRuntime(10702): at dalvik.system.NativeStart.main(Native Method) 
    02-11 13:59:16.504: E/AndroidRuntime(10702): Caused by: java.lang.NullPointerException 
    02-11 13:59:16.504: E/AndroidRuntime(10702): at com.androidhive.jsonparsing.Starter.onCreate(Starter.java:44) 
    02-11 13:59:16.504: E/AndroidRuntime(10702): at android.app.Activity.performCreate(Activity.java:5206) 
    02-11 13:59:16.504: E/AndroidRuntime(10702): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083) 
    02-11 13:59:16.504: E/AndroidRuntime(10702): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064) 
    02-11 13:59:16.504: E/AndroidRuntime(10702): ... 11 more 

這是需要

當應用程序啓動,我希望它獲得的經度和緯度,並把它的TextView與當前位置經緯度和長期價值。

+0

您可以發佈您的錯誤的堆棧跟蹤? – nicopico 2013-02-11 12:34:17

+0

這裏是日誌 – Tirolel 2013-02-11 13:03:05

回答

-1

從你的堆棧跟蹤中,我會說LatText爲空,因爲你沒有使用正確的ID。

嘗試使用LatText = (EditText) findViewById(R.id.lat);在你的XML佈局定義


OLD ANSWER

我認爲你是落後在這裏做的事情:

  1. LocationManager.requestLocationUpdates()可能需要很長的時間, 它應該在後臺線程中完成。
  2. 開始/結束活動或從後臺線程修改UI是不安全的。您應該使用該UI線程

您可以使用後臺線程來請求位置,並使用runOnUiThread更新您的TextViews或開始新的活動。

重要提示:確保停止活動的onPause方法中的線程!

+0

這個答案是錯的。你可以從主線程調用requestLocationUpdates,它根本沒有任何阻塞,因爲它有一個觸發回調的位置onLocationChanged() – NickT 2013-02-11 11:13:34

+0

好吧,呃任何人都可以引導我解決這個問題? – Tirolel 2013-02-11 11:52:47

+0

@NickT似乎你是對的,不知道爲什麼我認爲你應該從後臺線程調用它... – nicopico 2013-02-11 12:34:00

0

更換

LatText.setText(Double.toString(currentLat)); 
LongText.setText(Double.toString(currentLong)); 

LatText.setText(String.valueOf(currentLat)); 
LongText.setText(String.valueOf(currentLong));