【发布时间】:2013-12-05 03:40:55
【问题描述】:
我创建了一个返回 gps 坐标的函数,到目前为止它运行良好,但我希望它仅在单击按钮时启动。我试图将 gps 合并到一个由单击按钮时启动的事件所描述的函数中,但是当我这样做时我得到了很多错误,我对如何解决这个问题没有任何其他想法。
这是 .java 文件:(Saver.java)
package com.example.lifesaver;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class Saver extends Activity {
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_saver);
b = (Button) findViewById(R.id.button1);
/* b.setOnClickListener(new OnClickListener(){
public void onClick(View v)
{
localizare();
}
}); */
// We use LocationManager class to obtain GPS locations
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
}
//MyLocationListener class
public class MyLocationListener implements LocationListener
{
// private void localizare()
// {
public void onLocationChanged(Location loc)
{
loc.getLatitude();
loc.getLongitude();
String Text = "Current location is: " + "Latitud = " + loc.getLatitude() + "Longitud = " + loc.getLongitude();
Toast.makeText(getApplicationContext(), Text, Toast.LENGTH_SHORT).show();
}
public void onProviderDisabled(String provider)
{
Toast.makeText(getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT).show();
}
public void onProviderEnabled(String provider)
{
Toast.makeText(getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}//}
}
和 .xml 文件(活动)saver.xml):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Saver" >
<Button
android:id="@+id/button1"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/circle"
android:onClick="onClick"/>
</RelativeLayout>
另外,我在 AndroidManifest.xml 文件中添加了这个:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION">
</uses-permission>
【问题讨论】:
-
“很多错误”是什么意思?你需要更具体。
-
有什么错误?请发布 logcat,以便人们有想法。
-
1." 方法 localizare() 未定义类型 new View.OnClickListener(){}" 或 2." 类型 Saver.MyLocationListener 必须实现继承的抽象方法 LocationListener.onStatusChanged(String ,int,Bundle)" 或 3."void 是变量 onLocationChanged 的无效类型"
-
即使在代码中注释了该行之后,您是否也会收到该错误?请从您的 .xml 中删除此行
android:onClick="onClick"并重新运行它。 -
仍有错误... :(
标签: java android xml button gps