Google地图上的Android Force关闭

时间:2012-08-19 02:38:32

标签: java android android-listview

我正在尝试在Android屏幕的Google Map上显示Top Half,并在Bottom Half我正在尝试创建List View dynamically

问题陈述: -

但每当我尝试运行以下程序时,我的应用程序总是得到forced closed.

我总是得到的例外是 -

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

以下是我的代码 -

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();
}

} 

这是我的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>

2 个答案:

答案 0 :(得分:1)

Google MapView始终需要使用MapActivity。您可以使用不带ListActivity的ListView来实现带有地图的列表的目标。

编辑:

请参阅问题:ListView without ListActivity

答案 1 :(得分:0)

所以Android有这个规则基本上是这样的:

MapViews can only be created inside instances of MapActivity

我确定必须在您的logcat错误消息中打印出类似这样的内容。

这只是意味着由于Java缺乏对多重继承的支持,您无法扩展两个不同的活动。