Reputation: 326
I have a simple Android activity that should present some status both on a mapview and in textual form.
I have done this as two layouts that i swap between with setContentView(). At start it works fine, display the map. I can then swap to the textstatus view by pressing menu button and selecting Text view. But when I want to swap back to map the setContentView(R.layout.main); (that layout holds only a MapView and a linear layout) throws the following exception:
android.view.InflateException: Binary XML file line #7:
Error inflating class <unknown>
Anyone got any ideas? I am quite new to Android development so I dont know for sure that this is the best way to solve the problem.
Source listing:
maptest.java:
public class maptest extends MapActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
MapController mapController = mapView.getController();
mapController.setCenter(new GeoPoint((int)(65.319416667*1000000),
(int)(18.073833333*1000000)));
mapController.setZoom(8);
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
//@Override
protected boolean isRouteDisplayed() {
return false;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.map_view:
try{
setContentView(R.layout.main);
}
catch (Exception e) {
System.out.println("Error");
System.out.println(e.toString());
}
return true;
case R.id.text_view:
setContentView(R.layout.statustext);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
statustext.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:id="@+id/TextView01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Status text"></TextView>
</LinearLayout>
manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.csr.maptest"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".maptest"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</manifest>
menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/text_view"
android:title="Text view" />
<item android:id="@+id/map_view"
android:title="Map view" />
</menu>
Upvotes: 1
Views: 1725
Reputation: 3240
Did you miss something in the Main.xml? I can't see the
<com.google.android.maps.MapView ... />
and you didn't close the LinearLayout.
However, the problem is that you are only allowed to instantiate a single MapView in a MapActivity. I think, that everytime you do
setContentView(R.layout.main);
your mapview will be inflated and MapView is instantiated. So, you may better create the MapView in Code and take care, that you instantiate it only once:
if (mapView == null)
mapView = new MapView(this, "your-api-key");
and
findViewById(R.id.LinearLayout01).addView(mapView);
Upvotes: 1