致命异常:NoSuchMethodError:没有静态方法setOnApplyWindowInsetsListener

问题描述 投票:4回答:2

我怎么解决这个问题?

致命异构:主程序:a6e69.materialdesigntest,PID:1981 java.lang.NoSuchMethodError:无静态方法setOnApplyWindowInsetsListener(Landroid / view / View; Landroid / support / v4 / view / OnApplyWindowInsetsListener;)V类Landroid / support / v4 /视图/ ViewCompatLollipop;或其超级类('android.support.v4.view.ViewCompatLollipop'的声明出现在/data/data/a6e69.materialdesigntest/files/instant-run/dex/slice-internal_impl-24.2.0_0db54c0342e0b2146307a83cef64394e5c99e6bd-classes.dex)at at在android.support.v7.app.AppCompatDelegateImplV7的android.support.v4.view.ViewCompat.setOnApplyWindowInsetsListener(ViewCompat.java:2829)上的android.support.v4.view.ViewCompat $ LollipopViewCompatImpl.setOnApplyWindowInsetsListener(ViewCompat.java:1558)。 Android.support的Android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:312)中的androidSuDecor(AppCompatDelegateImplV7.java:212) .v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)位于android.app的android.app.Activity.performCreate(Activity.java:5990)的a6e69.materialdesigntest.MainActivity.onCreate(MainActivity.java:22)。 Instrumentation.callActivityOnCrea te(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)at android.os.Handler.dispatchMessage(Handler.java:102)at android.os.Looper.loop(Looper.java:135 )来自com.的java.lang.reflect.Method.invoke(Method.java:372)的java.lang.reflect.Method.invoke(Native Method)中的android.app.ActivityThread.main(ActivityThread.java:5254)。 android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

navigation drawer.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:+'}

我的知识

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);

}}

谢谢。

android material-design android-5.0-lollipop
2个回答
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
投票

简单地说,将AppCompatActivity更改为Activity

© www.soinside.com 2019 - 2024. All rights reserved.