java.lang.NoSuchFieldError:android.support.v4.app.Watson.mFragments

时间:2016-02-09 00:21:58

标签: java android actionbarsherlock

我正在尝试实现ActionBarSherlock滑动菜单,因为我喜欢这个动画在幻灯片菜单上滑动的方式。不幸的是我一直收到这个错误并且无法解决它。

这家伙得到了同样的错误,但没有一个答案解决了我的问题。 Android: java.land.NoSuchFieldError after update to SDK 23

这是我的Gradle文件。

buildscript {
    repositories {
        maven { url 'https://maven.fabric.io/public' }
    }

    dependencies {
        classpath 'io.fabric.tools:gradle:1.+'
    }
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
apply plugin: 'com.google.gms.google-services'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.myegotest.ego_17012016"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

repositories {
    mavenCentral()
    maven { url 'https://maven.fabric.io/public' }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    //compile 'com.android.support:appcompat-v7:23.1.1'
    //compile 'com.android.support:design:23.1.1'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.facebook.android:facebook-android-sdk:4.9.0'
    compile('com.twitter.sdk.android:twitter:1.12.0@aar') {
        transitive = true;
    }
    compile 'com.mcxiaoke.volley:library-aar:1.0.0'
    //compile 'com.android.support:multidex:1.0.1'
    compile "com.android.support:support-v4:23.1.1"

    compile 'com.squareup.okhttp:okhttp:2.5.0'
    compile 'com.squareup.retrofit:retrofit:1.9.0'
    compile 'com.google.android.gms:play-services-auth:8.3.0'
    compile project(':SlidingMenuLib-master')
}

以下是尝试实现SlidingMenu的活动。

 import android.app.ProgressDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Toast;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
import com.myegotest.ego_17012016.ProfileFragments.InterestsPageFragment;
import com.myegotest.ego_17012016.ProfileFragments.ProfilePageFragment;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.MalformedInputException;
import Library.LocalDataBase;
import Library.User;


public class MainActivity extends SlidingFragmentActivity    {

    //View item variables
    ViewPager mPager;

    //Other variables
    private String firstName;
    private String lastName;
    private String username;
    String profilepic;
    LocalDataBase mLocalDataBase;


    //The pager adapter, which provides the pages to the view pager widget.
    private PagerAdapter mPagerAdapter;




    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Get logged in user from LocalDataBase and
        //Destroy Activity if user is logged out
        mLocalDataBase = new LocalDataBase(this);
        User user = mLocalDataBase.getLoggedInUserInfo();
        if(!mLocalDataBase.userIsLoggedIn()){
            MainActivity.this.finish();
        }


        setBehindContentView(R.layout.menu_frame);


        //Get loggedIn user values from the LocalDataBase .
        firstName = user.mFirstName;
        lastName = user.mLastName;
        username = user.mUsername;

        //Capitalize 1st letter of the following variable.
        firstName = capitalizeFirstCharacter(firstName);
        lastName = capitalizeFirstCharacter(lastName);

        // Instantiate a ViewPager and a PagerAdapter.
        mPager = (ViewPager) findViewById(R.id.myViewPager);
        mPager.setPageTransformer(true, new ZoomOutPageTransformer());


        netAsync();
    }

    //Method to capitalize 1st letter of the String thats using this method.
    private String capitalizeFirstCharacter(String textInput){
        String input = textInput.toLowerCase();
        String output = input.substring(0, 1).toUpperCase() + input.substring(1);
        return output;
    }


    /**
     * A simple pager adapter that represents 2 ScreenSlidePageFragment objects, in
     * sequence.
     */
    private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
        public ScreenSlidePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch(position){
                case 1:
                    return new InterestsPageFragment();
                default: break;
            }
            return ProfilePageFragment.newInstance(firstName, lastName, profilepic);
        }

        @Override
        public int getCount() {
            return 2;
        }
    }


    //Animation for swiping between page views
    public class ZoomOutPageTransformer implements ViewPager.PageTransformer {
        private static final float MIN_SCALE = 0.85f;
        private static final float MIN_ALPHA = 0.5f;

        public void transformPage(View view, float position) {
            int pageWidth = view.getWidth();
            int pageHeight = view.getHeight();

            if (position < -1) { // [-Infinity,-1)
                // This page is way off-screen to the left.
                view.setAlpha(0);

            } else if (position <= 1) { // [-1,1]
                // Modify the default slide transition to shrink the page as well
                float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
                float vertMargin = pageHeight * (1 - scaleFactor) / 2;
                float horzMargin = pageWidth * (1 - scaleFactor) / 2;
                if (position < 0) {
                    view.setTranslationX(horzMargin - vertMargin / 2);
                } else {
                    view.setTranslationX(-horzMargin + vertMargin / 2);
                }

                // Scale the page down (between MIN_SCALE and 1)
                view.setScaleX(scaleFactor);
                view.setScaleY(scaleFactor);

                // Fade the page relative to its size.
                view.setAlpha(MIN_ALPHA +
                        (scaleFactor - MIN_SCALE) /
                                (1 - MIN_SCALE) * (1 - MIN_ALPHA));

            } else { // (1,+Infinity]
                // This page is way off-screen to the right.
                view.setAlpha(0);
            }
        }
    }


}

