1

我有一个简单的 Android 活动,它应该在地图视图和文本形式上呈现一些状态。

我已经完成了这两个布局,我用 setContentView() 在它们之间交换。开始时它工作正常,显示地图。然后我可以通过按菜单按钮并选择文本视图来切换到文本状态视图。但是当我想换回映射 setContentView(R.layout.main); (该布局仅包含 MapView 和线性布局)引发以下异常:

android.view.InflateException: Binary XML file line #7:
   Error inflating class <unknown>

有人有什么想法吗?我对 Android 开发很陌生,所以我不确定这是解决问题的最佳方法。

来源清单:

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

}

主.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>

显现:

<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>
4

1 回答 1

1

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);
于 2011-01-02T15:19:34.123 回答