2014-02-24 66 views
0

我正在製作一個GPS應用程序,您可以通過點擊按鈕來獲取GPS座標。正如標題所述,當我點擊按鈕時它會關閉。GPS應用程序 - 運行但關閉按鈕單擊

我是新開發的Android開發人員,從Java的一些知識和一些Android教程中得到了很多。

import android.app.Activity; 
import android.content.Context; 
import android.content.pm.ActivityInfo; 
import android.content.res.Configuration; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 

public class MainActivity extends Activity implements LocationListener { 

    private TextView startLatitudeField; 
    private TextView startLongitudeField; 
    private TextView endLatitudeField; 
    private TextView endLongitudeField; 
    private LocationManager locationManager; 
    private LocationListener locationListener; 
    private String provider; 
    double startLat = 0; 
    double startLng = 0; 
    double endLat = 0; 
    double endLng = 0; 



    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

     //Setting TextViews for the coordinates 
     startLatitudeField = (TextView) findViewById(R.id.TextView02); 
     startLongitudeField = (TextView) findViewById(R.id.TextView04); 
     endLatitudeField = (TextView) findViewById(R.id.textView06); 
     endLongitudeField = (TextView) findViewById(R.id.textView08); 



     //Button 
     Button button = (Button) findViewById(R.id.button1); 
     button.setOnClickListener(new View.OnClickListener() 
     { 

      @Override 
      public void onClick(View v) { 

       locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
       locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); 
       Location location = locationManager.getLastKnownLocation(provider); 

       if (startLat != 0) { 
        onLocationChanged(location); 
       } else { 
        onLocationChanged(location); 
        Button button = (Button) findViewById(R.id.button1); 
        button.setText("Get me home"); 
       } 
      } 
     }); 
    }; 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 


    @Override 
    public void onLocationChanged(Location location) { 

     if (startLat == 0) { 
      startLat = (double) (location.getLatitude()); 
      startLng = (double) (location.getLongitude()); 
      startLatitudeField.setText(String.valueOf(startLat)); 
      startLongitudeField.setText(String.valueOf(startLng)); 
     } 
     else { 

      endLat = (double) (location.getLatitude()); 
      endLng = (double) (location.getLongitude()); 
      endLatitudeField.setText(String.valueOf(endLat)); 
      endLongitudeField.setText(String.valueOf(endLng)); 
     } 

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

    } 
    @Override 
    public void onProviderEnabled(String provider) { 


    } 
    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 


    } 


} 

以下是我的錯誤日誌:

02-24 12:57:55.580: I/Process(4655): Sending signal. PID: 4655 SIG: 9 
02-24 12:58:09.664: D/AndroidRuntime(4772): Shutting down VM 
02-24 12:58:09.664: W/dalvikvm(4772): threadid=1: thread exiting with uncaught exception (group=0x41544ba8) 
02-24 12:58:09.664: E/AndroidRuntime(4772): FATAL EXCEPTION: main 
02-24 12:58:09.664: E/AndroidRuntime(4772): Process: com.example.getmehome, PID: 4772 
02-24 12:58:09.664: E/AndroidRuntime(4772): java.lang.IllegalArgumentException: invalid listener: null 
02-24 12:58:09.664: E/AndroidRuntime(4772):  at android.location.LocationManager.checkListener(LocationManager.java:1635) 
02-24 12:58:09.664: E/AndroidRuntime(4772):  at android.location.LocationManager.requestLocationUpdates(LocationManager.java:450) 
02-24 12:58:09.664: E/AndroidRuntime(4772):  at com.example.getmehome.MainActivity$1.onClick(MainActivity.java:57) 
02-24 12:58:09.664: E/AndroidRuntime(4772):  at android.view.View.performClick(View.java:4438) 
02-24 12:58:09.664: E/AndroidRuntime(4772):  at android.view.View$PerformClick.run(View.java:18422) 
02-24 12:58:09.664: E/AndroidRuntime(4772):  at android.os.Handler.handleCallback(Handler.java:733) 
02-24 12:58:09.664: E/AndroidRuntime(4772):  at android.os.Handler.dispatchMessage(Handler.java:95) 
02-24 12:58:09.664: E/AndroidRuntime(4772):  at android.os.Looper.loop(Looper.java:136) 
02-24 12:58:09.664: E/AndroidRuntime(4772):  at android.app.ActivityThread.main(ActivityThread.java:5017) 
02-24 12:58:09.664: E/AndroidRuntime(4772):  at java.lang.reflect.Method.invokeNative(Native Method) 
02-24 12:58:09.664: E/AndroidRuntime(4772):  at java.lang.reflect.Method.invoke(Method.java:515) 
02-24 12:58:09.664: E/AndroidRuntime(4772):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
02-24 12:58:09.664: E/AndroidRuntime(4772):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
02-24 12:58:09.664: E/AndroidRuntime(4772):  at dalvik.system.NativeStart.main(Native Method) 
+1

