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

huangapple go评论17阅读模式
英文:

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

问题

抱歉,我无法识别你要翻译的代码部分,请提供需要翻译的文本内容。

英文:

Hello my dear friends,

I am trying to develop media files to my server with the help of Retrofit and a Fileprovider.

Like the title already describes my situation, am I looking for a solution of the following error. This error let the app crash during the launch while the app runs without issues on other phones. I am trying for a week now to fix this error and tried everything from Multidex integration, writing a new java path for my SDK, checking my permissions and clearing my mobile's cache.

To be honest, I am not really good at understanding the logcat when it comes to files in the background that I have not seen before. Apparently, the app does not find my Fileprovider class. I defined the Fileprovider in my manifest file and wrote a corresponding file_path.xml file. I could not find the need of a specific Java provider.class nor any gradle dependencies to make a fileprovider work.

I am excited what you say to my problem and to my logcat. I appreciate every useful information!

Thank you for reading.

My 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) 

My Manifest file:

<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>

My Gradle file (I included many dependencies out of desperation ...):

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'
}

My Main Activity:

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;
        }
    }




}

I am a little bit overwhelmed by all these different software versions on my phone:

Android version: 8.0.0
Samsung Experience version: 9.0
Knox version:3.0, API level 24

Let me know if you need more information

答案1

得分: 2

我已解决了这个问题。
我在defaultConfig内的build.gradle中设置了androidx.core:core:1.8.0

configurations.all {
    resolutionStrategy {
        force 'androidx.core:core:1.8.0'
    }
}

然后将compileSdkVersion更改为32。
清理项目并重新构建项目,最后一切都正常。

英文:

I have solved this problem.<br/> I set androidx.core:core:1.8.0 in build.gradle inside defaultConfig.

configurations.all {
    resolutionStrategy {
        force &#39;androidx.core:core:1.8.0&#39;
    }
}

Then change compileSdkVersion to 32.<br/>
Clean project and rebuild project, finally everything be ok.

答案2

得分: 0

The AppCompat dependency compiles the Fileprovider which is within the androidx.core:core package as its dependency. Therefore remove this line implementation 'androidx.core:core:1.3.0' from your gradle file to see if it works. Because appcompat lib has version 1.1.0 while u still compiled 1.3.0 in your project.

英文:

The AppCompat dependency compiles the Fileprovider which is within the androidx.core:core package as its dependency. Therefore remove this line implementation &#39;androidx.core:core:1.3.0&#39; from your gradle file to see if it works. Because appcompat lib has version 1.1.0 while u still compiled 1.3.0 in your project.

huangapple
  • 本文由 发表于 2020年7月22日 22:23:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63036519.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定