我是android的新手,我想实现动作栏滑动,我阅读教程并写下这个。在程序中,我使用了framelayout,framelayout填充了mapsforge,但是运行时的progrma有错误。为什么?如果我删除片段和事务片段,该程序运行良好。
public class ShowSlidingMenu extends FragmentActivity {
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle actionBarDrawerToggle;
private ListView listView;
private CharSequence mDrawerTitle;
// used to store app title
private CharSequence mTitle;
// slide menu items
private String[] navMenuTitles;
private TypedArray navMenuIcons;
private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;
MapView mapView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sliding);
Fragment fragment=new MapFragmentTest();
FragmentTransaction ft= getSupportFragmentManager().beginTransaction();
ft.replace(R.id.mapView, fragment).commit();
mTitle = mDrawerTitle = getTitle();
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
navMenuIcons = getResources()
.obtainTypedArray(R.array.nav_drawer_icons);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
listView = (ListView) findViewById(R.id.list_slidermenu);
navDrawerItems = new ArrayList<NavDrawerItem>();
navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons
.getResourceId(0, -1)));
// Find People
navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons
.getResourceId(1, -1)));
// Photos
navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons
.getResourceId(2, -1)));
// Communities, Will add a counter here
navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons
.getResourceId(3, -1)));
// Pages
navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons
.getResourceId(4, -1)));
// What's hot, We will add a counter here
navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons
.getResourceId(5, -1)));
navMenuIcons.recycle();
adapter = new NavDrawerListAdapter(getApplicationContext(),
navDrawerItems);
listView.setAdapter(adapter);
// enabling action bar app icon and behaving it as toggle button
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
actionBarDrawerToggle = new ActionBarDrawerToggle(
this, drawerLayout, R.drawable.ic_drawer,
R.string.app_name, R.string.app_name) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
};
drawerLayout.setDrawerListener(actionBarDrawerToggle);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
displayView();
}
});
}
//remove onCreateOptionsMenu()
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if(actionBarDrawerToggle.onOptionsItemSelected(item))
return true;
//remove action bar settings
return super.onOptionsItemSelected(item);
}
//remove onPrepareOptionsMenu()
@Override
public void setTitle(CharSequence title) {
// TODO Auto-generated method stub
mTitle=title;
getActionBar().setTitle(mTitle);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
actionBarDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
actionBarDrawerToggle.onConfigurationChanged(newConfig);
}
public void displayView(){
Toast.makeText(getApplicationContext(), "hahaha", Toast.LENGTH_LONG).show();
}
}
xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="@+id/mainView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF" >
<FrameLayout
android:id="@+id/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></FrameLayout>
</RelativeLayout>
<ListView
android:id="@+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/list_background"
android:choiceMode="singleChoice"
android:divider="@color/list_divider"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector" />
</android.support.v4.widget.DrawerLayout>
fragment_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<org.mapsforge.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
MapFragmentTest
public class MapFragmentTest extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_layout, container, false);
return rootView;
}
}
日志:
01-27 09:08:48.185: E/AndroidRuntime(13176): FATAL EXCEPTION: main
01-27 09:08:48.185: E/AndroidRuntime(13176): java.lang.RuntimeException: Unable to start activity ComponentInfo {org.example.sendandreceivesms/org.example.slidingmenubar.ShowSlidingMenu}: android.view.InflateException: Binary XML file line #6: Error inflating class org.mapsforge.android.maps.MapView
答案 0 :(得分:0)
更改
<org.mapsforge.android.maps.MapView
到
<com.google.android.gms.maps.MapView
由于您在评论中提到您使用mapsforge库,请按照
http://code.google.com/p/mapsforge/wiki/GettingStartedMapView
答案 1 :(得分:0)
您是否已在manifest.xml文件中声明了显示地图所需的权限。
根据您应添加的文档 -
The map library requires only one permission for writing cached images to the external storage (typically the phones SD card). Add the following line to your applications AndroidManifest.xml file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
并使用下面的代码显示地图 -
import android.os.Bundle;
import java.io.File;
import org.mapsforge.android.maps.MapActivity;
import org.mapsforge.android.maps.MapView;
public class HelloMapView extends MapActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MapView mapView = new MapView(this);
mapView.setClickable(true);
mapView.setBuiltInZoomControls(true);
mapView.setMapFile(new File("/sdcard/path/to/mapfile.map"));
setContentView(mapView);
}
}
答案 2 :(得分:0)
我有类似的错误消息,我已经修复了以下内容:
mapsforge-core-0.5.0.jar
,mapsforge-map-0.5.0.jar
,mapsforge-map-android-0.5.0.jar
,mapsforge-map-reader-0.5.0.jar
)并在android java构建路径配置中检查(项目&gt;属性&gt ; Java构建路径&gt;订单和导出)AndroidGraphicFactory.createInstance(this.getApplication());
中
如果未添加,则会根据documentation 希望以上有助于:)