我正在尝试创建地图应用程序,我需要执行与Google地图相似的操作。 我想打开显示位置列表,当用户点击它们时,我想显示一个对话框。
我能够做到这里。但是现在当用户点击打开的对话框时,我想要显示另一个具有他们点击的项目详情的动作。
我试图开始一个新的意图,但它给了我一个错误。
这就是我正在做的事情
private void drawPopupWindow(Canvas canvas,int index, MapView mapView, boolean shadow) {
OverlayItem item = mOverlays.get(index);
GeoPoint geoPoint = item.getPoint();
LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout dialogLayout = (LinearLayout)inflater.inflate(R.layout.location_dialog, null);
LayoutParams mapDialogParams = new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
geoPoint, -1, -22,
LayoutParams.BOTTOM_CENTER);
mapView.addView(dialogLayout, mapDialogParams);
dialogLayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent detailIntent = new Intent(mContext, ItemDetailView.class);
mContext.startActivity(detailIntent); // I passed the context from mapActivity
}
});
}
这是堆栈跟踪
> E/AndroidRuntime( 4985):
> java.lang.RuntimeException: Unable to
> start activity
> ComponentInfo{com.achie.test.mapssample/com.achie.test.mapssample.ItemDetailView}:
> android.view.InflateException: Binary
> XML file line #2: Error inflating
> class <unknown> E/AndroidRuntime(
> 4985): at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
> E/AndroidRuntime( 4985): at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
> E/AndroidRuntime( 4985): at
> android.app.ActivityThread.access$2200(ActivityThread.java:119)
> E/AndroidRuntime( 4985): at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
> E/AndroidRuntime( 4985): at
> android.os.Handler.dispatchMessage(Handler.java:99)
> E/AndroidRuntime( 4985): at
> android.os.Looper.loop(Looper.java:123)
> E/AndroidRuntime( 4985): at
> android.app.ActivityThread.main(ActivityThread.java:4363)
> E/AndroidRuntime( 4985): at
> java.lang.reflect.Method.invokeNative(Native
> Method) E/AndroidRuntime( 4985): at
> java.lang.reflect.Method.invoke(Method.java:521)
> E/AndroidRuntime( 4985): at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
> E/AndroidRuntime( 4985): at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
> E/AndroidRuntime( 4985): at
> dalvik.system.NativeStart.main(Native
> Method) E/AndroidRuntime( 4985):
> Caused by:
> android.view.InflateException: Binary
> XML file line #2: Error inflating
> class <unknown> E/AndroidRuntime(
> 4985): at
> android.view.LayoutInflater.createView(LayoutInflater.java:513)
> E/AndroidRuntime( 4985): at
> android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
> E/AndroidRuntime( 4985): at
> android.view.LayoutInflater.inflate(LayoutInflater.java:385)
> E/AndroidRuntime( 4985): at
> android.view.LayoutInflater.inflate(LayoutInflater.java:320)
> E/AndroidRuntime( 4985): at
> android.view.LayoutInflater.inflate(LayoutInflater.java:276)
> E/AndroidRuntime( 4985): at
> com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
> E/AndroidRuntime( 4985): at
> android.app.Activity.setContentView(Activity.java:1622)
> E/AndroidRuntime( 4985): at
> com.achie.test.mapssample.ItemDetailView.onCreate(ItemDetailView.java:11)
> E/AndroidRuntime( 4985): at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
> E/AndroidRuntime( 4985): at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
> E/AndroidRuntime( 4985): ... 11 more
> E/AndroidRuntime( 4985): Caused by:
> java.lang.reflect.InvocationTargetException
> E/AndroidRuntime( 4985): at
> com.google.android.maps.MapView.<init>(MapView.java:238)
> E/AndroidRuntime( 4985): at
> java.lang.reflect.Constructor.constructNative(Native
> Method) E/AndroidRuntime( 4985): at
> java.lang.reflect.Constructor.newInstance(Constructor.java:446)
> E/AndroidRuntime( 4985): at
> android.view.LayoutInflater.createView(LayoutInflater.java:500)
> E/AndroidRuntime( 4985): ... 20 more
> E/AndroidRuntime( 4985): Caused by:
> java.lang.IllegalArgumentException:
> MapViews can only be created inside
> instances of MapActivity.
> E/AndroidRuntime( 4985): at
> com.google.android.maps.MapView.<init>(MapView.java:282)
> E/AndroidRuntime( 4985): at
> com.google.android.maps.MapView.<init>(MapView.java:255)
> E/AndroidRuntime( 4985): ... 24 more
为什么我收到此错误,如何解决此问题并开启新活动?
另外,我在哪里可以找到Android上谷歌地图的源代码?
谢谢。
答案 0 :(得分:3)
当堆栈跟踪告诉您,“MapViews只能在MapActivity的实例中创建。”您正在尝试在MapView
之外创建MapActivity
。更改您的代码以避免这样做,您将清除此错误。