0

我正在使用Google Maps API V2進行映射。我按照步驟here。我的應用程序項目中對Google Play服務prjoject的引用也被設置。從MapFragment派生的片段類的java.lang.NoClassDefFoundError

這是我的Acitivty課程。

public class MapActivity extends Activity 
    implements NavigationDrawerFragment.NavigationDrawerCallbacks { 

/** 
* Fragment managing the behaviors, interactions and presentation of the navigation drawer. 
*/ 
private NavigationDrawerFragment mNavigationDrawerFragment; 

/** 
* Used to store the last screen title. For use in {@link #restoreActionBar()}. 
*/ 
private CharSequence mTitle; 

private MainMapFragment mMapFragment; 
private GoogleMap mMap; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_map); 

    mNavigationDrawerFragment = (NavigationDrawerFragment) 
      getFragmentManager().findFragmentById(R.id.navigation_drawer); 
    mTitle = getTitle(); 


    int resultcode =  
    GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext()); 
    if (resultcode == ConnectionResult.SUCCESS) 
    { 
     Log.d("Test", "success"); 
    } 
    // Set up the drawer. 
    mNavigationDrawerFragment.setUp(
      R.id.navigation_drawer, 
      (DrawerLayout) findViewById(R.id.drawer_layout)); 

} 

@Override 
public void onNavigationDrawerItemSelected(int position) { 
    // update the main content by replacing fragments 
    FragmentManager fragmentManager = getFragmentManager(); 
    if (position == 0) { 
     mMapFragment = MainMapFragment.newInstance(position + 1); 
     fragmentManager.beginTransaction() 
       .replace(R.id.container, mMapFragment) 
       .commit(); 
    } 
    if (position == 1) 
    { 
     WebFragment webFragment = WebFragment.newInstance(position + 1); 
     fragmentManager.beginTransaction() 
       .replace(R.id.container, webFragment) 
       .commit(); 
    } 
    else 
    { 
     fragmentManager.beginTransaction() 
       .replace(R.id.container, PlaceholderFragment.newInstance(position + 1)) 
       .commit(); 
    } 

} 

public static class MainMapFragment extends MapFragment{ 
    /** 
    * The fragment argument representing the section number for this 
    * fragment. 
    */ 
    private static final String ARG_SECTION_NUMBER = "section_number"; 

    /** 
    * Returns a new instance of this fragment for the given section 
    * number. 
    */ 
    public static MainMapFragment newInstance(int sectionNumber) { 
     MainMapFragment fragment = new MainMapFragment(); 
     Bundle args = new Bundle(); 
     args.putInt(ARG_SECTION_NUMBER, sectionNumber); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    public MainMapFragment() { 
    } 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 
     ((MyActivity) activity).onSectionAttached(
       getArguments().getInt(ARG_SECTION_NUMBER)); 
    } 
} 

public static class WebFragment extends Fragment { 
    /** 
    * The fragment argument representing the section number for this 
    * fragment. 
    */ 
    private static final String ARG_SECTION_NUMBER = "section_number"; 

    /** 
    * Returns a new instance of this fragment for the given section 
    * number. 
    */ 
    public static WebFragment newInstance(int sectionNumber) { 
     WebFragment fragment = new WebFragment(); 
     Bundle args = new Bundle(); 
     args.putInt(ARG_SECTION_NUMBER, sectionNumber); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    public WebFragment() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     WebView webview = new WebView(getActivity()); 


     webview.loadUrl("http://www.something.com/"); 
     return webview; 
    } 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 
     ((MyActivity) activity).onSectionAttached(
       getArguments().getInt(ARG_SECTION_NUMBER)); 
    } 
} 


/** 
* A placeholder fragment containing a simple view. 
*/ 
public static class PlaceholderFragment extends Fragment { 
    /** 
    * The fragment argument representing the section number for this 
    * fragment. 
    */ 
    private static final String ARG_SECTION_NUMBER = "section_number"; 

    /** 
    * Returns a new instance of this fragment for the given section 
    * number. 
    */ 
    public static PlaceholderFragment newInstance(int sectionNumber) { 
     PlaceholderFragment fragment = new PlaceholderFragment(); 
     Bundle args = new Bundle(); 
     args.putInt(ARG_SECTION_NUMBER, sectionNumber); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    public PlaceholderFragment() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_something, container, false); 
     TextView textView = (TextView) rootView.findViewById(R.id.section_label); 
     textView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER))); 
     return rootView; 
    } 

    @Override![enter image description here][1] 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 
     ((MyActivity) activity).onSectionAttached(
       getArguments().getInt(ARG_SECTION_NUMBER)); 
    } 
} 

} 

