开发者

Please help: android.view.InflateException: Binary XML file line #3: Error inflating class com.google.android.maps.MapView

开发者 https://www.devze.com 2023-04-01 23:54 出处:网络
I wrote the Get Point application (https://market.android.com/details?id=jv.android.getpoint&feature=search_result) to store GPS coordinates and export a KML file to be opened in Google Earth. Tha

I wrote the Get Point application (https://market.android.com/details?id=jv.android.getpoint&feature=search_result) to store GPS coordinates and export a KML file to be opened in Google Earth. That application was created because I need to learn the Android platform.

Because I'm new on Android please forgive me if I'm asking for a dummy question. :)

The Google Marked Developer Console show me there is an error I can't reproduce by myself either in emulator (With Google APU , Galaxy S or Galaxy 5 :(

The error message is: android.view.InflateException: Binary XML file line #3: Error inflating class com.google.android.maps.MapView.

I was looking for a solution, but could not find an answer :(

The Stack is here:

 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.ja开发者_运维问答va: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

The main activity has an menu option to call the MapActivity. The XML and code:

To start the map:

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

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

The PointView class:

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

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

My Manifest:

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

The option to show the map is called by a context menu displayed when the user long press a ListView. That ListView has a custom Adapter (the highlighted line is displayed in the stack):

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

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

In Default.Property:

target=Google Inc.:Google APIs:8

Any help will be appreciated.


The core of your problem is at the very bottom of the stack trace: MapViews can only be created inside instances of MapActivity. Basically, the inflation is taking place from outside of your PointView for whatever reason.

Is jv.android.getpoint.ListaCoordAdapter one of your classes? It looks from the stack trace that this is the class that's trying to inflate the layout. If this class is yours, does it extend MapActivity?


I fixed it by extend MapActivity instead of Activity in the activity class that calls the MapView

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号