I have a Button inside a Fragment, I want this Button to be redirected to another Fragment (Activity) where I will have a ListView, but when I click it the app closes. Any solution?
I get this error in the Fragment(Logcat):
2021-08-17 21:20:10.631 10882-10882/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.pibeds, PID: 10882
java.lang.IllegalArgumentException: No view found for id 0x7f080118 (com.example.pibeds:id/nav_host_fragment_container) for fragment corario_chico{eab363c} (93f305dd-c0c9-46d9-94ab-d2425c8f18f8) id=0x7f080118}
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:315)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1187)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1356)
at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1434)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1497)
at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:447)
at androidx.fragment.app.FragmentManager.executeOps(FragmentManager.java:2169)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1992)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1947)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849)
at androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:413)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Window1.java I want to redirect to a second window from this window, but when I try to do so, the app closes.
package com.example.pibeds;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class corario_chico extends Fragment{
private Button button;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.corario_chico, container, false);
button = view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MainActivity.fragmentManager.beginTransaction().replace(R.id.nav_host_fragment_container,new corario_chico(), null).commit();
}
});
return view;
}
}
MainActivity.java Here I have everything, I don't know if something happened to me to declare, if you can tell me what I'm doing wrong I would really appreciate it.
package com.example.pibeds;
import android.os.Bundle;
import android.view.View;
import android.view.Menu;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.navigation.NavigationView;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import com.example.pibeds.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity {
public static FragmentManager fragmentManager;
private AppBarConfiguration mAppBarConfiguration;
private ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
setSupportActionBar(binding.appBarMain.toolbar);
binding.appBarMain.fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = binding.drawerLayout;
NavigationView navigationView = binding.navView;
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.corario_grande, R.id.corario_chico)
.setOpenableLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
navigationView.setItemIconTintList(null);
fragmentManager = getSupportFragmentManager();
if(findViewById(R.id.nav_host_fragment_container)!=null){
if(savedInstanceState!=null)
{
return;
}
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
corario_chico corario_chico = new corario_chico();
fragmentTransaction.add(R.id.nav_host_fragment_container, corario_chico, null);
fragmentTransaction.commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
}
xml This is where I have the redirect button but it doesn't work :( I still don't know why
<?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=".corario_chico">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button" />
</FrameLayout>
I'm starting in Android Studio, sorry if I made a mistake in something very simple but this is new to me. Thank you very much for your help.
According to the error message:
This is raised when you perform the transaction, by loading the Fragment
corario_chico
:The problem indicates that in the Fragment you are trying to obtain the reference of a resource that does not exist, to know what the resource is, open your generated class
R.java
and look for0x7f080118
, there you will be able to see the id of the resource that does not exist when loading your Fragment.Do this and don't forget to mark as answered if it works for you