简体   繁体   English

从另一个类访问MainActivity对象

[英]Accesing a MainActivity object from another class

I have a difficult situation with an android program im writing. 我在编写Android程序时遇到了困难的情况。 So.. : I have created a GoogleMap object (theMap) in MainActivity.Then I start a new Activity called GetDirections and it starts a new Class called RequestDirection(Non-Activity).The class RequestDirection performs Google Directions API request and parses the JSON result to create a PolylineOptions object. 如此..:我在MainActivity中创建了一个GoogleMap对象(theMap)。然后我启动了一个名为GetDirections的新Activity,并启动了一个名为RequestDirection(Non-Activity)的新类。RequestDirection类执行Google Directions API请求并解析JSON结果创建一个PolylineOptions对象。 So HOW can i access the GoogleMap object (theMap) to add the polyline??? 那么我如何才能访问GoogleMap对象(theMap)以添加折线呢??? Example code: 示例代码:

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)
}

}

Note that all 3 classes are NOT nested.they are separate files 请注意,所有3个类都不嵌套,它们是单独的文件

I could use mainActivity context and pass it to a new class but now i start a new Activity and i istantiate the class from there. 我可以使用mainActivity上下文并将其传递给新的类,但是现在我开始一个新的Activity并从那里进行类化。

What can i do?? 我能做什么?? I wasted a whole day and no solution.. 我浪费了一整天,没有解决办法。


Added the exceptions for dst suggestion: 添加了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

You should probably return the polyline as a result of your GetDirections activity. 由于您的GetDirections活动,您可能应该返回折线。 You can then add the polyline from within the MainActivity . 然后,您可以从MainActivity添加折线。 That way your UI is only updated from the enclosing Activity , and not from another Activity (which would be against Androids code style, although it might be possible). 这样,您的UI仅从封闭的Activity更新,而不是从另一个Activity (虽然可能会违反Android的代码样式)。

If you know that your MainActivity is always going to come first, a simple way to allow access to the GoogleMap object is to make is static . 如果您知道MainActivity永远是第一位的,那么允许访问GoogleMap对象的一种简单方法就是使它为static For instance instead of public GoogleMap theMap put public static GoogleMap theMap . 例如,将public static GoogleMap theMap代替了public GoogleMap theMap public static GoogleMap theMap Then from any other class you can call MainActivity.theMap to access that object. 然后,可以从任何其他类调用MainActivity.theMap来访问该对象。

If the situation gets more complicated there are other better ways to do it, but if it's as simple as just getting the object, this way will definitely work. 如果情况变得更加复杂,则可以使用其他更好的方法来完成,但是如果只是获取对象那么简单,则此方法肯定会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM