当使用AppCompatActivity时,Android出现java.lang.NoClassDefFoundError错误。

3

我把我的应用迁移到了Androidx后,在调试应用时遇到了以下警告:

拒绝重新初始化之前失败的类java.lang.Class: java.lang.NoClassDefFoundError: 无法解析Landroid/view/View$OnUnhandledKeyEventListener; void androidx.core.view.ViewCompat.setOnApplyWindowInsetsListener(android.view.View, androidx.core.view.OnApplyWindowInsetsListener) (ViewCompat.java:2421)

尽管这只是个警告,但我没找到解决方法。

我唯一发现的问题是和AppCompatActivity相关的 - 我在每个活动中都在使用它:

public class MainActivity extends AppCompatActivity

当我将 AppCompatActivity 替换为 Activity 时,警告消失了。
但我认为这不是一个好的解决方案,因为需要向后兼容。我的应用程序的minSdkVersion是23,但有人能确认我可以用 Activity 替换 AppCompatActivity 吗?
我认为未来的功能不会向后兼容...
否则,我认为没有其他简单的解决方法。
以下是 MainActivity 中代码的部分内容:
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.PreferenceManager;

import com.google.firebase.messaging.FirebaseMessaging;

import java.util.Locale;
import java.util.Set;

public class MainActivity extends AppCompatActivity  {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Locale locale = new Locale(lng);
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        getBaseContext().getResources().updateConfiguration(config,
                getBaseContext().getResources().getDisplayMetrics());

new DatabaseHelper(this); 

        this.setContentView(R.layout.activity_main);

ImageView language = findViewById(R.id.lang);
        ImageView logo = findViewById(R.id.logo);

        if (lng.equals("sk")) {
            language.setImageResource(R.drawable.en);
            logo.setImageResource(R.drawable.logo);
        }

        else { language.setImageResource(R.drawable.sk);
            logo.setImageResource(R.drawable.logoen);
        }

        ImageView notify = findViewById(R.id.settings);

        if (readState()) {
            notify.setImageResource(R.drawable.notifyon); }
        else {notify.setImageResource(R.drawable.notifyoff);}

    }

    public void ClickSearch(View v)
    {
        Intent intent = new Intent(MainActivity.this, Search.class);
        startActivity(intent);
    }

}

另一个部分的警告也指向我代码中的这一行:this.setContentView(R.layout.activity_main);

我的依赖关系:

 implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation "androidx.preference:preference:1.1.0"
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'com.google.android.gms:play-services-maps:17.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    implementation 'com.google.firebase:firebase-core:17.2.1'
    implementation 'com.google.firebase:firebase-analytics:17.2.1'
    implementation 'com.crashlytics.sdk.android:crashlytics:2.10.1'
    implementation 'com.google.firebase:firebase-messaging:20.0.0'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'com.squareup.picasso:picasso:2.5.2'
    implementation 'com.google.android.material:material:1.0.0'


}
apply plugin: 'com.google.gms.google-services'

请您能否发布更多的代码,包括导入部分。 - Manoj Perumarath
我更新了我的问题,并附上了部分代码。 - Darksymphony
1
在同一个错误日志中,我还看到了这个:Caused by: java.lang.ClassNotFoundException: 在路径 DexPathList 上没有找到类“android.view.View$OnUnhandledKeyEventListener”。目前似乎没有解决方案-https://issuetracker.google.com/issues/120750246。 - Darksymphony
1
可能是重复的问题 https://dev59.com/Dek5XIcBkEYKwwoY7OLw - Networks
我已经看到了十次...你看到那个链接上有什么解决方案吗?因为我没有。 - Darksymphony
1个回答

3

目前唯一有效的方法是将AppCompatActivity替换为Activity,这样警告就会消失。将来会看到,如果appcompat库冲突得到解决。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接