致命异常:NoSuchMethodError:无静态方法setOnApplyWindowInsetsListener

时间:2016-09-02 16:05:16

标签: android material-design android-5.0-lollipop

我如何解决这个问题?

  

致命的例外:主要                                                                           过程:a6e69.materialdesigntest,PID:1981                                                                           java.lang.NoSuchMethodError:没有静态方法setOnApplyWindowInsetsListener(Landroid / view / View; Landroid / support / v4 / view / OnApplyWindowInsetsListener;)类Landroid / support / v4 / view / ViewCompatLollipop中的V;或其超级类(声明' android.support.v4.view.ViewCompatLollipop'出现在/data/data/a6e69.materialdesigntest/files/instant-run/dex/slice-internal_impl-24.2.0_0db54c0342e0b2146307a83cef64394e5c99e6bd- classes.dex)                                                                               在android.support.v4.view.ViewCompat $ LollipopViewCompatImpl.setOnApplyWindowInsetsListener(ViewCompat.java:1558)                                                                               在android.support.v4.view.ViewCompat.setOnApplyWindowInsetsListener(ViewCompat.java:2829)                                                                               在android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:425)                                                                               在android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:312)                                                                               在android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:277)                                                                               在android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)                                                                               at a6e69.materialdesigntest.MainActivity.onCreate(MainActivity.java:22)                                                                               在android.app.Activity.performCreate(Activity.java:5990)                                                                               在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)                                                                               在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)                                                                               在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)                                                                               在android.app.ActivityThread.access $ 800(ActivityThread.java:151)                                                                               在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1303)                                                                               在android.os.Handler.dispatchMessage(Handler.java:102)                                                                               在android.os.Looper.loop(Looper.java:135)                                                                               在android.app.ActivityThread.main(ActivityThread.java:5254)                                                                               at java.lang.reflect.Method.invoke(Native Method)                                                                               在java.lang.reflect.Method.invoke(Method.java:372)                                                                               在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903)                                                                               在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

NavigationDrawer.java

public class NavigationDrawerFragment extends Fragment {
private RecyclerView recyclerView;
private RecycleAdapter adapter;
private boolean mUserLearnedDrawer;
private boolean mFromSavedInstanceState;
public static final String PREF_FILE_NAME="testpref";
public static final String KEY_USER_LEANERD_DRAWER="user_leanerd_drawer";
private View containerView;
ActionBarDrawerToggle mDrawerToggle;
DrawerLayout mDrawerLayout;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mUserLearnedDrawer=Boolean.valueOf(readFromPreferences(getActivity(),KEY_USER_LEANERD_DRAWER,"false"));
    if(savedInstanceState!=null){
        mFromSavedInstanceState=true;
    }

}

public NavigationDrawerFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View layout=inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
    recyclerView= (RecyclerView) layout.findViewById(R.id.drawerList);
    adapter=new RecycleAdapter(getActivity(),getData());
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    return layout;
}

public static List<Information> getData(){
    List<Information> data=new ArrayList<>();
    int[] icons={R.drawable.abacus,R.drawable.bell,R.drawable.blackboard,R.drawable.book};
    String[] titles={"abacus","bell","blackboard","book"};

    for (int i=0;i<icons.length && i<titles.length;i++){
        Information current=new Information();
        current.iconId=icons[i];
        current.title=titles[i];
        data.add(current);
    }
    return data;
}

public void setUp(int fragmentid, DrawerLayout drawerlayout, final Toolbar tollbar) {
    containerView=getActivity().findViewById(fragmentid);
    mDrawerLayout=drawerlayout;
    mDrawerToggle=new ActionBarDrawerToggle(getActivity(),mDrawerLayout,tollbar,R.string.drawer_open,R.string.drawer_close){

        @TargetApi(Build.VERSION_CODES.HONEYCOMB)
        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            if(!mUserLearnedDrawer){
                mUserLearnedDrawer=true;
                saveToPreferences(getActivity(),KEY_USER_LEANERD_DRAWER,mUserLearnedDrawer+"");
            }
            getActivity().invalidateOptionsMenu();
        }

        @TargetApi(Build.VERSION_CODES.HONEYCOMB)
        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            getActivity().invalidateOptionsMenu();
        }

        @TargetApi(Build.VERSION_CODES.HONEYCOMB)
        @Override
        public void onDrawerSlide(View drawerView, float slideOffset) {
            tollbar.setAlpha((float) (1-(slideOffset-.3)));
        }
    };

    if (!mUserLearnedDrawer && !mFromSavedInstanceState){
        mDrawerLayout.openDrawer(containerView );}
    mDrawerLayout.addDrawerListener(mDrawerToggle);

    mDrawerLayout.post(new Runnable() {
        @Override
        public void run() {
            mDrawerToggle.syncState();
        }
    });

}
public static void saveToPreferences(Context context,String preferenceName,String preferenceValue){
    SharedPreferences sharedpreferences=context.getSharedPreferences(PREF_FILE_NAME,context.MODE_PRIVATE);
    SharedPreferences.Editor editor=sharedpreferences.edit();
    editor.putString(preferenceName,preferenceValue);
    editor.apply();
}
public static String readFromPreferences(Context context,String preferenceName,String defaultValue){
    SharedPreferences sharedpreferences=context.getSharedPreferences(PREF_FILE_NAME,context.MODE_PRIVATE);return sharedpreferences.getString(preferenceName,defaultValue);}

清单

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".SubActivity">
        <meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".MainActivity"/>
    </activity>

</application>

 应用

android {
compileSdkVersion 23
buildToolsVersion "23.0.3"

defaultConfig {
    applicationId "a6e69.materialdesigntest"
    minSdkVersion 10
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}}dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:support-v4:23.4.0'
compile 'com.android.support:recyclerview-v7:+'}

MainActivity

public class MainActivity extends AppCompatActivity {
Toolbar appBar;


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

    appBar = (Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(appBar);


    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.navFragment);

    drawerFragment.setUp(R.id.navFragment,(DrawerLayout) findViewById(R.id.drawerLayout1), appBar);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);


    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();


    if (id == R.id.setting) {
        Toast.makeText(this, "hey you just hint", Toast.LENGTH_SHORT).show();
        return true;
    } else if (id == R.id.next) {
        startActivity(new Intent(this, SubActivity.class));
    }
    return super.onOptionsItemSelected(item);

}}

TNX。

2 个答案:

答案 0 :(得分:9)

请在以下

中的gradle文件中更新以下内容
compileSdkVersion 24
buildToolsVersion "24.0.1"

targetSdkVersion to 24

compile 'com.android.support:appcompat-v7:24.2.0'
compile 'com.android.support:support-v4:24.2.0'
compile 'com.android.support:recyclerview-v7:24.2.0'

注意:请使用库的最新版本,并避免使用+,就像您正在为您的recyclerview所做的那样。事实上你应该在android studio中得到这个警告。

答案 1 :(得分:-1)

只需将AppCompatActivity更改为Acticity