0

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.

Problem Statement:-

But whenever I try to run this below program, my application always gets 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 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>
arsenal
  • 23,366
  • 85
  • 225
  • 331

2 Answers2

1

Google MapView always require the use of MapActivity. You can use a ListView without a ListActivity to achieve your goal of a list with a map.

Edit:

See SO question: ListView without ListActivity

Community
  • 1
  • 1
Morrison Chang
  • 11,691
  • 3
  • 41
  • 77
0

So Android has this rule which basically goes like this:

MapViews can only be created inside instances of MapActivity

I'm sure something like this must be printed in your logcat error messages.

What this simply means is that due to Java's lack of support for multiple inheritance you can't extend two different Activities.

A Random Android Dev
  • 2,691
  • 1
  • 18
  • 17