-2

本のチュートリアルに従ってアプリケーションを作成しますが、不明な場所でプログラムをクラッシュさせるエラーが発生しました: logCat でエラー メッセージが表示されなかった So code of my location class`:

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.widget.TextView;

public class MainActivity extends Activity {

    private LocationManager myLocationManager;
    private LocationListener myLocationListener;
    private TextView myLatitude, myLongitude;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        myLatitude = (TextView)findViewById(R.id.textView1);
        myLongitude = (TextView)findViewById(R.id.textView2);



        myLocationManager = (LocationManager)getSystemService(
          Context.LOCATION_SERVICE);

        myLocationListener = new MyLocationListener();

        myLocationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER,
                0,
                0,
                myLocationListener);

        //Get the current location in start-up
        myLatitude.setText(String.valueOf(
            myLocationManager.getLastKnownLocation(
                LocationManager.GPS_PROVIDER).getLatitude()));

        myLongitude.setText(String.valueOf(
            myLocationManager.getLastKnownLocation(
               LocationManager.GPS_PROVIDER).getLongitude()));
    }

    private class MyLocationListener implements LocationListener{

        public void onLocationChanged(Location argLocation) {
        // TODO Auto-generated method stub
        myLatitude.setText(String.valueOf(
          argLocation.getLatitude()));
        myLongitude.setText(String.valueOf(
          argLocation.getLongitude()));
        }

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

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

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

マニフェスト ファイルに次のアクセス許可を追加します。Galaxy TAb でアプリケーションを起動した後、クラッシュしましたが、ツールバーで数秒後にデバッガーからアイコン「GPS」コードが飛び散りました (役立つ場合)。

DalvikVM[localhost:8624]    
    Thread [<1> main] (Suspended (exception RuntimeException))  
        <VM does not provide monitor information>   
        ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2663  
        ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2679   
        ActivityThread.access$2300(ActivityThread, ActivityThread$ActivityRecord, Intent) line: 125 
        ActivityThread$H.handleMessage(Message) line: 2033  
        ActivityThread$H(Handler).dispatchMessage(Message) line: 99 
        Looper.loop() line: 123 
        ActivityThread.main(String[]) line: 4627    
        Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
        Method.invoke(Object, Object...) line: 521  
        ZygoteInit$MethodAndArgsCaller.run() line: 868  
        ZygoteInit.main(String[]) line: 626 
        NativeStart.main(String[]) line: not available [native method]  
    Thread [<6> Binder Thread #2] (Running) 
    Thread [<5> Binder Thread #1] (Running) 
4

1 に答える 1

0

それはこれらの行です:

//Get the current location in start-up
myLatitude.setText(String.valueOf(
myLocationManager.getLastKnownLocation(
LocationManager.GPS_PROVIDER).getLatitude()));

ヌルポインタの原因

onLocationChanged() がトリガーされるまで、最後の既知の場所は null になります

于 2013-09-25T09:26:14.347 に答える