无法通过Mapsforge获取位置

时间:2016-07-25 01:24:20

标签: android mapsforge

我正在尝试使用Mapsforge 0.6.1获取经度和纬度,但会出现错误。我正在使用Android Studio。

logcat的:

public function store(Request $request)
{
    //establish random generated string for gallery_id
    $rando = str_random(8);

    //input all data to database 
    $data = $request->all(); 

    $newproperty['title'] = $data['title']; 
    $newproperty['address'] = $data['address']; 
    $newproperty['city'] = $data['city'];
    $newproperty['province'] = $data['province']; 
    $newproperty['contact_person'] = Auth::user()->id; 
    $newproperty['gallery_id'] = $rando; 
    $newproperty['property_description'] = $data['description']; 

    if($request->hasfile('images')) {  
       $files = $request->file('images'); 

       //storage into AWS 
       foreach ($files as $file) { 
           $uploadedFile = $file;
           $upFileName = time() . '.' . $uploadedFile->getClientOriginalName(); 
           $filename = strtolower($upFileName); 

           $s3 = \Storage::disk('s3'); 
           $filePath = 'properties/' . $rando . '/' . $filename;

           $s3->put($filePath, file_get_contents($uploadedFile), 'public');

           $propicurl = 'https://s3-ap-southeast-1.amazonaws.com/cebuproperties/' . $filePath; 

           $array = array_add(['img'=> '$propicurl']);

           $newproperty['picgallery'] = $array; 

       } 
    } 

    Properties::create($newproperty); 

    return redirect('/properties');
}

这是我的代码。在onCreate方法中,我调用了getLatitude,然后出现错误。

E/AndroidRuntime: FATAL EXCEPTION: main
   Process: com.example.luis.safe, PID: 27378
   java.lang.RuntimeException: Unable to create service com.example.luis.safe.Tracker: java.lang.NullPointerException: Attempt to invoke virtual method 'double org.mapsforge.core.model.LatLong.getLatitude()' on a null object reference
       at android.app.ActivityThread.handleCreateService(ActivityThread.java:2905)
       at android.app.ActivityThread.access$1900(ActivityThread.java:157)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1439)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:148)
       at android.app.ActivityThread.main(ActivityThread.java:5530)
       at java.lang.reflect.Method.invoke(Native Method)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'double org.mapsforge.core.model.LatLong.getLatitude()' on a null object reference
       at com.example.luis.safe.Tracker.onCreate(Tracker.java:57)
       at android.app.ActivityThread.handleCreateService(ActivityThread.java:2895)
       at android.app.ActivityThread.access$1900(ActivityThread.java:157) 
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1439) 
       at android.os.Handler.dispatchMessage(Handler.java:102) 
       at android.os.Looper.loop(Looper.java:148) 
       at android.app.ActivityThread.main(ActivityThread.java:5530) 
       at java.lang.reflect.Method.invoke(Native Method) 
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732) 
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622) 

1 个答案:

答案 0 :(得分:0)

正如日志所说:latLongnull。在初始化getLatitude()之前,您无法getLongitude()latLong。在latLong中初始化onLocationChanged(Location arg0)。并查看此code以了解您应该做什么。