簡體   English   中英

運行時異常:無法獲取 androidx.core.content.FileProvider:ClassNotFoundExecption:出現在我的 Galaxy S8 上

[英]Runtimeexception: Unable to get androidx.core.content.FileProvider: ClassNotFoundExecption: appears on my Galaxy S8

你好我親愛的朋友們,

我正在嘗試在 Retrofit 和 Fileprovider 的幫助下將媒體文件開發到我的服務器。

就像標題已經描述了我的情況一樣,我是否正在尋找以下錯誤的解決方案。 此錯誤使應用程序在啟動期間崩潰,而應用程序在其他手機上運行時沒有問題。 我正在嘗試一個星期來修復此錯誤,並嘗試了從 Multidex 集成、為我的 SDK 編寫新的 java 路徑、檢查我的權限並清除我的手機緩存的所有方法。

老實說,當涉及到我以前沒有見過的后台文件時,我並不是很擅長理解 logcat。 顯然,該應用程序沒有找到我的 Fileprovider class。 我在清單文件中定義了 Fileprovider,並編寫了相應的 file_path.xml 文件。 我找不到需要特定的 Java provider.class 或任何 gradle 依賴項來使文件提供程序工作。

我很高興你對我的問題和我的 logcat 所說的話。 我感謝每一個有用的信息!

感謝您的閱讀。

我的日志貓:

  java.lang.RuntimeException: Unable to instantiate application com.example.loginandregister.MainActivity: java.lang.ClassCastException: com.example.loginandregister.MainActivity cannot be cast to android.app.Application
        at android.app.LoadedApk.makeApplication(LoadedApk.java:979)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6030)
        at android.app.ActivityThread.-wrap1(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1764)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6938)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
     Caused by: java.lang.ClassCastException: com.example.loginandregister.MainActivity cannot be cast to android.app.Application
        at android.app.Instrumentation.newApplication(Instrumentation.java:1108)
        at android.app.Instrumentation.newApplication(Instrumentation.java:1093)
        at android.app.LoadedApk.makeApplication(LoadedApk.java:973)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6030) 
        at android.app.ActivityThread.-wrap1(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1764) 
        at android.os.Handler.dispatchMessage(Handler.java:105) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6938) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374) 

我的清單文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.retrofit_uploadmediafiles">

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.READ_INTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

    <application

        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        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=".ImageActivity"/>



        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.example.retrofit_uploadmediafiles.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
        </provider>
    </application>

我的 Gradle 文件(出於絕望,我包含了許多依賴項......):

android {
    compileSdkVersion 29

    defaultConfig {
        applicationId "com.example.retrofit_uploadmediafiles"
        minSdkVersion 23
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

        multiDexEnabled true
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.test:runner:1.2.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'com.afollestad.material-dialogs:core:0.9.4.5'
    implementation 'com.github.bumptech.glide:glide:4.11.0'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'


    implementation "androidx.multidex:multidex:2.0.1"

    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
    implementation 'com.rengwuxian.materialedittext:library:2.1.4'

    implementation group: 'commons-io', name: 'commons-io', version: '2.7'
}

我的主要活動:

public class MainActivity extends AppCompatActivity  implements View.OnClickListener{
    private Button image, video, pdf;


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

        setContentView(R.layout.activity_main);

        image = findViewById(R.id.image);
        video = findViewById(R.id.video);
        pdf = findViewById(R.id.Pdf);

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            image.setEnabled(false);
            video.setEnabled(false);
            pdf.setEnabled(false);
            ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
        } else {
            image.setEnabled(true);
            video.setEnabled(true);
            pdf.setEnabled(true);
        }

        image.setOnClickListener(this);
        video.setOnClickListener(this);
        pdf.setOnClickListener(this);


    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if(requestCode == 0 ) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                image.setEnabled(true);
                video.setEnabled(true);
                pdf.setEnabled(true);
            }
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.image:
                Intent intent = new Intent(this, ImageActivity.class);
                startActivity(intent);
                break;
            case R.id.video:
                //Intent intent2 = new Intent(this, VideoActivity.class);
                //startActivity(intent2);   I am trying to make the image upload work for now 
                break;
            case R.id.Pdf:
                //Intent intent3 = new Intent(this, PdfActivity.class);
                //startActivity(intent3);
                break;
        }
    }




}

我對手機上所有這些不同的軟件版本有點不知所措:

Android 版本:8.0.0 三星體驗版:9.0 諾克斯版本:3.0,API 24級

如果您需要更多信息,請與我們聯系

AppCompat 依賴項將androidx.core:core package 中的 Fileprovider 編譯為其依賴項。 因此,從您的 gradle 文件中刪除此行implementation 'androidx.core:core:1.3.0'以查看它是否有效。 因為 appcompat lib 的版本為1.1.0 ,而您仍然在項目中編譯1.3.0

暫無
暫無

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

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