请帮助:android.view.InflateException:二进制 XML 文件第 3 行:错误膨胀类 com.google.android.maps.MapView
我编写了 Get Point 应用程序 (https ://market.android.com/details?id=jv.android.getpoint&feature=search_result) 来存储 GPS 坐标并导出要在 Google 地球中打开的 KML 文件。创建该应用程序是因为我需要学习 Android 平台。
因为我是 Android 新手,如果我问一个虚拟问题,请原谅我。 :)
Google 标记的开发者控制台显示有一个错误,我无法在模拟器中自行重现(使用 Google APU、Galaxy S 或 Galaxy 5 :(
错误消息为:android.view.InflateException: Binary 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 类: 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;
}
}
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 时显示的上下文菜单将调用显示地图的选项。自定义适配器(突出显示的行显示在堆栈中):
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
任何帮助将不胜感激。
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.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
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题的核心位于堆栈跟踪的最底部:
MapViews 只能在 MapActivity 的实例内创建
。基本上,无论出于何种原因,通货膨胀都是在您的观点之外发生的。jv.android.getpoint.ListaCoordAdapter
是您的课程之一吗?从堆栈跟踪来看,这是试图膨胀布局的类。如果这个类是你的,它会扩展 MapActivity 吗?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?我通过扩展 MapActivity 而不是调用 MapView 的活动类中的 Activity 来修复它
I fixed it by extend MapActivity instead of Activity in the activity class that calls the MapView