我在编写一个android程序时遇到了困难。所以..:我在 MainActivity 中创建了一个 GoogleMap 对象 (theMap)。然后我启动了一个名为 GetDirections 的新 Activity,它启动了一个名为 RequestDirection(Non-Activity) 的新类。RequestDirection 类执行 Google Directions API 请求并解析 JSON结果创建一个 PolylineOptions 对象。那么我如何访问 GoogleMap 对象(theMap)来添加折线???示例代码:
public class MainActivity extends Activity {
public GoogleMap theMap;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationIcon=R.drawable.red_pin;
theMap=((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
}}
public class GetDirections extends Activity{
//ask user to set Start/Destination Point
RequestDirection req= new RequestDirection();
req.execute();
finish();
}
public class RequestDirection extends AsyncTask {
doInBackground( ){
//returns the json string
}
onPostExecute( ){
PolylineOptions options= (the overview polyline points)
}
}
请注意,所有 3 个类都不是嵌套的。它们是单独的文件
我可以使用 mainActivity 上下文并将其传递给一个新类,但现在我开始一个新的 Activity 并从那里开始该类。
我能做些什么??我浪费了一整天,没有解决方案..
添加了 dst 建议的例外情况:
08-19 03:13:38.127: W/dalvikvm(1871): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
08-19 03:13:38.157: E/AndroidRuntime(1871): FATAL EXCEPTION: main
08-19 03:13:38.157: E/AndroidRuntime(1871): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=0, data=null} to activity {com.ceid.CeidMaps/com.ceid.CeidMaps.MainActivity}: java.lang.NullPointerException
08-19 03:13:38.157: E/AndroidRuntime(1871): at android.app.ActivityThread.deliverResults(ActivityThread.java:3141)
08-19 03:13:38.157: E/AndroidRuntime(1871): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3184)
08-19 03:13:38.157: E/AndroidRuntime(1871): at android.app.ActivityThread.access$1100(ActivityThread.java:130)
08-19 03:13:38.157: E/AndroidRuntime(1871): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)
08-19 03:13:38.157: E/AndroidRuntime(1871): at android.os.Handler.dispatchMessage(Handler.java:99)
08-19 03:13:38.157: E/AndroidRuntime(1871): at android.os.Looper.loop(Looper.java:137)
08-19 03:13:38.157: E/AndroidRuntime(1871): at android.app.ActivityThread.main(ActivityThread.java:4745)
08-19 03:13:38.157: E/AndroidRuntime(1871): at java.lang.reflect.Method.invokeNative(Native Method)
08-19 03:13:38.157: E/AndroidRuntime(1871): at java.lang.reflect.Method.invoke(Method.java:511)
08-19 03:13:38.157: E/AndroidRuntime(1871): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-19 03:13:38.157: E/AndroidRuntime(1871): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-19 03:13:38.157: E/AndroidRuntime(1871): at dalvik.system.NativeStart.main(Native Method)
08-19 03:13:38.157: E/AndroidRuntime(1871): Caused by: java.lang.NullPointerException
08-19 03:13:38.157: E/AndroidRuntime(1871): at com.ceid.CeidMaps.MainActivity.onActivityResult(MainActivity.java:162)
08-19 03:13:38.157: E/AndroidRuntime(1871): at android.app.Activity.dispatchActivityResult(Activity.java:5192)
08-19 03:13:38.157: E/AndroidRuntime(1871): at android.app.ActivityThread.deliverResults(ActivityThread.java:3137)
08-19 03:13:38.157: E/AndroidRuntime(1871): ... 11 more