我正在尝试使用SherlockActivtiyBar在片段内开发Google Android Map v2。
我有这个错误:
04-12 10:01:26.830: E/AndroidRuntime(1089): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.it.michele.ab.map/it.michele.prova.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
你能帮助我吗?
我做错了什么?
这是我的Eclipse项目:
http://www.michelepierri.it/project.zip
这是我的代码:
SherlockMapFragment.java
package com.actionbarsherlock.app;
import android.app.Activity;
import android.support.v4.app.Watson.OnCreateOptionsMenuListener;
import android.support.v4.app.Watson.OnOptionsItemSelectedListener;
import android.support.v4.app.Watson.OnPrepareOptionsMenuListener;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.internal.view.menu.MenuItemWrapper;
import com.actionbarsherlock.internal.view.menu.MenuWrapper;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.google.android.gms.maps.SupportMapFragment;
public class SherlockMapFragment extends SupportMapFragment implements OnCreateOptionsMenuListener, OnPrepareOptionsMenuListener, OnOptionsItemSelectedListener {
private SherlockFragmentActivity mActivity;
public SherlockFragmentActivity getSherlockActivity() {
return mActivity;
}
@Override
public void onAttach(Activity activity) {
if (!(activity instanceof SherlockFragmentActivity)) {
throw new IllegalStateException(getClass().getSimpleName() + " must be attached to a SherlockFragmentActivity.");
}
mActivity = (SherlockFragmentActivity)activity;
super.onAttach(activity);
}
@Override
public void onDetach() {
mActivity = null;
super.onDetach();
}
@Override
public final void onCreateOptionsMenu(android.view.Menu menu, android.view.MenuInflater inflater) {
onCreateOptionsMenu(new MenuWrapper(menu), mActivity.getSupportMenuInflater());
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
//Nothing to see here.
}
@Override
public final void onPrepareOptionsMenu(android.view.Menu menu) {
onPrepareOptionsMenu(new MenuWrapper(menu));
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
//Nothing to see here.
}
@Override
public final boolean onOptionsItemSelected(android.view.MenuItem item) {
return onOptionsItemSelected(new MenuItemWrapper(item));
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
//Nothing to see here.
return false;
}
}
MainActivity.java
package it.michele.prova;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import android.os.Bundle;
import android.app.Activity;
//import android.view.Menu;
import com.actionbarsherlock.view.*;
import com.example.it.michele.ab.map.R;
import com.example.it.michele.ab.map.R.layout;
import com.example.it.michele.ab.map.R.menu;
//import com.google.android.gms.maps.*;
public class MainActivity extends SherlockFragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, (android.view.Menu) menu);
return true;
}
}
activity_main.xml中
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<fragment
android:id="@+id/fragment_map"
android:name=".ui.fragments.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:tag="tag_fragment_map" />
<fragment
android:id="@+id/fragment_help"
android:name=".ui.fragments.HelpFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:tag="tag_fragment_help" />
</FrameLayout>
答案 0 :(得分:0)
通常情况下,如果您在使用地图时收到fragment/class not found error
,那么您的问题可能是缺少google-play-services
库的良好参考,因为MapFragment
/ SupportMapFragment
是这个图书馆:
检查您的Android Dependencies
文件夹(blue)
是否在google-play-services
(orange)
内,您会发现com.google.android.gms.maps
包(green)
包含{MapFragment
1}} class (red)
:
如果不是这种情况,您可以查看我写的这篇博客文章的前3个步骤,以了解如何正确引用此软件包:
答案 1 :(得分:0)
我花了好几天试图解决这个问题。在我的情况下,我没有在我的Android清单中引用打开的GL es v2 - 一旦我把它放在那里,错误就消失了,我的地图出现了。添加声明 如果它还没有在你的机器人清单权限中 - 那可能会为你解决它。