简体   繁体   English

Android在Google地图上强制关闭

[英]Android Forced Close on the Google Maps

I am trying to show Google Map on the Top Half of the Android Screen, And in the Bottom Half I am trying to create List View dynamically . 我试图在Android屏幕的Top Half部分显示Google Map ,而在Bottom Half分则尝试List View dynamically创建List View dynamically

Problem Statement:- 问题陈述:-

But whenever I try to run this below program, my application always gets forced closed. 但是,每当我尝试运行以下程序时,我的应用程序总是被forced closed.

And the exception that I always get is- 我总是得到的例外是-

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testproject/com.example.testproject.MyTwoListItemsActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class com.google.android.maps.MapView

Below is my code- 以下是我的代码-

public class MyTwoListItemsActivity extends ListActivity {
private ListView mListView;
private MapView mapView;
private MapController mapController;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mapView = (MapView) findViewById(R.id.mapView);
    mListView = (ListView) findViewById(R.id.mylist);
    mapView.setStreetView(true);
    mapView.setBuiltInZoomControls(true);

    mapController = mapView.getController();
    mapController.setZoom(15);

    ArrayList<Map<String, String>> list = buildData();
    String[] from = { "name", "purpose" };
    int[] to = { android.R.id.text1, android.R.id.text2 };

    SimpleAdapter adapter = new SimpleAdapter(this, list,
            android.R.layout.simple_list_item_2, from, to);
    mListView.setAdapter(adapter);
}

private ArrayList<Map<String, String>> buildData() {
    ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
    list.add(putData("Android", "Mobile"));
    list.add(putData("Windows7", "Windows7"));
    list.add(putData("iPhone", "iPhone"));
    return list;
}

private HashMap<String, String> putData(String name, String purpose) {
    HashMap<String, String> item = new HashMap<String, String>();
    item.put("name", name);
    item.put("purpose", purpose);
    return item;
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    String item = (String) getListAdapter().getItem(position);
    Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();
}

} 

And this is my XML file- 这是我的XML文件-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <com.google.android.maps.MapView
        android:id="@+id/mapView"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:apiKey="0s_fADEBtq0-j_teQ1j-yaoDAivoHHtwN81rJ-g"
        android:clickable="true"
        android:state_enabled="true" />

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <ListView
            android:id="@+id/mylist"
            android:layout_width="164dp"
            android:layout_height="142dp"
            android:layout_weight="0.33" >
        </ListView>
    </LinearLayout>

</LinearLayout>

Google MapView always require the use of MapActivity. Google MapView始终要求使用MapActivity。 You can use a ListView without a ListActivity to achieve your goal of a list with a map. 您可以使用没有ListActivity的ListView来实现带有地图的列表的目标。

Edit: 编辑:

See SO question: ListView without ListActivity 看到这样的问题: 没有ListActivity的ListView

So Android has this rule which basically goes like this: 因此,Android具有以下规则,基本上是这样的:

MapViews can only be created inside instances of MapActivity

I'm sure something like this must be printed in your logcat error messages. 我确定必须在您的logcat错误消息中打印出类似的内容。

What this simply means is that due to Java's lack of support for multiple inheritance you can't extend two different Activities. 这只是意味着,由于Java缺乏对多重继承的支持,因此您无法扩展两个不同的Activity。

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

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