-1

我是android新手,我想实现动作栏滑动,我阅读教程并写了这个。在程序中,我使用了框架布局,框架布局用 mapsforge 填充,但运行时的程序有错误。为什么?如果我删除片段和事务片段,程序运行良好。

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>

片段布局.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
4

3 回答 3

0

我有类似的错误消息,我已经用以下方法修复了它:

  • 添加所有 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 build path configuration 中检查它(Project > Properties > Java Build Path > Order and Export)
  • 将此行添加到地图之前的类中或将其添加到 Application 类中AndroidGraphicFactory.createInstance(this.getApplication()); 如果未添加,它将根据他们的文档发生冲突

希望以上有所帮助:)

于 2015-01-26T03:30:04.897 回答
0

改变

<org.mapsforge.android.maps.MapView

<com.google.android.gms.maps.MapView

由于您在评论中提到您使用 mapsforge 库,因此请关注

http://code.google.com/p/mapsforge/wiki/GettingStartedMapView

于 2014-01-27T05:45:59.490 回答
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);
}
}
于 2014-01-27T05:52:11.767 回答