0

我正在尝试在 Android 屏幕上显示Google MapTop 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>
4

2 回答 2

1

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

编辑:

请参阅 SO 问题:没有 ListActivity 的 ListView

于 2012-08-19T02:57:35.360 回答
0

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

MapViews can only be created inside instances of MapActivity

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

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

于 2012-08-19T02:58:28.267 回答