我将位置坐标发送到手机时遇到问题。
以下是Main.Java的代码
package com.example.finder;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Main extends Activity {
TextView textLat;
TextView textLong;
TextView textAlt;
public String onLocat;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1 = (Button) findViewById(R.id.checkin);
btn1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Bundle extras = getIntent().getExtras();
String ALT = extras.getString("wysokosc");
String SPE = extras.getString("speed");
sendSMS("09164232662", "wys" + " " + ALT + " " + "spee" + " - "
+ SPE);
}
});
textLat = (TextView) findViewById(R.id.textLat);
textLong = (TextView) findViewById(R.id.textLong);
textAlt = (TextView) findViewById(R.id.textAlt);
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new mylocationlistener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
}
class mylocationlistener implements LocationListener {
public void onLocationChanged(Location location) {
if (location != null) {
double pLong = location.getLongitude();
double pLat = location.getLatitude();
double pAlt = location.getAltitude();
String PPro = location.getProvider();
textLat.setText(Double.toString(pLat));
textLong.setText(Double.toString(pLong));
textAlt.setText(Double.toString(pAlt));
Intent i = new Intent(Main.this,
Main.class);
i.putExtra("wysokosc",Double.toString(pLat));
i.putExtra("speed",Double.toString(pLong));
startActivity(i);
}
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
private void sendSMS(String phoneNumber, String message) {
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage("09164232662", null, message, null, null);
}
}
以下是GPS.Java的代码
package com.example.finder;
import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
public class GPS extends Activity {
Button checkout;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gps);
checkout = (Button) findViewById(R.id.checkout);
checkout.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
Bundle extras = getIntent().getExtras();
String ALT = extras.getString("wysokosc");
String SPE = extras.getString("speed");
sendSMS("09164232662", "wys" + " " + ALT + " " + "spee" + " - "
+ SPE);
}
});
}
// ---sends an SMS message to another device---
private void sendSMS(String phoneNumber, String message) {
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, null, null);
}
}
以下是清单文件的代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.finder"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Main"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.finder.GPS"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
这是logcat
02-02 14:27:03.619: D/libEGL(10290): loaded /system/lib/egl/libEGL_mali.so
02-02 14:27:03.629: D/libEGL(10290): loaded /system/lib/egl/libGLESv1_CM_mali.so
02-02 14:27:03.629: D/libEGL(10290): loaded /system/lib/egl/libGLESv2_mali.so
02-02 14:27:03.634: D/(10290): Device driver API match
02-02 14:27:03.634: D/(10290): Device driver API version: 10
02-02 14:27:03.634: D/(10290): User space API version: 10
02-02 14:27:03.634: D/(10290): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Tue Oct 16 15:37:13 KST 2012
02-02 14:27:03.659: D/OpenGLRenderer(10290): Enabling debug mode 0
02-02 14:27:05.309: D/AndroidRuntime(10290): Shutting down VM
02-02 14:27:05.309: W/dalvikvm(10290): threadid=1: thread exiting with uncaught exception (group=0x411b72a0)
02-02 14:27:05.314: E/AndroidRuntime(10290): FATAL EXCEPTION: main
02-02 14:27:05.314: E/AndroidRuntime(10290): java.lang.NullPointerException
02-02 14:27:05.314: E/AndroidRuntime(10290): at com.example.finder.Main$1.onClick(Main.java:36)
02-02 14:27:05.314: E/AndroidRuntime(10290): at android.view.View.performClick(View.java:4211)
02-02 14:27:05.314: E/AndroidRuntime(10290): at android.view.View$PerformClick.run(View.java:17267)
02-02 14:27:05.314: E/AndroidRuntime(10290): at android.os.Handler.handleCallback(Handler.java:615)
02-02 14:27:05.314: E/AndroidRuntime(10290): at android.os.Handler.dispatchMessage(Handler.java:92)
02-02 14:27:05.314: E/AndroidRuntime(10290): at android.os.Looper.loop(Looper.java:137)
02-02 14:27:05.314: E/AndroidRuntime(10290): at android.app.ActivityThread.main(ActivityThread.java:4898)
02-02 14:27:05.314: E/AndroidRuntime(10290): at java.lang.reflect.Method.invokeNative(Native Method)
02-02 14:27:05.314: E/AndroidRuntime(10290): at java.lang.reflect.Method.invoke(Method.java:511)
02-02 14:27:05.314: E/AndroidRuntime(10290): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
02-02 14:27:05.314: E/AndroidRuntime(10290): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
02-02 14:27:05.314: E/AndroidRuntime(10290): at dalvik.system.NativeStart.main(Native Method)
02-02 14:27:16.474: I/Process(10290): Sending signal. PID: 10290 SIG: 9
问题是它收到的信号却没有通过我的手机发送。
让我知道我在做什么错。
答案 0 :(得分:0)
更改为:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new mylocationlistener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
final String ALT = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLatitude();
final String SPE = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLongitude();
Button btn1 = (Button) findViewById(R.id.checkin);
btn1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
sendSMS("09164232662", "wys" + " " + ALT + " " + "spee" + " - "
+ SPE);
}
});
textLat = (TextView) findViewById(R.id.textLat);
textLong = (TextView) findViewById(R.id.textLong);
textAlt = (TextView) findViewById(R.id.textAlt);
}
答案 1 :(得分:0)
我认为您的应用程序中存在概念性问题。首先,您已在清单中定义了两个入口点,它应该类似于:
<activity
android:name=".Main"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.finder.GPS" />
那么你的CPS是无用的,你可以称之为?
你打电话给同一个活动吗?
意图i =新意图(Main.this, Main.class); i.putExtra( “wysokosc”,Double.toString(PLAT)); i.putExtra( “速度”,Double.toString(PLONG)); startActivity(ⅰ); 只需将pLong,pLat和pAlt定义为全局,并在onClick中使用它们发送短信。
希望这有帮助。