AndroidDev 板


LINE

後來我嘗試了另外一個sample code做修改 public class MainActivity extends AppCompatActivity { private static final int REQUEST_LOCATION = 1; Button button; TextView textView; LocationManager locationManager; String lattitude,longitude; String lattitude2,longitude2; LocationListener locationListener; public static final String TAG = "MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION); textView = (TextView)findViewById(R.id.text_location); button = (Button)findViewById(R.id.button_location); button.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v){ locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { buildAlertMessageNoGps(); } else if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { getLocation(); } } }); } private void getLocation() { if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission (MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION); } else { Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); Location location1 = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); Location location2 = locationManager.getLastKnownLocation(LocationManager. PASSIVE_PROVIDER); locationManager.requestLocationUpdates("gps",5000,0,locationListener); if (location != null) { double latti = location.getLatitude(); double longi = location.getLongitude(); lattitude = String.valueOf(latti); longitude = String.valueOf(longi); textView.setText("Your current location is"+ "\n" + "Lattitude = " + lattitude + "\n" + "Longitude = " + longitude); Log.i(TAG,"location"); } else if (location1 != null) { double latti = location1.getLatitude(); double longi = location1.getLongitude(); lattitude = String.valueOf(latti); longitude = String.valueOf(longi); Log.i(TAG,"location1"); textView.setText("Your current location is"+ "\n" + "Lattitude = " + lattitude + "\n" + "Longitude = " + longitude); } else if (location2 != null) { double latti = location2.getLatitude(); double longi = location2.getLongitude(); lattitude = String.valueOf(latti); longitude = String.valueOf(longi); textView.setText("Your current location is"+ "\n" + "Lattitude = " + lattitude + "\n" + "Longitude = " + longitude); Log.i(TAG,"location2"); }else{ Toast.makeText(this,"Unble to Trace your location",Toast.LENGTH_SHORT).show(); Log.i(TAG,"Unable"); } locationListener =new LocationListener() { @Override public void onLocationChanged(Location location) { double latti2 = location.getLatitude(); double longi2 = location.getLongitude(); lattitude2 = String.valueOf(latti2); longitude2 = String.valueOf(longi2); Log.i(TAG,"location1"); textView.setText("Your current location is"+ "\n" + "Lattitude = " + lattitude2 + "\n" + "Longitude = " + longitude2); } public void onStatusChanged (String provider, int status, Bundle extras){ } @Override public void onProviderEnabled(String s) { } @Override public void onProviderDisabled(String s) { } }; } } protected void buildAlertMessageNoGps() { final AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("Please Turn ON your GPS Connection") .setCancelable(false) .setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(final DialogInterface dialog, final int id) { startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)); } }) .setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(final DialogInterface dialog, final int id) { dialog.cancel(); } }); final AlertDialog alert = builder.create(); alert.show(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } } 這個sample code原來是沒有locationListener 也沒有locationManager.requestLocationUpdates(,,,) 加了這串以後 logcat顯示出錯誤 D/AndroidRuntime: Shutting down VM E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.currentgps, PID: 18899 java.lang.IllegalArgumentException: invalid listener: null at android.location.LocationManager.checkListener(LocationManager.java:2344) at android.location.LocationManager.requestLocationUpdates(LocationManager.java:557) at com.example.currentgps.MainActivity.getLocation(MainActivity.java:87) at com.example.currentgps.MainActivity.access$000(MainActivity.java:31) at com.example.currentgps.MainActivity$1.onClick(MainActivity.java:60) at android.view.View.performClick(View.java:6597) at android.view.View.performClickInternal(View.java:6574) at android.view.View.access$3100(View.java:778) at android.view.View$PerformClick.run(View.java:25885) at android.os.Handler.handleCallback(Handler.java:873) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) I/Process: Sending signal. PID: 18899 SIG: 9 Process 18899 terminated. 這個sample code能成功顯示出目前的GPS location 但是我希望5秒更新一次 後續我想要把所有的GPS location記錄下來,然後發到email上 但是目前嘗試了很多 看了網上的教學 只要用到locationManager.requestLocationUpdates()就不成功 可以請高手幫忙看看這個報錯是哪裡有問題嗎? --



※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 172.58.27.173 (美國)
※ 文章網址: https://webptt.com/m.aspx?n=bbs/AndroidDev/M.1570640533.A.8D5.html
1F:→ LZN: locationListener還沒init就被調用了 10/14 11:15







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:Gossiping站內搜尋

TOP