Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android被迫关闭了谷歌地图_Java_Android_Android Listview - Fatal编程技术网

Java Android被迫关闭了谷歌地图

Java Android被迫关闭了谷歌地图,java,android,android-listview,Java,Android,Android Listview,我试图在Android屏幕的上半部分显示谷歌地图,在下半部分我试图动态创建列表视图 问题陈述:- 但每当我尝试运行下面的程序时,我的应用程序总是被强制关闭。 我经常遇到的例外是- java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testproject/com.example.testproject.MyTwoListItemsActivity}: android.view.Inflate

我试图在Android屏幕的
上半部分显示
谷歌地图
,在
下半部分
我试图动态创建
列表视图

问题陈述:-

但每当我尝试运行下面的程序时,我的应用程序总是被强制关闭。

我经常遇到的例外是-

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

} 
公共类MyTwoListItemsActivity扩展了ListActivity{
私有列表视图;
私有地图视图;
专用地图控制器;
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mapView=(mapView)findViewById(R.id.mapView);
mListView=(ListView)findViewById(R.id.mylist);
mapView.setStreetView(真);
mapView.SetBuilTinZoomControl(真);
mapController=mapView.getController();
mapController.setZoom(15);
ArrayList=buildData();
字符串[]from={“name”,“purpose”};
int[]to={android.R.id.text1,android.R.id.text2};
SimpleAdapter=新SimpleAdapter(此,列表,
android.R.layout.simple_list_item_2,from,to);
mListView.setAdapter(适配器);
}
私有ArrayList buildData(){
ArrayList=新建ArrayList();
添加(putData(“安卓”、“移动”);
添加(putData(“Windows7”、“Windows7”));
添加(putData(“iPhone”、“iPhone”);
退货清单;
}
私有HashMap putData(字符串名称、字符串用途){
HashMap项=新建HashMap();
项目。填写(“名称”,名称);
项目.付诸表决(“目的”,目的);
退货项目;
}
@凌驾
受保护的void onListItemClick(列表视图l、视图v、整数位置、长id){
字符串项=(字符串)getListAdapter().getItem(位置);
Toast.makeText(此项+选定项),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>

Google MapView始终要求使用MapActivity。您可以在不使用ListActivity的情况下使用ListView来实现使用地图创建列表的目标

编辑:


请看问题:

安卓有这样一条规则,基本上是这样的:

MapViews can only be created inside instances of MapActivity
我确信类似的东西一定会印在你的logcat错误信息中


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

谢谢Morrison,您能给我举个例子说明如何在没有ListActivity的情况下实现相同的活动吗。