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)