最后,这是我得到的错误。

E/AndroidRuntime: FATAL EXCEPTION: main
E/AndroidRuntime: Process: com.myegotest.ego_17012016, PID: 8545
E/AndroidRuntime: java.lang.NoSuchFieldError: android.support.v4.app.Watson.mFragments
E/AndroidRuntime:     at android.support.v4.app.Watson.onCreatePanelMenu(Watson.java:50)
E/AndroidRuntime:     at com.actionbarsherlock.ActionBarSherlock.callbackCreateOptionsMenu(ActionBarSherlock.java:560)
E/AndroidRuntime:     at com.actionbarsherlock.internal.ActionBarSherlockNative.dispatchCreateOptionsMenu(ActionBarSherlockNative.java:64)
E/AndroidRuntime:     at com.actionbarsherlock.app.SherlockFragmentActivity.onCreatePanelMenu(SherlockFragmentActivity.java:164)
E/AndroidRuntime:     at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:486)
E/AndroidRuntime:     at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:855)
E/AndroidRuntime:     at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:271)
E/AndroidRuntime:     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
E/AndroidRuntime:     at android.view.Choreographer.doCallbacks(Choreographer.java:603)
E/AndroidRuntime:     at android.view.Choreographer.doFrame(Choreographer.java:572)
E/AndroidRuntime:     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:733)
E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:157)
E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5293)
E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
I/Process: Sending signal. PID: 8545 SIG: 9

1 个答案:

答案 0 :(得分:0)

我能够通过从我的gradle文件中删除facebook sdk来解决问题。由于我需要facebook sdk,我能够通过使MainActivity扩展FragmentActivity而不是SlidingFragmentActivty来解决这个问题。我在MainActivty中创建了这个内部类来扩展SlidingActivity,这样我仍然可以使用我需要的方法。

private class SlidingMenuClass extends SlidingActivity {
        private SlidingMenu sm;

        public SlidingMenuClass(Context ctx){
            sm = new SlidingMenu(ctx);
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setBehindContentView(R.layout.menu_frame);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);


            sm = getSlidingMenu();
            sm.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
            sm.setShadowWidthRes(R.dimen.shadow_width);
            //sm.setShadowDrawable(R.drawable.shadow);
            sm.setFadeDegree(1f);
            sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
            sm.setBehindOffsetRes(R.dimen.behindoffsetRes);
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
                case android.R.id.home:
                    toggle();
                    return true;
            }
            return super.onOptionsItemSelected(item);
        }

    }