2

I am very new to java, but for some reason, trying to create an app the app is building properly in android studio, but, when trying to open in an emulator, it is crashing. This is my complete code

is the zip of the app, with I guess, the main error is

final View rootView = inflater.inflate(R.layout.fragment_sun, container, false); 

Logcat: Error

2019-07-25 10:42:48.002 5383-5383/com.example.phocast E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.phocast, PID: 5383
    android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class android.support.v7.widget.RecyclerView
    Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.v7.widget.RecyclerView
    Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v7.widget.RecyclerView" on path: DexPathList[[zip file "/data/app/com.example.phocast-j1KYRRVBUGO2ddQzmNBB8w==/base.apk", zip file "/data/app/com.example.phocast-j1KYRRVBUGO2ddQzmNBB8w==/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.phocast-j1KYRRVBUGO2ddQzmNBB8w==/split_lib_resources_apk.apk", zip file "/data/app/com.example.phocast-j1KYRRVBUGO2ddQzmNBB8w==/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.phocast-j1KYRRVBUGO2ddQzmNBB8w==/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.phocast-j1KYRRVBUGO2ddQzmNBB8w==/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.phocast-j1KYRRVBUGO2ddQzmNBB8w==/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.phocast-j1KYRRVBUGO2ddQzmNBB8w==/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.phocast-j1KYRRVBUGO2ddQzmNBB8w==/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.phocast-j1KYRRVBUGO2ddQzmNBB8w==/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.phocast-j1KYRRVBUGO2ddQzmNBB8w==/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.phocast-j1KYRRVBUGO2ddQzmNBB8w==/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.phocast-j1KYRRVBUGO2ddQzmNBB8w==/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.phocast-j1KYRRVBUGO2ddQzmNBB8w==/lib/x86, /system/lib, /vendor/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:125)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at android.view.LayoutInflater.createView(LayoutInflater.java:606)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at com.example.phocast.ui.main.SunFragment.onCreateView(SunFragment.java:69)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2439)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
        at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)

on line 69 of SunFragment.java code.

I don't understand what went wrong in the XML.

The corresponding XML is:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_recycler_view"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:android="http://schemas.android.com/apk/res/android" />

can someone please check?

build.gradel

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.1"
    defaultConfig {
        applicationId "com.example.phocast"
        minSdkVersion 21
        targetSdkVersion 29
        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 'androidx.appcompat:appcompat:1.0.2'
    implementation 'com.google.android.material:material:1.0.0'
    implementation'com.google.android.gms:play-services-places:17.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'org.shredzone.commons:commons-suncalc:2.6'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'androidx.recyclerview:recyclerview:1.0.0'
    implementation 'com.github.bumptech.glide:glide:3.7.0'
    implementation 'com.google.android.gms:play-services-maps:17.0.0'
    implementation 'com.google.android.gms:play-services-location:17.0.0'
}
BaRud
  • 3,055
  • 7
  • 41
  • 89

3 Answers3

3

Use

androidx.recyclerview.widget.RecyclerView

in fragment_sun.xml instead of

android.support.v7.widget.RecyclerView

Because as in build.gradle dependency is androidx.recyclerview:recyclerview:1.0.0 but in SunFragment RecyclerView is imported from androidx

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • I have tried that, but without any improvement, as android studio is suggesting for androidx. One thing is, when I am trying to refractor, this says "No usage found". – BaRud Jul 25 '19 at 05:21
  • Also, when I tried to change the card_item.xml to androidx, I got error`error: not well-formed (invalid token). /home/rudra/Projects/Phocast/app/src/main/res/layout/card_item.xml: error: file failed to compile.` – BaRud Jul 25 '19 at 05:24
  • @BaRud :When we use "Migrate to androidx" then problem occurred in xml files. i have faced same issue. you need to do it manually for xml files – ρяσѕρєя K Jul 25 '19 at 05:24
  • @BaRud: use `androidx.cardview.widget.CardView` for card view. when you type view name like `... ` – ρяσѕρєя K Jul 25 '19 at 05:26
  • Try to invalidate cache, restart, rebuild project and then use androidx. – Bek Jul 25 '19 at 05:36
  • This explanation is wrong. "but in `SunFragment` RecyclerView is imported from `androidx`". – shizhen Jul 25 '19 at 05:55
  • @shizhen : i have checked `SunFragment ` class `RecyclerView ` is imported from `androidx` , if we change it to support library then code will work without changing in xml file – ρяσѕρєя K Jul 25 '19 at 05:58
  • Note that the gradle dependency declares with androidx. the root cause is inside xml layout file. If you change to support library, then you have to change the dependency declaration as well. – shizhen Jul 25 '19 at 06:00
2

You are using androidx support libraries so use androidx.recyclerview.widget.RecyclerView

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/rv_recycler_view"
    android:scrollbars="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android" />
Manohar
  • 22,116
  • 9
  • 108
  • 144
1

Below screenshot is from your project search result:

enter image description here

Try to replace all those android.support.v7 packages with the corresponding androidx package

See Migrating to AndroidX for detailed package mappings.

E.g. Change

android.support.v7.widget.RecyclerView

to

androidx.recyclerview.widget.RecyclerView

This is because your source code is using AndroidsX package but your xml layout declare with android support package which is wrong.

shizhen
  • 12,251
  • 9
  • 52
  • 88