请帮助:android.view.InflateException:二进制XML文件行#3:错误膨胀类com.google.android.maps.MapView

时间:2011-09-03 12:26:23

标签: android maps inflate mapactivity

我编写了Get Point应用程序(https://market.android.com/details?id=jv.android.getpoint&feature=search_result)来存储GPS坐标并导出要在Google Earth中打开的KML文件。该应用程序的创建是因为我需要学习Android平台。

因为我是Android上的新手,请原谅我,如果我要求一个虚拟问题。 :)

Google Marked Developer Console向我展示了一个错误,我无法在模拟器中自行复制(使用Google APU,Galaxy S或Galaxy 5 :(

错误消息为: android.view.InflateException:二进制XML文件行#3:错误导致类com.google.android.maps.MapView

我一直在寻找解决方案,但找不到答案:(

筹码在这里:

 at android.view.InflateException: Binary XML file line #3: Error inflating class com.google.android.maps.MapView
 at android.view.LayoutInflater.createView(LayoutInflater.java:513)
 at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
 at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
 at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
 at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
 at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
 at jv.android.getpoint.ListaCoordAdapter.getView(ListaCoordAdapter.java:29)
 at android.widget.AbsListView.obtainView(AbsListView.java:1315)
 at android.widget.ListView.makeAndAddView(ListView.java:1727)
 at android.widget.ListView.fillDown(ListView.java:652)
 at android.widget.ListView.fillFromTop(ListView.java:709)
 at android.widget.ListView.layoutChildren(ListView.java:1580)
 at android.widget.AbsListView.onLayout(AbsListView.java:1147)
 at android.view.View.layout(View.java:7035)
 at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
 at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
 at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
 at android.view.View.layout(View.java:7035)
 at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
 at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
 at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
 at android.view.View.layout(View.java:7035)
 at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
 at android.view.View.layout(View.java:7035)
 at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
 at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
 at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
 at android.view.View.layout(View.java:7035)
 at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
 at android.view.View.layout(View.java:7035)
 at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
 at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
 at android.os.Handler.dispatchMessage(Handler.java:99)
 at android.os.Looper.loop(Looper.java:123)
 at android.app.ActivityThread.main(ActivityThread.java:4627)
 at java.lang.reflect.Method.invokeNative(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:521)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
 at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
 at com.google.android.maps.MapView.<init>(MapView.java:238)
 at java.lang.reflect.Constructor.constructNative(Native Method)
 at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
 at android.view.LayoutInflater.createView(LayoutInflater.java:500)
 ... 39 more
Caused by: java.lang.IllegalArgumentException: MapViews can only be created inside instances of MapActivity.
 at com.google.android.maps.MapView.<init>(MapView.java:282)
 at com.google.android.maps.MapView.<init>(MapView.java:255)
 ... 43 more

主要活动有一个菜单选项来调用MapActivity。 XML和代码:

启动地图:

            Intent intent = new Intent(GetPointActivity.this, PointView.class);

            intent.putExtra("latitude", latitude);
            intent.putExtra("longitude", longitude);
            startActivity(intent);

PointView类:

public class PointView extends MapActivity {

    MapView mv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.maps);

        mv = (MapView)findViewById(R.id.mvPosicao);
        mv.setLongClickable(true);
        mv.setBuiltInZoomControls(true);

        Intent intent = getIntent();

        if (intent != null)
        {
            Bundle params = intent.getExtras();

            if (params != null) {
                Double lat = (Double) params.getDouble("latitude");
                Double lon = (Double) params.getDouble("longitude");

                markPosition (lat, lon);
            }
        }

        registerForContextMenu(mv);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu  menu) {            
        //Log.i("MYINFO", "I'm in");    
        MenuItem mi0 = menu.add(Menu.NONE, 0, 0, getString(R.string.mnVerMapa));
        mi0.setIcon(R.drawable.mapview);

        MenuItem mi1 = menu.add(Menu.NONE, 1, 1, getString(R.string.mnVerSatelite));
        mi1.setIcon(R.drawable.satelliteview);

        MenuItem mi2 = menu.add(Menu.NONE, 2, 2, getString(R.string.mnVerStreetView));
        mi2.setIcon(R.drawable.streetview);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {       
        switch (item.getItemId()) {
        case 0:
            mv.setSatellite(false);
            break;
        case 1: 
            mv.setSatellite(true);
            break;
        case 2: 
            mv.setStreetView(true);
            break;
        }

        return true;
    }    

    public void markPosition (Double lat, Double lon){
        int latE6 = (int)(lat*1E6);
        int lonE6 = (int)(lon*1E6);

        GeoPoint point = new GeoPoint(latE6, lonE6);

        ImageOverlay io = new ImageOverlay(new GeoPoint(latE6, lonE6), R.drawable.waypoint2);

        mv.getOverlays().add(io);
        mv.getController().setCenter(point);
    }   

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }
}

PointView XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent">
  <com.google.android.maps.MapView       
    android:id="@+id/mvPosicao"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:enabled="true"
    android:clickable="true"
    android:apiKey="<my googlemaps key" />
</LinearLayout>

我的宣言:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="<package>.getpoint"
      android:versionCode="1"
      android:versionName="1.0.1">
    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"></uses-permission>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>

    <application android:label="@string/app_name" android:icon="@drawable/maps">
        <uses-library android:name="com.google.android.maps" />
        <activity android:name=".GetPointActivity" android:label="@string/app_name" android:configChanges="keyboard|keyboardHidden|orientation">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".PointView" android:configChanges="keyboard|keyboardHidden|orientation" android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar"/>
                </application>
</manifest>

当用户长按ListView时,显示的上下文菜单会调用显示地图的选项。 ListView有一个自定义适配器(突出显示的行显示在堆栈中):

public class ListaCoordAdapter extends ArrayAdapter<CoordToShow> {

    private List<CoordToShow> items;        
    Context context;

    public ListaCoordAdapter(Context context, int textViewResourceId, List<CoordToShow> items) {                
        super(context, textViewResourceId, items);                
        this.items = items;
        this.context = context;
    }        

    @Override        
    public View getView(int position, View convertView, ViewGroup parent) {                
        View v = convertView;                

        if (v == null) {                        
            LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);                
            **v = vi.inflate(R.layout.rowcoord, null);**                
        }                

        CoordToShow it = items.get(position);                
        if (it != null) {                        
            ImageView iv = (ImageView) v.findViewById(R.id.ivCoord);
            TextView l1 = (TextView) v.findViewById(R.id.linha1);
            TextView l2 = (TextView) v.findViewById(R.id.linha2);

            l1.setText(it.getNome());
            l2.setText(it.toString());

            if (iv != null) {         
                if (it.getTipo() == CoordToShow.PONTO)                  
                    iv.setImageResource(R.drawable.waypoint2);
                else 
                    iv.setImageResource(R.drawable.track);
            }                
        }                

        return v;        
    }
}

rowcoord xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:paddingTop="4dip"
     android:paddingBottom="6dip"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content" android:orientation="horizontal">
    <ImageView android:src="@drawable/waypoint2" android:id="@+id/ivCoord" android:layout_width="32dp" android:layout_height="32dp"></ImageView>

     <LinearLayout
         android:id="@+id/ll2"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content" android:orientation="vertical">
         <TextView android:id="@+id/linha1"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:textAppearance="?android:attr/textAppearanceMedium" />
         <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/linha2"></TextView>
     </LinearLayout>
</LinearLayout>

在Default.Property中:

target=Google Inc.:Google APIs:8

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:4)

问题的核心位于堆栈跟踪的最底部:MapViews can only be created inside instances of MapActivity。基本上,无论出于何种原因,通货膨胀都是从PointView外部进行的。

jv.android.getpoint.ListaCoordAdapter是你的一个班级吗?从堆栈跟踪看,这是试图给布局充气的类。如果这个类是你的,它是否扩展了MapActivity?

答案 1 :(得分:1)

我通过在调用MapView

的活动类中使用扩展MapActivity而不是Activity来修复它