简体   繁体   中英

Accesing a MainActivity object from another class

I have a difficult situation with an android program im writing. 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. So HOW can i access the GoogleMap object (theMap) to add the polyline??? 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

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.

What can i do?? I wasted a whole day and no solution..


Added the exceptions for dst suggestion:

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. You can then add the polyline from within the 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).

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 . For instance instead of public GoogleMap theMap put public static GoogleMap theMap . Then from any other class you can call MainActivity.theMap to access that object.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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