java.lang.NoClassDefFoundError,用于从MapFragment派生的片段类

时间:2014-12-03 04:32:30

标签: android google-maps android-activity android-fragments google-maps-android-api-2

我正在使用Google Maps API V2进行制作。我按照here步骤进行了操作。还设置了我的应用程序项目中对Google Play服务prjoject的引用。

这是我的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)

我正在努力解决这个问题一个星期。我真的不知道什么是错误的。请帮我解决这个问题。非常感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

清理你的项目...重建你的路径......如果你仍然遇到这个问题......可能是你最新下载的谷歌播放服务库的问题...从这里下载一个旧的

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

如果有帮助请注明