2011-12-28 34 views
1

我試圖在用戶按下獲取位置按鈕後,在單獨文本瀏覽中顯示經度和緯度。所有的代碼對我來說似乎都沒問題,但我得到空指針異常。只有在用戶按下按鈕時才更改位置,我不想執行任何操作。試圖在textView中顯示經度和緯度

package com.seanhannon.fyp; 

import android.app.Activity; 
import android.content.Context; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 


public class FYPActivity extends Activity { 

public Button getLocation; 
public TextView LongCoord; 
public TextView LatCoord; 
public double longitude; 
public double latitude; 
public LocationManager lm; 


/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, new MyLocationListener()); 

    getLocation = (Button)getLocation.findViewById(R.id.GetLocation); 
    LongCoord = (TextView)LongCoord.findViewById(R.id.longCoord); 
    LatCoord = (TextView)LatCoord.findViewById(R.id.LatCoord); 

    getLocation.setOnClickListener(new OnClickListener() 
    { 

     @Override 
     public void onClick(View arg0) 
     { 

      showCurrentLocation(); 
     } 



    }); 

} 


protected void showCurrentLocation() 
{ 
    // TODO Auto-generated method stub 
    Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
    latitude = location.getLatitude(); 
    longitude = location.getLongitude(); 

    LongCoord.setText(Double.toString(longitude)); 
    LatCoord.setText(Double.toString(latitude)); 
} 

} 

class MyLocationListener implements LocationListener 
{ 

    @Override 
    public void onLocationChanged(Location location) 
    { 
     // TODO Auto-generated method stub 



    } 

    @Override 
    public void onProviderDisabled(String provider) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onProviderEnabled(String provider) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
     // TODO Auto-generated method stub 

    } 

} 

XML文件是通常與細和粗位置等

這裏的權限是logcat的輸出

FATAL EXCEPTION: main 
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.seanhannon.fyp/com.seanhannon.fyp.FYPActivity}: java.lang.NullPointerException 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:123) 
at android.app.ActivityThread.main(ActivityThread.java:4627) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:521) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.NullPointerException 
at com.seanhannon.fyp.FYPActivity.onCreate(FYPActivity.java:33) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
... 11 more 

回答

2

這是你的代碼和平

getLocation = (Button)getLocation.findViewById(R.id.GetLocation); 
LongCoord = (TextView)LongCoord.findViewById(R.id.longCoord); 
LatCoord = (TextView)LatCoord.findViewById(R.id.LatCoord); 

用以下代碼更改它

getLocation = (Button)findViewById(R.id.GetLocation); 
LongCoord = (TextView)findViewById(R.id.longCoord); 
LatCoord = (TextView)findViewById(R.id.LatCoord); 

它會刪除空指針異常......

+0

謝謝磨,因爲沒有任何Android所以基本都走了完全 –

+0

最歡迎的,是否能解決你的問題,接受的答案已經幾個月。 – AAnkit