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

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

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:

  1. 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
  2. at android.app.LoadedApk.makeApplication(LoadedApk.java:979)
  3. at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6030)
  4. at android.app.ActivityThread.-wrap1(Unknown Source:0)
  5. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1764)
  6. at android.os.Handler.dispatchMessage(Handler.java:105)
  7. at android.os.Looper.loop(Looper.java:164)
  8. at android.app.ActivityThread.main(ActivityThread.java:6938)
  9. at java.lang.reflect.Method.invoke(Native Method)
  10. at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
  11. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
  12. Caused by: java.lang.ClassCastException: com.example.loginandregister.MainActivity cannot be cast to android.app.Application
  13. at android.app.Instrumentation.newApplication(Instrumentation.java:1108)
  14. at android.app.Instrumentation.newApplication(Instrumentation.java:1093)
  15. at android.app.LoadedApk.makeApplication(LoadedApk.java:973)
  16. at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6030) 
  17. at android.app.ActivityThread.-wrap1(Unknown Source:0) 
  18. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1764) 
  19. at android.os.Handler.dispatchMessage(Handler.java:105) 
  20. at android.os.Looper.loop(Looper.java:164) 
  21. at android.app.ActivityThread.main(ActivityThread.java:6938) 
  22. at java.lang.reflect.Method.invoke(Native Method) 
  23. at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) 
  24. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374) 

My Manifest file:

  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  2. package="com.example.retrofit_uploadmediafiles">
  3. <uses-permission android:name="android.permission.INTERNET"/>
  4. <uses-permission android:name="android.permission.READ_INTERNAL_STORAGE"/>
  5. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  6. <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
  7. <application
  8. android:allowBackup="true"
  9. android:icon="@mipmap/ic_launcher"
  10. android:label="@string/app_name"
  11. android:roundIcon="@mipmap/ic_launcher_round"
  12. android:supportsRtl="true"
  13. android:theme="@style/AppTheme">
  14. <activity android:name=".MainActivity">
  15. <intent-filter>
  16. <action android:name="android.intent.action.MAIN" />
  17. <category android:name="android.intent.category.LAUNCHER" />
  18. </intent-filter>
  19. </activity>
  20. <activity android:name=".ImageActivity"/>
  21. <provider
  22. android:name="androidx.core.content.FileProvider"
  23. android:authorities="com.example.retrofit_uploadmediafiles.fileprovider"
  24. android:exported="false"
  25. android:grantUriPermissions="true">
  26. <meta-data
  27. android:name="android.support.FILE_PROVIDER_PATHS"
  28. android:resource="@xml/provider_paths"/>
  29. </provider>
  30. </application>

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

  1. android {
  2. compileSdkVersion 29
  3. defaultConfig {
  4. applicationId "com.example.retrofit_uploadmediafiles"
  5. minSdkVersion 23
  6. targetSdkVersion 29
  7. versionCode 1
  8. versionName "1.0"
  9. testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
  10. multiDexEnabled true
  11. }
  12. buildTypes {
  13. release {
  14. minifyEnabled false
  15. proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
  16. }
  17. }
  18. }
  19. dependencies {
  20. implementation fileTree(dir: "libs", include: ["*.jar"])
  21. implementation 'androidx.appcompat:appcompat:1.1.0'
  22. implementation 'androidx.core:core:1.3.0'
  23. implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
  24. implementation 'androidx.test:runner:1.2.0'
  25. testImplementation 'junit:junit:4.12'
  26. androidTestImplementation 'androidx.test.ext:junit:1.1.1'
  27. androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
  28. implementation 'com.afollestad.material-dialogs:core:0.9.4.5'
  29. implementation 'com.github.bumptech.glide:glide:4.11.0'
  30. androidTestImplementation 'com.android.support.test:runner:1.0.2'
  31. implementation "androidx.multidex:multidex:2.0.1"
  32. implementation 'com.squareup.retrofit2:retrofit:2.9.0'
  33. implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
  34. implementation 'com.rengwuxian.materialedittext:library:2.1.4'
  35. implementation group: 'commons-io', name: 'commons-io', version: '2.7'
  36. }

My Main Activity:

  1. public class MainActivity extends AppCompatActivity implements View.OnClickListener{
  2. private Button image, video, pdf;
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. MultiDex.install(this);
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_main);
  8. image = findViewById(R.id.image);
  9. video = findViewById(R.id.video);
  10. pdf = findViewById(R.id.Pdf);
  11. if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
  12. image.setEnabled(false);
  13. video.setEnabled(false);
  14. pdf.setEnabled(false);
  15. ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
  16. } else {
  17. image.setEnabled(true);
  18. video.setEnabled(true);
  19. pdf.setEnabled(true);
  20. }
  21. image.setOnClickListener(this);
  22. video.setOnClickListener(this);
  23. pdf.setOnClickListener(this);
  24. }
  25. @Override
  26. public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
  27. if(requestCode == 0 ) {
  28. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  29. image.setEnabled(true);
  30. video.setEnabled(true);
  31. pdf.setEnabled(true);
  32. }
  33. }
  34. }
  35. @Override
  36. public void onClick(View view) {
  37. switch (view.getId()) {
  38. case R.id.image:
  39. Intent intent = new Intent(this, ImageActivity.class);
  40. startActivity(intent);
  41. break;
  42. case R.id.video:
  43. //Intent intent2 = new Intent(this, VideoActivity.class);
  44. //startActivity(intent2); I am trying to make the image upload work for now
  45. break;
  46. case R.id.Pdf:
  47. //Intent intent3 = new Intent(this, PdfActivity.class);
  48. //startActivity(intent3);
  49. break;
  50. }
  51. }
  52. }

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

  1. configurations.all {
  2. resolutionStrategy {
  3. force 'androidx.core:core:1.8.0'
  4. }
  5. }

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

英文:

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

  1. configurations.all {
  2. resolutionStrategy {
  3. force &#39;androidx.core:core:1.8.0&#39;
  4. }
  5. }

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:

确定