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/cn.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灯, 水草

请输入看板名称,例如:WOW站内搜寻

TOP