任何錯誤日誌?,請 – Kedarnath

+0

http://pastebin.com/DwAxVMZ2 – Bob21

+0

把你的位置管理器超過按鈕onClick()。 –

回答

1

你忘了初始化provider & locationListener變量,這就是爲什麼它顯示IllegalArgumentException: invalid listener: null

我編輯了自己的代碼,現在它工作在我的設備,

public class MainActivity extends Activity 
{ 
    private TextView startLatitudeField; 
    private TextView startLongitudeField; 
    private TextView endLatitudeField; 
    private TextView endLongitudeField; 
    private LocationManager locationManager; 
    private LocationListener locationListener; 
    private String provider; 
    double startLat = 0; 
    double startLng = 0; 
    double endLat = 0; 
    double endLng = 0; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

     //Setting TextViews for the coordinates 
     startLatitudeField = (TextView) findViewById(R.id.txtView1); 
     startLongitudeField = (TextView) findViewById(R.id.txtView2); 
     endLatitudeField = (TextView) findViewById(R.id.txtView3); 
     endLongitudeField = (TextView) findViewById(R.id.txtView4); 

     //Button 
     Button button = (Button) findViewById(R.id.cmdButton); 
     button.setOnClickListener(new View.OnClickListener() 
     { 

      @Override 
      public void onClick(View v) { 

       locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
       locationListener = new MyLocationListener (); 
       locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); 
       Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

       if (startLat != 0) 
       { 
        locationListener.onLocationChanged(location); 
       } else 
       { 
        locationListener.onLocationChanged(location); 
        Button button = (Button) findViewById(R.id.cmdButton); 
        button.setText("Get me home"); 
       } 
      } 
     }); 
    }; 


    private class MyLocationListener implements LocationListener 
    { 
     @Override 
     public void onLocationChanged(Location location) 
     { 
      // TODO Auto-generated method stub 
      if (startLat == 0) { 
       startLat = (double) (location.getLatitude()); 
       startLng = (double) (location.getLongitude()); 
       startLatitudeField.setText(String.valueOf(startLat)); 
       startLongitudeField.setText(String.valueOf(startLng)); 
      } 
      else { 

       endLat = (double) (location.getLatitude()); 
       endLng = (double) (location.getLongitude()); 
       endLatitudeField.setText(String.valueOf(endLat)); 
       endLongitudeField.setText(String.valueOf(endLng)); 
      } 
     } 

     @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 

     } 
    } 
} 
+0

使用這段代碼我仍然崩潰。 – Bob21

+1

@Tom,它對我來說非常好。你能告訴我你的新錯誤日誌嗎? – Kedarnath

+0

http://pastebin.com/L8T0VGbX – Bob21

1

Activity必須implements LocationListenerimplements all the methods

更新:你應該添加提供商位置,如:

Criteria criteria = new Criteria(); 
criteria.setAccuracy(Criteria.ACCURACY_FINE); 
Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false)); 

在這裏,你必須使用GPS_PROVIDER使設置Accuracy

criteria.setAccuracy(Criteria.ACCURACY_FINE); 

如果你使用NETWORK_PROVIDER然後套裝Accuracy

criteria.setAccuracy(Criteria.ACCURACY_COARSE); 

欲瞭解更多信息,請訪問:http://androidexample.com/GPS_Basic_-_Android_Example/index.php?view=article_discription&aid=68&aaid=93

+0

我已經完成了這項工作並擁有權限。仍與當前代碼崩潰(我已編輯我的OP) – Bob21

+0

看到我的新更新,並嘗試 –

+0

我試過了,仍然崩潰。然而,當我建立這個應用程序時,我開始使用你在那裏的代碼,它只是獲取一次座標並存儲它們並再次使用它們。每次我按下按鈕,我都想獲得新的座標。 – Bob21

相關問題