I am making a example app for GPS positioning on my Evo 4G. This is to eventually be integrated into a app for time card use. But, it force closes. I don't have any ideas why... Any help?
我正在为我的Evo 4G上的GPS定位做一个示例应用程序。这最终将被集成到应用程序中以供时间卡使用。但是,它强行关闭。我没有任何想法为什么......有什么帮助吗?
package com.Rick.GPS;
import android.app.Activity;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
public class Main extends Activity implements LocationListener {
/* this class implements LocationListener, which listens for both
* changes in the location of the device and changes in the status
* of the GPS system.
* */
static final String tag = "Main"; // for Log
TextView txtInfo;
LocationManager lm;
StringBuilder sb;
int noOfFixes = 0;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* get TextView to display the GPS data */
txtInfo = (TextView) findViewById(R.id.Text1);
/* the location manager is the most vital part it allows access
* to location and GPS status services */
lm = (LocationManager) getSystemService(LOCATION_SERVICE);
}
protected void onResume() {
/*
* onResume is is always called after onStart, even if the app hasn't been
* paused
*
* add location listener and request updates every 1000ms or 10m
*/
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10f, this);
super.onResume();
}
protected void onPause() {
/* GPS, as it turns out, consumes battery like crazy */
lm.removeUpdates(this);
super.onPause();
}
public void onLocationChanged(Location location) {
Log.v(tag, "Location Changed");
sb = new StringBuilder(512);
noOfFixes++;
/* display some of the data in the TextView */
sb.append("No. of Fixes: ");
sb.append(noOfFixes);
sb.append('\n');
sb.append('\n');
sb.append("Londitude: ");
sb.append(location.getLongitude());
sb.append('\n');
sb.append("Latitude: ");
sb.append(location.getLatitude());
sb.append('\n');
sb.append("Altitiude: ");
sb.append(location.getAltitude());
sb.append('\n');
sb.append("Accuracy: ");
sb.append(location.getAccuracy());
sb.append('\n');
sb.append("Timestamp: ");
sb.append(location.getTime());
sb.append('\n');
txtInfo.setText(sb.toString());
}
public void onProviderDisabled(String provider) {
/* this is called if/when the GPS is disabled in settings */
Log.v(tag, "Disabled");
/* bring up the GPS settings */
Intent intent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
public void onProviderEnabled(String provider) {
Log.v(tag, "Enabled");
Toast.makeText(this, "GPS Enabled", Toast.LENGTH_SHORT).show();
}
public void onStatusChanged(String provider, int status, Bundle extras) {
/* This is called when the GPS status alters */
switch (status) {
case LocationProvider.OUT_OF_SERVICE:
Log.v(tag, "Status Changed: Out of Service");
Toast.makeText(this, "Status Changed: Out of Service",
Toast.LENGTH_SHORT).show();
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
Log.v(tag, "Status Changed: Temporarily Unavailable");
Toast.makeText(this, "Status Changed: Temporarily Unavailable",
Toast.LENGTH_SHORT).show();
break;
case LocationProvider.AVAILABLE:
Log.v(tag, "Status Changed: Available");
Toast.makeText(this, "Status Changed: Available",
Toast.LENGTH_SHORT).show();
break;
}
}
protected void onStop() {
/* may as well just finish since saving the state is not important for this toy app */
finish();
super.onStop();
}
}
Here is the layout file...
这是布局文件......
<TextView
android:id="@+id/Text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
And here is the manifest...
这是明显的......
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Rick.GPS"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".GPSActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Thanks for any help you can give me...
感谢你给与我的帮助...
4 个解决方案
#1
1
Two mistakes I could spot:
我发现的两个错误:
- You need the call to
setContentView(R.layout.main);
, don't comment it. otherwise, you get a NullPointerException in the next line. - In the XML you define
Text1
asEditText
, but than in your class you try to cast it toTextView
and you get class cast exception. Change it either in the XML or in the java file. According to your comment in the code, you should change it in the XML.
您需要调用setContentView(R.layout.main);,不要对它进行注释。否则,你会在下一行得到NullPointerException。
在XML中,您将Text1定义为EditText,但是在您的类中,您尝试将其强制转换为TextView并获得类强制转换异常。在XML或java文件中更改它。根据您在代码中的注释,您应该在XML中更改它。
#2
1
protected void onPause() { /* GPS, as it turns out, consumes battery like crazy */ lm.removeUpdates(this); super.onPause(); }
ALSO THE setContentView code is commented...
还注释了setContentView代码......
#3
0
Does onLocationChange have access to the textField? Anyway, I think a better way to design this application would be to do the location listening in a separate thead, and for example use a handler to update the UI thread. This approach also removes the risk that a location fix might block the UI-thread.
onLocationChange是否可以访问textField?无论如何,我认为设计这个应用程序的更好方法是在单独的thead中进行位置监听,例如使用处理程序来更新UI线程。此方法还消除了位置修复可能会阻止UI线程的风险。
#4
0
You're calling super.onResume in the body of your onPause method.
你在onPause方法的主体中调用super.onResume。
#1
1
Two mistakes I could spot:
我发现的两个错误:
- You need the call to
setContentView(R.layout.main);
, don't comment it. otherwise, you get a NullPointerException in the next line. - In the XML you define
Text1
asEditText
, but than in your class you try to cast it toTextView
and you get class cast exception. Change it either in the XML or in the java file. According to your comment in the code, you should change it in the XML.
您需要调用setContentView(R.layout.main);,不要对它进行注释。否则,你会在下一行得到NullPointerException。
在XML中,您将Text1定义为EditText,但是在您的类中,您尝试将其强制转换为TextView并获得类强制转换异常。在XML或java文件中更改它。根据您在代码中的注释,您应该在XML中更改它。
#2
1
protected void onPause() { /* GPS, as it turns out, consumes battery like crazy */ lm.removeUpdates(this); super.onPause(); }
ALSO THE setContentView code is commented...
还注释了setContentView代码......
#3
0
Does onLocationChange have access to the textField? Anyway, I think a better way to design this application would be to do the location listening in a separate thead, and for example use a handler to update the UI thread. This approach also removes the risk that a location fix might block the UI-thread.
onLocationChange是否可以访问textField?无论如何,我认为设计这个应用程序的更好方法是在单独的thead中进行位置监听,例如使用处理程序来更新UI线程。此方法还消除了位置修复可能会阻止UI线程的风险。
#4
0
You're calling super.onResume in the body of your onPause method.
你在onPause方法的主体中调用super.onResume。