截圖我的應用程序的項目屬性: Google Play Service project referenced as lib

Java Build Path

的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.app" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="14" 
     android:targetSdkVersion="19" /> 

    <permission 
     android:name="com.example.app.permission.MAPS_RECEIVE" 
     android:protectionLevel="signature" /> 

    <uses-feature 
     android:glEsVersion="0x00020000" 
     android:required="true" /> 

    <uses-permission android:name="com.example.app.permission.MAPS_RECEIVE" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.example.app.MapActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <meta-data 
     android:name="com.google.android.gms.version" 
     android:value="@integer/google_play_services_version" /> 
     <meta-data 
      android:name="com.google.android.maps.v2.API_KEY" 
      android:value="MY_API_KEY" /> 
    </application> 

</manifest> 

當我在我的設備上運行我的應用程序崩潰。請填寫下面的logcat消息。

12-03 08:58:30.259: D/dalvikvm(17745): Late-enabling CheckJNI 
    12-03 08:58:30.377: W/ActivityThread(17745): Application com.example.app is waiting for the debugger on port 8100... 
    12-03 08:58:30.379: I/System.out(17745): Sending WAIT chunk 
    12-03 08:58:30.446: I/dalvikvm(17745): Debugger is active 
    12-03 08:58:30.590: I/System.out(17745): Debugger has connected 
    12-03 08:58:30.590: I/System.out(17745): waiting for debugger to settle... 
    12-03 08:58:30.790: I/System.out(17745): waiting for debugger to settle... 
    12-03 08:58:30.990: I/System.out(17745): waiting for debugger to settle... 
    12-03 08:58:31.191: I/System.out(17745): waiting for debugger to settle... 
    12-03 08:58:31.414: I/System.out(17745): waiting for debugger to settle... 
    12-03 08:58:31.614: I/System.out(17745): waiting for debugger to settle... 
    12-03 08:58:31.815: I/System.out(17745): debugger has settled (1364) 
    12-03 08:58:31.843: I/dalvikvm(17745): Could not find method  
    com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable, referenced  
    from method com.example.app.MapActivity.onCreate 
    12-03 08:58:31.843: W/dalvikvm(17745): VFY: unable to resolve static method 
    5180:Lcom/google/android/gms/common/GooglePlayServicesUtil;.isGooglePlayServicesAvailable 
    (Landroid/content/Context;)I 
    12-03 08:58:31.843: D/dalvikvm(17745): VFY: replacing opcode 0x71 at 0x0021 
    12-03 08:58:31.845: W/dalvikvm(17745): Unable to resolve superclass of 
    Lcom/example/app/MapActivity$MainMapFragment; (790) 
    12-03 08:58:31.845: W/dalvikvm(17745): Link of class 
    'Lcom/example/app/MapActivity$MainMapFragment;' failed 
    12-03 08:58:31.846: I/dalvikvm(17745): Could not find method 
    com.example.app.MapActivity$MainMapFragment.newInstance, referenced from method 
    com.example.app.MapActivity.onNavigationDrawerItemSelected 
    12-03 08:58:31.846: W/dalvikvm(17745): VFY: unable to resolve static method 5090: 
    Lcom/example/app/MapActivity$MainMapFragment;.newInstance (I) 

    Lcom/example/app/MapActivity$MainMapFragment; 
    12-03 08:58:31.846: D/dalvikvm(17745): VFY: replacing opcode 0x71 at 0x000b 
    12-03 08:58:32.042: D/AndroidRuntime(17745): Shutting down VM 
    12-03 08:58:32.042: W/dalvikvm(17745): threadid=1: thread exiting with uncaught exception 
    (group=0x4186bd40) 
    12-03 08:58:32.070: E/AndroidRuntime(17745): FATAL EXCEPTION: main 
    12-03 08:58:32.070: E/AndroidRuntime(17745): Process: com.example.app, PID: 17745 
    12-03 08:58:32.070: E/AndroidRuntime(17745): java.lang.NoClassDefFoundError: 
    com.example.app.MapActivity$MainMapFragment 
    12-03 08:58:32.070: E/AndroidRuntime(17745): at 
    com.example.app.MapActivity.onNavigationDrawerItemSelected(MapActivity.java:73) 
    12-03 08:58:32.070: E/AndroidRuntime(17745): at 
    com.example.app.NavigationDrawerFragment.selectItem(NavigationDrawerFragment.java:200) 
    12-03 08:58:32.070: E/AndroidRuntime(17745): at 
    com.example.app.NavigationDrawerFragment.onCreate(NavigationDrawerFragment.java:79) 
    12-03 08:58:32.070: E/AndroidRuntime(17745): at 
    android.app.Fragment.performCreate(Fragment.java:1678) 
    12-03 08:58:32.070: E/AndroidRuntime(17745): at 
    android.app.FragmentManagerImpl.moveToState(FragmentManager.java:859) 
    12-03 08:58:32.070: E/AndroidRuntime(17745): at 
    android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1040) 
    12-03 08:58:32.070: E/AndroidRuntime(17745): at 
    android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1142) 
    12-03 08:58:32.070: E/AndroidRuntime(17745): at 
    android.app.Activity.onCreateView(Activity.java:4803) 
    12-03 08:58:32.070: E/AndroidRuntime(17745): at 
    android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689) 
    12-03 08:58:32.070: E/AndroidRuntime(17745): at 
    android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 
    12-03 08:58:32.070: E/AndroidRuntime(17745): at 
    android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
    12-03 08:58:32.070: E/AndroidRuntime(17745): at 
    android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
    12-03 08:58:32.070: E/AndroidRuntime(17745): at 
    android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
    12-03 08:58:32.070: E/AndroidRuntime(17745): at 
    com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290) 
    12-03 08:58:32.070: E/AndroidRuntime(17745): at 
    android.app.Activity.setContentView(Activity.java:1937) 
    12-03 08:58:32.070: E/AndroidRuntime(17745): at 
    com.example.app.MapActivity.onCreate(MapActivity.java:49) 
    12-03 08:58:32.070: E/AndroidRuntime(17745): at 
    android.app.Activity.performCreate(Activity.java:5248) 
    12-03 08:58:32.070: E/AndroidRuntime(17745): at 
    android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110) 
    12-03 08:58:32.070: E/AndroidRuntime(17745): at 
    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162) 
    12-03 08:58:32.070: E/AndroidRuntime(17745): at 
    android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257) 
    12-03 08:58:32.070: E/AndroidRuntime(17745): at 
    android.app.ActivityThread.access$800(ActivityThread.java:139) 
    12-03 08:58:32.070: E/AndroidRuntime(17745): at 
    android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210) 
    12-03 08:58:32.070: E/AndroidRuntime(17745): at 
    android.os.Handler.dispatchMessage(Handler.java:102) 
    12-03 08:58:32.070: E/AndroidRuntime(17745): at android.os.Looper.loop(Looper.java:136) 
    12-03 08:58:32.070: E/AndroidRuntime(17745): at 
    android.app.ActivityThread.main(ActivityThread.java:5086) 
    12-03 08:58:32.070: E/AndroidRuntime(17745): at java.lang.reflect.Method.invokeNative(Native 
    Method) 
    12-03 08:58:32.070: E/AndroidRuntime(17745): at 
    java.lang.reflect.Method.invoke(Method.java:515) 
    12-03 08:58:32.070: E/AndroidRuntime(17745): at 
    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
    12-03 08:58:32.070: E/AndroidRuntime(17745): at 
    com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
    12-03 08:58:32.070: E/AndroidRuntime(17745): at dalvik.system.NativeStart.main(Native Method) 

我在這個問題上掙扎了一個星期。我真的不知道什麼是錯誤的。請幫我解決這個問題。非常感謝您的幫助。

+0

你的設備測試? – 2014-12-03 04:35:05

+0

您必須僅在設備中測試MAPS,因爲仿真器不支持此功能... – 2014-12-03 04:37:44

+0

是的。我只在設備上進行測試。當我在設備上運行時,該應用崩潰。 – Keerthi 2014-12-03 04:41:58

回答

2

清理你的項目...重建你的路徑...如果你仍然有這個問題...它可能是你最近下載的谷歌播放服務庫的問題...從這裏下載一箇舊的

https://dl.dropboxusercontent.com/u/22456079/post%20files/SO/24732568/google-play-services_lib_4.x.zip

請標記它,如果這有助於

+0

你救了我的命。在您的鏈接中使用舊的谷歌播放服務庫後,地圖工作。非常感謝你的幫助。 – Keerthi 2014-12-03 15:17:21

+0

快樂編碼:) – 2014-12-04 05:03:43