我是Android的新手,我想实现Action Bar滑动,我阅读教程并编写此内容。在程序中,我使用了framelayout,即带有mapsforge的framelayout填充,但是运行时的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
您是否在subtest.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);
}
}
更改
<org.mapsforge.android.maps.MapView
to
<com.google.android.gms.maps.MapView
由于您在评论中提到您使用mapsforge库关注
http://code.google.com/p/mapsforge/wiki/gettingstartedmapview
我有类似的错误消息,我已经将其修复了以下内容:
- 添加所有JAR文件(包括
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构建路径配置(Project> Properties> Java构建路径> order Path> order and Export)中检查它
)
- 在地图之前将此行添加到类中,或将其添加到应用程序类
AndroidGraphicFactory.createInstance(this.getApplication());
中如果没有添加,它将根据他们的文档发生冲突
希望以上有帮助:)