簡體   English   中英

XML,Android Studio中的“無法實例化一個或多個類”錯誤

[英]“Failed to instantiate one or more class” error in XML, Android Studio

XML代碼:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/bgLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:paddingTop="24dp">

            <Button
                android:id="@+id/button_1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/button1"
                android:padding="16dp"
                android:layout_margin="8dp"/>

            <Button
                android:id="@+id/button_2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="16dp"
                android:layout_margin="8dp"
                android:text="@string/button2"/>

            <Button
                android:id="@+id/button_3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="16dp"
                android:layout_margin="8dp"
                android:text="@string/button3"/>
        </LinearLayout>
    </ScrollView>

</android.support.design.widget.CoordinatorLayout>

這是Gradle文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"
    defaultConfig {
        applicationId "com.example.android.bottomsheettest"
        minSdkVersion 21
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    allprojects {
        repositories {
            jcenter()
            maven {
                url "https://maven.google.com"
            }
        }
    }
    lintOptions {
        disable 'RestrictedApi'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:design:26.0.1'
    compile "com.android.support:support-core-utils:26.0.1"
}

在XML預覽中,有一個錯誤指出: 錯誤

這很奇怪,因為它是一個NullPointerException錯誤。 我沒有在主要活動文件中更新任何內容。

我計划為此添加一個底頁,這就是為什么添加Coordinate Layout標簽的原因。 自從添加該標簽以來,我找不到任何方法來消除此錯誤。

在刪除lintOptions和allProject組件並替換了依賴項之后,該錯誤已解決

compile 'com.android.support:design:26.0.1'

與:

compile 'com.android.support:design:26.0.0-alpha1'

它只是將26.0.1降級到26.0.0才更穩定。 別忘了降級所有的Android庫

我也在為這個錯誤而苦苦掙扎:

“ android ...協調器布局”和“無法實例化一個或多個類”

我嘗試了許多“失敗”的嘗試:

  1. 在XML文件的“設計”選項卡中更改API級別。
  2. 改變設計主題
  3. 安裝最新的SDK文件等

但終於經過幾個不眠之夜,我得以通過以下方式做到這一點:

  1. 卸載完整的Android Studio和SDK下載文件。 [注意:這也幫助我節省了120GB MAC書籍中的寶貴空間]
  2. 按照安裝過程中的提示安裝默認的SDK文件。
  3. 在我的“ build.gradle”文件中進行一些更改。 “編譯SDK版本”,“目標SDK版本”和“依賴項”都具有相同的級別。 [26就我而言; 我的最小SDK為15]

同步Gradle之后,這又給了我一個錯誤。 但是它顯示了一些錯誤(有些東西被MAVEN遺忘了以作為該錯誤的快照),我只是下載了該文件所需的額外文件。

然后,它起作用了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM