【问题标题】:error: cannot access DrawerLayout when using AppCompatActivity错误:使用 AppCompatActivity 时无法访问 DrawerLayout
【发布时间】:2023-04-07 08:43:01
【问题描述】:

使用 appcompat "com.android.support:appcompat-v7:28.0.0"

我没有在我的活动中使用android.support.v4.widget.DrawerLayout。即使在测试设备上运行应用程序时出现以下错误,

error: cannot access DrawerLayout
public class MainActivity extends AppCompatActivity {
   ^
  class file for android.support.v4.widget.DrawerLayout not found

app.gradle

apply plugin: 'com.android.application'

android {
compileSdkVersion 28
buildToolsVersion "28.0.3"
defaultConfig {
    applicationId "com.xx.xx"
    minSdkVersion 21
    targetSdkVersion 21
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}

compileOptions {
    sourceCompatibility 1.8
    targetCompatibility 1.8
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'org.apache.commons:commons-lang3:3.7'

testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

活动

package com.xx.xx;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.AppCompatTextView;
import android.widget.LinearLayout;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {

private LinearLayout stateContainer;
private ToggleButton toggleNw;
private ToggleButton toggleLocation;
private AppCompatEditText etLanguageCode;
private AppCompatEditText etCountryCode;
private AppCompatTextView txSplittedText;
private ToggleButton toggleState;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    stateContainer = findViewById(R.id.stateContainer);
    toggleNw = findViewById(R.id.toggleNw);
    toggleLocation = findViewById(R.id.toggleLocation);
    etLanguageCode = findViewById(R.id.etLanguageCode);
    etCountryCode = findViewById(R.id.etCountryCode);
    txSplittedText = findViewById(R.id.txSplittedText);
    toggleState = (ToggleButton) findViewById(R.id.toggleState);
}
}

【问题讨论】:

  • 你的 build.gradle 文件中是否有 'com.android.support:design:x.x.x' 依赖项
  • 请贴出你整个MainActivity的代码。或至少开始部分。
  • @Sree 是的,com.android.support:design:28.0.0
  • @Sujith 请分享您的build.gradle 文件并提出问题
  • @Nilesh Rathod 告诉我你为什么觉得这被标记为重复。

标签: android android-support-library android-appcompat android-design-library


【解决方案1】:

找不到 android.support.v4.widget.DrawerLayout 的类文件

DrawerLayout 不包含在支持设计库中。它位于android.support.v4 库中。

你可以添加依赖com.android.support:drawerlayout:28.0.0

【讨论】:

    【解决方案2】:

    DrawerLayout 类文件未找到, 可以添加依赖com.android.support:support-v4:28.0.0

    【讨论】:

      猜你喜欢
      • 2023-03-23
      • 2021-01-25
      • 2014-07-23
      • 1970-01-01
      • 1970-01-01
      • 2019-01-21
      • 2018-03-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多