如何使用 MapView 制作 2 个分开的屏幕?
Posted
技术标签:
【中文标题】如何使用 MapView 制作 2 个分开的屏幕?【英文标题】:How to make 2 parted screen with MapView? 【发布时间】:2011-01-02 00:32:01 【问题描述】:我正在尝试使用 Google 地图制作屏幕。
此屏幕有 2 个部分。第一部分包括按钮 TextView 和 ListView 以及位于屏幕顶部的位置。第二部分包括 MapView(谷歌地图)和屏幕底部的位置。
如何制作?
我已经做到了。但没有运气。它抛出这个异常:
01-07 11:53:12.813: ERROR/androidRuntime(725): Uncaught handler: thread main exiting due to uncaught exception
01-07 11:53:12.833: ERROR/AndroidRuntime(725): java.lang.RuntimeException: Unable to start activity ComponentInfocom.mypackagename.uk/com.mypackagename.uk.MyCustomActivity: android.view.InflateException: Binary XML file line #6: Error inflating class java.lang.reflect.Constructor
01-07 11:53:12.833: ERROR/AndroidRuntime(725): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2268)
01-07 11:53:12.833: ERROR/AndroidRuntime(725): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
01-07 11:53:12.833: ERROR/AndroidRuntime(725): at android.app.ActivityThread.access$1800(ActivityThread.java:112)
01-07 11:53:12.833: ERROR/AndroidRuntime(725): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
01-07 11:53:12.833: ERROR/AndroidRuntime(725): at android.os.Handler.dispatchMessage(Handler.java:99)
01-07 11:53:12.833: ERROR/AndroidRuntime(725): at android.os.Looper.loop(Looper.java:123)
01-07 11:53:12.833: ERROR/AndroidRuntime(725): at android.app.ActivityThread.main(ActivityThread.java:3948)
01-07 11:53:12.833: ERROR/AndroidRuntime(725): at java.lang.reflect.Method.invokeNative(Native Method)
01-07 11:53:12.833: ERROR/AndroidRuntime(725): at java.lang.reflect.Method.invoke(Method.java:521)
01-07 11:53:12.833: ERROR/AndroidRuntime(725): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
01-07 11:53:12.833: ERROR/AndroidRuntime(725): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
01-07 11:53:12.833: ERROR/AndroidRuntime(725): at dalvik.system.NativeStart.main(Native Method)
01-07 11:53:12.833: ERROR/AndroidRuntime(725): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class java.lang.reflect.Constructor
01-07 11:53:12.833: ERROR/AndroidRuntime(725): at android.view.LayoutInflater.createView(LayoutInflater.java:512)
01-07 11:53:12.833: ERROR/AndroidRuntime(725): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:564)
01-07 11:53:12.833: ERROR/AndroidRuntime(725): at android.view.LayoutInflater.rInflate(LayoutInflater.java:617)
01-07 11:53:12.833: ERROR/AndroidRuntime(725): at android.view.LayoutInflater.parseInclude(LayoutInflater.java:701)
01-07 11:53:12.833: ERROR/AndroidRuntime(725): at android.view.LayoutInflater.rInflate(LayoutInflater.java:613)
01-07 11:53:12.833: ERROR/AndroidRuntime(725): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
01-07 11:53:12.833: ERROR/AndroidRuntime(725): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
01-07 11:53:12.833: ERROR/AndroidRuntime(725): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
01-07 11:53:12.833: ERROR/AndroidRuntime(725): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:309)
01-07 11:53:12.833: ERROR/AndroidRuntime(725): at android.app.Activity.setContentView(Activity.java:1626)
01-07 11:53:12.833: ERROR/AndroidRuntime(725): at com.mypackagename.uk.MyCustomActivity.onCreate(*MyCustomActivity.java:457*)
01-07 11:53:12.833: ERROR/AndroidRuntime(725): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
01-07 11:53:12.833: ERROR/AndroidRuntime(725): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
01-07 11:53:12.833: ERROR/AndroidRuntime(725): ... 11 more
01-07 11:53:12.833: ERROR/AndroidRuntime(725): Caused by: java.lang.reflect.InvocationTargetException
01-07 11:53:12.833: ERROR/AndroidRuntime(725): at com.google.android.maps.MapView.<init>(MapView.java:237)
01-07 11:53:12.833: ERROR/AndroidRuntime(725): at java.lang.reflect.Constructor.constructNative(Native Method)
01-07 11:53:12.833: ERROR/AndroidRuntime(725): at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
01-07 11:53:12.833: ERROR/AndroidRuntime(725): at android.view.LayoutInflater.createView(LayoutInflater.java:499)
01-07 11:53:12.833: ERROR/AndroidRuntime(725): ... 23 more
01-07 11:53:12.833: ERROR/AndroidRuntime(725): Caused by: java.lang.IllegalStateException: You are only allowed to have a single MapView in a MapActivity
01-07 11:53:12.833: ERROR/AndroidRuntime(725): at com.google.android.maps.MapActivity.setupMapView(MapActivity.java:180)
01-07 11:53:12.833: ERROR/AndroidRuntime(725): at com.google.android.maps.MapView.<init>(MapView.java:279)
01-07 11:53:12.833: ERROR/AndroidRuntime(725): at com.google.android.maps.MapView.<init>(MapView.java:254)
01-07 11:53:12.833: ERROR/AndroidRuntime(725): ... 27 more
我有抛出异常的代码行:MyCustomActivity.java:457
这一行是:setContentView(R.layout.my_layout);
我的 XML 是:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_
android:layout_
android:layout_gravity="top|center_vertical">
<com.mypackagename.uk.MyLayout
<TableLayout android:layout_
android:layout_>
<TableRow android:layout_ android:layout_ android:id="@+id/my_row1">
<TableLayout android:layout_
android:layout_ android:layout_weight="1">
<TableRow android:layout_marginTop="5dip" android:layout_ android:layout_>
<TextView android:layout_></TextView>
<ImageView android:layout_></ImageView>
<TextView android:layout_></TextView>
<TextView android:layout_ android:layout_></TextView>
<ImageView android:layout_></ImageView>
</TableRow>
</TableLayout>
</TableRow>
</TableLayout>
<com.google.android.maps.MapView
android:id="@+id/map_view_on_yardage"
android:layout_
android:layout_
android:enabled="true"
android:clickable="true"
android:apiKey="my-api-code">
</com.google.android.maps.MapView>
</com.mypackagename.uk.MyLayout>
【问题讨论】:
您需要在堆栈跟踪中进一步查看,以找到触发您在上面粘贴的行的异常。我建议您编辑此问题并粘贴到整个堆栈跟踪中,而不仅仅是这一行。 【参考方案1】:我也有问题..我确实喜欢这个... 创建一个可以接受两个片段的布局...... .. 在 top_fragment 放置您需要的任何内容...... 并在下面的片段上放置地图片段...
希望对你有用……
nested_frag.xml
<FrameLayout
android:id="@+id/top_content"
android:layout_
android:layout_
android:layout_weight="0.7"
android:background="#fff2ff" >
</FrameLayout>
<FrameLayout
android:id="@+id/map"
android:layout_
android:layout_
android:layout_weight="0.3"
android:background="#fff2ff" >
</FrameLayout>
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
View view = inflater.inflate(R.layout.nested_frag, container, false);
b = getArguments();
Fragment map = new MapFragment();
map.setArguments(b);
FragmentTransaction trans = getChildFragmentManager()
.beginTransaction();
trans.add(R.id.top_content, new HomeScreen());
trans.add(R.id.map, map).addToBackStack("somestack").commit();
return view;
【讨论】:
以上是关于如何使用 MapView 制作 2 个分开的屏幕?的主要内容,如果未能解决你的问题,请参考以下文章
当我缩放 mapView 以显示我的注释时,选定的注释部分不在屏幕上