Android Q:在Android java.lang.NoSuchMethodError

时间:2019-05-22 11:18:09

标签: android android-notifications android-bubbles

我正在尝试在Android中使用This新功能并收到错误消息

java.lang.NoSuchMethodError: No direct method <init>()V in class Landroid/app/Notification$BubbleMetadata$Builder; or its super classes 

(“ android.app.Notification $ BubbleMetadata $ Builder”的声明出现在/system/framework/framework.jar中)

项目正在使用“ android-Q”构建,但是当活动开始时应用程序崩溃。

这是我在MainActivity.kt中使用的示例代码

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Create bubble intent
        val target = Intent(this, MainActivity::class.java)
        val bubbleIntent = PendingIntent.getActivity(this, 0, target, 0 /* flags */)

// Create bubble metadata
        val bubbleData = Notification.BubbleMetadata.Builder()
            .setDesiredHeight(600)
            // Note: although you can set the icon is not displayed in Q Beta 2
            .setIcon(Icon.createWithResource(this, R.drawable.notification_icon_background))
            .setIntent(bubbleIntent)
            .build()

// Create notification
        val chatBot = Person.Builder()
            .setBot(true)
            .setName("BubbleBot")
            .setImportant(true)
            .build()

        val builder = Notification.Builder(this, 11.toString())
            .setContentIntent(bubbleIntent)
            .setSmallIcon(R.drawable.notification_icon_background)
            .setBubbleMetadata(bubbleData)
            //.addPerson(chatBot)

        val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.notify(11, builder.build())


    }
}

AndroidManifest.xml

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

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

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.core:core-ktx:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}

1 个答案:

答案 0 :(得分:3)

要在Android中测试Q Beta图片,您需要遵循以下instructions

实际上,要使用Q API,您需要安装 Android Studio 3.5预览,并在应用build.gradle中定位Q android版本,如下所示:

android {
    compileSdkVersion 'android-Q'

    defaultConfig {
       ...
       targetSdkVersion 'Q'
       ...
    }
}