我的程序有两项服务。一项服务是GPSTracker,另一项是PlayerService。 GPSTraker必须从用户处获取LatLng,并且当用户前往该区域时,PlayerService必须播放确定区域的音乐。
启动程序时,用户可以设置本地和本地音乐。该程序启动用户选择区域的音乐文件的活动PlayListActivity。 选择音乐时,gps会验证用户的位置,并播放您所在地区的音乐。 GPSTracker包含服务PlayerService,以启动音乐文件。
在主要活动中,该程序调用用户选择文件的服务。
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
m_Text = input.getText().toString();
CircleOptions circleOptions = new CircleOptions();
circleOptions.center(new LatLng(point.latitude,point.longitude));
circleOptions.radius(Double.parseDouble(m_Text));
circleOptions.strokeColor(Color.BLACK);
circleOptions.fillColor(0x5500ff00);
circleOptions.strokeWidth(2);
circles.add(googleMap.addCircle(circleOptions));
Intent i = new Intent (getApplicationContext(), PlayListActivity.class);
startActivityForResult(i, 100);
}
});
并具有回调功能
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
HashMap<String, String> resultado;
if(resultCode != 0 && resultCode != RESULT_CANCELED)
if(resultCode == 100){
int index = data.getExtras().getInt("songIndex");
Log.i("Main:Extra", String.valueOf(index));
resultado = songsList.get(index);
String path = resultado.get("songPath");
gps.addPath(path);
gps.verifyPosition();
}
}
在PlayerLisActivity中,当用户选择音乐时,将结果返回给主Activity。
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent in = new Intent(getApplicationContext(), MainActivity.class);
Log.i("Play:Extra", String.valueOf(songIndex));
in.putExtra("songIndex", songIndex);
setResult(100, in);
finish();
}
在GPSTracker中,它创建了构造函数的意图: playerService = new Intent(context,PlayerService.class);
并且,当主活动调用gps.verifyPosition()时,当startService(playerService)出现错误时。
public void verifyPosition(){
float[] distance = new float[2];
int i = 0;
if(circles != null)
for(Circle c : circles){
Location.distanceBetween( marker.getPosition().latitude, marker.getPosition().longitude,
c.getCenter().latitude, c.getCenter().longitude, distance);
if( distance[0] < c.getRadius() ){
Log.i("TRACKER:MUSICA", paths.get(i));
playerService.putExtra("path", paths.get(i));
startService(playerService);
break;
}
i++;
}
}
05-22 15:24:06.919: E/AndroidRuntime(2110): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=100, data=Intent { cmp=com.example.radiomap/.MainActivity (has extras) }} to activity {com.example.radiomap/com.example.radiomap.MainActivity}: java.lang.NullPointerException
05-22 15:24:06.919: E/AndroidRuntime(2110): at android.app.ActivityThread.deliverResults(ActivityThread.java:3500)
05-22 15:24:06.919: E/AndroidRuntime(2110): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3543)
05-22 15:24:06.919: E/AndroidRuntime(2110): at android.app.ActivityThread.access$1200(ActivityThread.java:159)
05-22 15:24:06.919: E/AndroidRuntime(2110): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
05-22 15:24:06.919: E/AndroidRuntime(2110): at android.os.Handler.dispatchMessage(Handler.java:99)
05-22 15:24:06.919: E/AndroidRuntime(2110): at android.os.Looper.loop(Looper.java:176)
05-22 15:24:06.919: E/AndroidRuntime(2110): at android.app.ActivityThread.main(ActivityThread.java:5419)
05-22 15:24:06.919: E/AndroidRuntime(2110): at java.lang.reflect.Method.invokeNative(Native Method)
05-22 15:24:06.919: E/AndroidRuntime(2110): at java.lang.reflect.Method.invoke(Method.java:525)
05-22 15:24:06.919: E/AndroidRuntime(2110): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
05-22 15:24:06.919: E/AndroidRuntime(2110): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
05-22 15:24:06.919: E/AndroidRuntime(2110): at dalvik.system.NativeStart.main(Native Method)
05-22 15:24:06.919: E/AndroidRuntime(2110): Caused by: java.lang.NullPointerException
05-22 15:24:06.919: E/AndroidRuntime(2110): at android.content.ContextWrapper.startService(ContextWrapper.java:480)
05-22 15:24:06.919: E/AndroidRuntime(2110): at com.example.radiomap.GPSTracker.verifyPosition(GPSTracker.java:240)
05-22 15:24:06.919: E/AndroidRuntime(2110): at com.example.radiomap.MainActivity.onActivityResult(MainActivity.java:228)
05-22 15:24:06.919: E/AndroidRuntime(2110): at android.app.Activity.dispatchActivityResult(Activity.java:5563)
05-22 15:24:06.919: E/AndroidRuntime(2110): at android.app.ActivityThread.deliverResults(ActivityThread.java:3496)
05-22 15:24:06.919: E/AndroidRuntime(2110): ... 11 more
有人能帮助我吗?