我有一个简单的 Android Activity ,它应该在 map View 和文本形式中显示一些状态。
我已经完成了这两个布局,我使用 setContentView() 在它们之间进行了交换。一开始它工作正常,显示 map 。然后我可以通过按菜单按钮并选择 TextView 来切换到文本状态 View 。但是当我想换回映射 setContentView(R.layout.main); (该布局仅包含一个 MapView 和一个线性布局)抛出以下异常:
android.view.InflateException: Binary XML file line #7:
Error inflating class <unknown>
有人有什么想法吗?我是 Android 开发的新手,所以我不确定这是解决问题的最佳方法。
来源列表:
map 测试.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);
}
}
}
主.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
状态文本.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>
list :
<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>
菜单.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>
最佳答案
您是否遗漏了 Main.xml 中的某些内容?我看不到
<com.google.android.maps.MapView ... />
并且您没有关闭 LinearLayout。
但是,问题是您只能在 MapActivity 中实例化单个 MapView。 我想,每次你做
setContentView(R.layout.main);
您的 mapview 将被膨胀并且 MapView 被实例化。 因此,您最好在代码中创建 MapView 并注意只实例化一次:
if (mapView == null)
mapView = new MapView(this, "your-api-key");
和
findViewById(R.id.LinearLayout01).addView(mapView);
关于android - Inflate mapview 第二次抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4143909/