3

I'm working on an Android application in Android Studio and I'm struggling to get my new fragment to display. In the logcat I see an error but I'm not sure what it means and can't find the answer to this issue anywhere else. I'm not sure if I'm making other errors in trying to set up this fragment.

I've tried only adding the fragment programmatically but found other examples on YouTube of adding the fragment to XML so I've tried that too and it didn't work either. I've been trying to work this out for days so any help would be greatly appreciated.

I tried starting the new fragment using fragmentTransaction.add and fragmentTransaction.replace with no difference.

Here is the main class

public class Dashboard extends AppCompatActivity {

    Button menuButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dashboard);

        menuButton = (Button) findViewById(R.id.menu_button);

        menuButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                startClassMenu();

            }
        });
    }

    public void startClassMenu(){

        Fragment classMenuFragment = new ClassMenuFragment();
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.class_menu_fragment_place, classMenuFragment);
        fragmentTransaction.commit();
    }
}

Here is the main class xml

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginStart="0dp"
    android:layout_marginLeft="0dp"
    android:layout_marginTop="0dp"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    android:theme="?attr/actionBarTheme" />

<Button
    android:id="@+id/menu_button"
    android:layout_width="40dp"
    android:layout_height="34dp"
    android:layout_alignParentStart="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginStart="21dp"
    android:layout_marginLeft="21dp"
    android:layout_marginTop="82dp"
    android:background="@drawable/icon_blue_menu" />

<Button
    android:id="@+id/home_button"
    android:layout_width="40dp"
    android:layout_height="38dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_marginTop="6dp"
    android:layout_marginEnd="16dp"
    android:layout_marginRight="16dp"
    android:background="@drawable/home_icon" />

<Button
    android:id="@+id/settings_button"
    android:layout_width="37dp"
    android:layout_height="33dp"
    android:layout_alignBottom="@+id/menu_button"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_marginEnd="100dp"
    android:layout_marginRight="100dp"
    android:layout_marginBottom="72dp"
    android:background="@drawable/icon_settings" />

<fragment
    android:id="@+id/class_menu_fragment_place"
    android:name="teamingenium.ingeniummobileapplication.fragments.ClassMenuFragment"
    android:layout_width="177dp"
    android:layout_height="187dp"
    android:layout_alignEnd="@+id/menu_button"
    android:layout_alignParentTop="true"
    android:layout_marginTop="135dp"
    android:layout_marginEnd="-127dp" />

Here is the fragment Java class

public class ClassMenuFragment extends Fragment {

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

    View view = inflater.inflate(R.layout.fragment_class_menu, container, false);

    return view;

}
} 

And here is the fragment xml class

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragments.ClassMenuFragment"
     android:id="@+id/fragment_class_menu">

<TextView
    android:id="@+id/fragment_class_menu_text_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:text="Fragment Started" />

</FrameLayout>

Here is the error from logcat

2018-12-29 20:09:48.405 1925-1925/system_process E/LoadedApk: Unable to instantiate appComponentFactory
    java.lang.ClassNotFoundException: Didn't find class "android.support.v4.app.CoreComponentFactory" on path: DexPathList[[],nativeLibraryDirectories=[/system/priv-app/GoogleSdkSetup/lib/x86, /system/lib, /system/lib]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
    at android.app.LoadedApk.createAppFactory(LoadedApk.java:226)
    at android.app.LoadedApk.updateApplicationInfo(LoadedApk.java:338)
    at android.app.ActivityThread.handleDispatchPackageBroadcast(ActivityThread.java:5388)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1733)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at com.android.server.SystemServer.run(SystemServer.java:454)
    at com.android.server.SystemServer.main(SystemServer.java:294)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:838)
Vermox
  • 57
  • 1
  • 1
  • 5
  • May be it will help https://stackoverflow.com/questions/57700486/didnt-find-class-androidx-core-app-corecomponentfactory – Alex77 Feb 13 '20 at 09:52

2 Answers2

1

refer to this link fragment

As you use <fragment in your layout xml, you have no need to invoke startClassMenu() ,just to delete the startClassMenu() method and try again.

Try other two methods:

1, try to use android.app.Fragment and android.app.FragmentManager and android.app.Activity instead of using support library.

2, try to add -keep public class * extends android.support.v4.** in your proguard-rules.pro file

Tang HuaiZhe
  • 134
  • 5
0

Try these

After checking this "DexPathList", it is definitely due to dexes.

multidex increases the number of methods

The Dalvik Executable specification limits the total number of methods that can be referenced within a single DEX file to 65,536

This error can be caused due to using both androidx and legacy android support libraries

Just open project structure and go to Dependencies and select All Modules and check is there any library which contains android.support

Just replace that with androidx version for that library

If still that issue exists then

Try these to fix this issue

dependencies {
    def multidex_version = "2.0.1"
    implementation "androidx.multidex:multidex:$multidex_version"
}

in the latest versions of android studio, just REMOVE these lines if it is there inside the manifest file

<application
...
tools:replace="android:appComponentFactory"  // REMOVE THIS
android:appComponentFactory="androidx"       // REMOVE THIS
>

Check for this line

 defaultConfig {
        ...
        multiDexEnabled true
    }

Add this line in your MainActivity.kt

class MyApplication : Application() {

    override fun attachBaseContext(base: Context) {
        super.attachBaseContext(base)
        MultiDex.install(this)  //ADD THIS
    }
}

Add this in your proguard-rules.pro file

-keep class com.example.** { *; }

Verify in this

 buildTypes {
        dev{
            minifyEnabled false
             }
        release {
            minifyEnabled true
             }
    }

In latest version of android studio there is no instant run disable options

Now android studio requires jdk versions above 8 so NO NEED TO ADDING THIS

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
    jvmTarget = "1.8"
}