来自非活动类的意图

时间:2012-06-05 12:40:55

标签: android android-intent android-activity

在我的android应用程序中我正在使用一个库,在这里有一个非活动类来自我想要的意图,如何做到这一点

我正在使用这个库

https://github.com/galex/android-mapviewballoons

这里是我想要的代码片段,这个函数是在库中调用的

    protected boolean onBalloonTap(int index, OverlayItem item) {
    Toast.makeText(c, "clicked pin is :  " + item.getTitle(),
            Toast.LENGTH_SHORT).show();



    Intent intent = new Intent(firstActivity.class,secondActivity.class);
    startActivity(intent);
    //there is no startActivity method here
    return true;
}

这里是logcat

06-05 18:06:19.350: D/dalvikvm(370): GC_EXTERNAL_ALLOC freed 72K, 49% free 2790K/5379K, external 2064K/2137K, paused 74ms
06-05 18:06:19.709: D/dalvikvm(370): GC_EXTERNAL_ALLOC freed 34K, 47% free 2885K/5379K, external 2300K/2686K, paused 50ms
06-05 18:06:20.240: D/dalvikvm(370): GC_EXTERNAL_ALLOC freed 6K, 47% free 2884K/5379K, external 3730K/4658K, paused 72ms
06-05 18:06:42.919: D/PhoneWindow(370): couldn't save which view has focus because the focused view com.android.internal.policy.impl.PhoneWindow$DecorView@40538220 has no id.
06-05 18:06:44.569: D/dalvikvm(370): GC_EXTERNAL_ALLOC freed 54K, 46% free 3022K/5511K, external 5867K/6444K, paused 47ms
06-05 18:06:47.969: D/dalvikvm(370): GC_CONCURRENT freed 831K, 49% free 3472K/6727K, external 7393K/8076K, paused 5ms+7ms
06-05 18:06:48.829: I/MapActivity(370): Handling network change notification:CONNECTED
06-05 18:06:48.829: E/MapActivity(370): Couldn't get connection factory client
06-05 18:06:49.220: D/dalvikvm(370): GC_CONCURRENT freed 1748K, 53% free 3827K/8007K, external 7777K/8076K, paused 6ms+7ms
06-05 18:06:51.919: D/AndroidRuntime(370): Shutting down VM
06-05 18:06:51.919: W/dalvikvm(370): threadid=1: thread exiting with uncaught exception (group=0x40015560)
06-05 18:06:51.949: E/AndroidRuntime(370): FATAL EXCEPTION: main
06-05 18:06:51.949: E/AndroidRuntime(370): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
06-05 18:06:51.949: E/AndroidRuntime(370):  at android.app.ContextImpl.startActivity(ContextImpl.java:624)
06-05 18:06:51.949: E/AndroidRuntime(370):  at android.content.ContextWrapper.startActivity(ContextWrapper.java:258)
06-05 18:06:51.949: E/AndroidRuntime(370):  at com.project.ennis.Maps.SimpleItemizedOverlay.onBalloonTap(SimpleItemizedOverlay.java:80)
06-05 18:06:51.949: E/AndroidRuntime(370):  at com.readystatesoftware.mapviewballoons.BalloonItemizedOverlay$2.onTouch(BalloonItemizedOverlay.java:231)
06-05 18:06:51.949: E/AndroidRuntime(370):  at android.view.View.dispatchTouchEvent(View.java:3881)
06-05 18:06:51.949: E/AndroidRuntime(370):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:903)
06-05 18:06:51.949: E/AndroidRuntime(370):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
06-05 18:06:51.949: E/AndroidRuntime(370):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
06-05 18:06:51.949: E/AndroidRuntime(370):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
06-05 18:06:51.949: E/AndroidRuntime(370):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
06-05 18:06:51.949: E/AndroidRuntime(370):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
06-05 18:06:51.949: E/AndroidRuntime(370):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
06-05 18:06:51.949: E/AndroidRuntime(370):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
06-05 18:06:51.949: E/AndroidRuntime(370):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1691)
06-05 18:06:51.949: E/AndroidRuntime(370):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1125)
06-05 18:06:51.949: E/AndroidRuntime(370):  at android.app.Activity.dispatchTouchEvent(Activity.java:2096)
06-05 18:06:51.949: E/AndroidRuntime(370):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1675)
06-05 18:06:51.949: E/AndroidRuntime(370):  at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2194)
06-05 18:06:51.949: E/AndroidRuntime(370):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1878)
06-05 18:06:51.949: E/AndroidRuntime(370):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-05 18:06:51.949: E/AndroidRuntime(370):  at android.os.Looper.loop(Looper.java:130)
06-05 18:06:51.949: E/AndroidRuntime(370):  at android.app.ActivityThread.main(ActivityThread.java:3683)
06-05 18:06:51.949: E/AndroidRuntime(370):  at java.lang.reflect.Method.invokeNative(Native Method)
06-05 18:06:51.949: E/AndroidRuntime(370):  at java.lang.reflect.Method.invoke(Method.java:507)
06-05 18:06:51.949: E/AndroidRuntime(370):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-05 18:06:51.949: E/AndroidRuntime(370):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-05 18:06:51.949: E/AndroidRuntime(370):  at dalvik.system.NativeStart.main(Native Method)
06-05 18:06:53.749: I/Process(370): Sending signal. PID: 370 SIG: 9
06-05 18:06:55.140: D/dalvikvm(383): GC_EXTERNAL_ALLOC freed 55K, 49% free 2762K/5379K, external 2102K/2137K, paused 80ms

2 个答案:

答案 0 :(得分:3)

Context对象从您的Activity传递到非活动类

contextObj.startActivity(intent);

答案 1 :(得分:2)

使用我认为你在那个班级有上下文.....

 Intent intent = new Intent(c,secondActivity.class);
  intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//it you are already adding other
 c.startActivity(intent); 

正在使用

Toast.makeText(c,"点击图钉是:" + item.getTitle(),             Toast.LENGTH_SHORT).show();