Hello currently I am working on a Android project on Android Studio where I can switch Fragments using FragmentPagerAdapter, using root fragments for each, so when I was using one fragment it worked perfectly but when i wanted to implement the second one it gave me the following error;
java.lang.IllegalArgumentException: No view found for id 0x7f0c00ae (net.beardboy.aarcapp:id/root_frame_graphs) for fragment GraphsFragment{d178606 #0 id=0x7f0c00ae}
I swap between fragments using getSupportFragmentManager(), as I said it worked fine the first time I used but then it gave me error. this is my code:
public class MainActivity extends AppCompatActivity implements ReportsAdapter.ReportCallBacks {
ProgressDialog progressDialogReports;
ProgressDialog progressDialogGraphs;
ProgressDialog progressDialogSettings;
DrawerLayout drawerLayout;
ViewPager viewPager;
FragmentPagerAdapter fragmentPagerAdapter;
Toolbar toolbar;
User currentUser;
SessionStateManager sessionStateManager;
RequestQueue requestQueue;
ReportsFragments reportsFragments;
GraphsFragment graphsFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sessionStateManager = new SessionStateManager(this);
currentUser = new User();
currentUser = sessionStateManager.getCurrentUser();
requestQueue = Volley.newRequestQueue(this);
graphsFragment = new GraphsFragment();
toolbar = (Toolbar) findViewById(R.id.toolbar_main);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabBarLayout);
viewPager = (ViewPager) findViewById(R.id.viewPager);
setSupportActionBar(toolbar);
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
fragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
private static final int FRAGMENT_COUNT = 3;
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
try {
getReportsdata();
} catch (JSONException e) {
e.printStackTrace();
}
return new RootFragmentReports();
case 1:
getGraphsdata();
return new RootFragmentGraphs();
case 2:
return new RootFragmentSettings();
}
return null;
}
@Override
public int getCount() {
return FRAGMENT_COUNT;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Reportes";
case 1:
return "Graficas";
case 2:
return "Pagos";
default:
return null;
}
}
};
viewPager.setOffscreenPageLimit(3);
viewPager.setAdapter(fragmentPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
invalidateOptionsMenu();
}
@Override
public void onPageSelected(int position) {
invalidateOptionsMenu();
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
public void getReportsdata() throws JSONException {
progressDialogReports = ProgressDialog.show(MainActivity.this, getString(R.string.please_wait), getString(R.string.downloading_data), true, false);
JSONObject jsonUser = new JSONObject();
JSONObject jsonMain = new JSONObject();
jsonUser.put(JSONKeys.KEY_ACCESS_TOKEN, currentUser.getAccess_token());
jsonUser.put(JSONKeys.KEY_CLIENTE_ID, currentUser.getCilenteId());
jsonUser.put(JSONKeys.KEY_NAME, currentUser.getName());
jsonUser.put(JSONKeys.KEY_EMAIL, currentUser.getEmail());
jsonMain.put(JSONKeys.KEY_AARC_USER, jsonUser);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, JSONKeys.URL_REPORTES_POR_CLIENTE, jsonMain, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
progressDialogReports.dismiss();
try {
ArrayList<JSONReport> jsonReports = (ArrayList<JSONReport>) JSONParser.parseJSONReport(response);
replaceFragment(jsonReports);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressDialogReports.dismiss();
Log.i("errr", error.toString());
DialogUtil.createSimpleDialog(MainActivity.this, getString(R.string.error_downloading_data), error.toString()).show();
}
});
int socketTimeout = 180000;//3 minutes - change to what you want
RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
jsonObjectRequest.setRetryPolicy(policy);
requestQueue.add(jsonObjectRequest);
}
public void getGraphsdata() {
ArrayList<Graph> graphArrayList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Graph graficas = new Graph();
graficas.setAnalisis(i);
graficas.setSucursal("descripcion" + i);
graphArrayList.add(graficas);
}
ArrayList<Entry> entries = new ArrayList<>();
entries.add(new Entry(2f, 0));
entries.add(new Entry(4f, 1));
entries.add(new Entry(3f, 2));
entries.add(new Entry(4f, 3));
LineDataSet dataset = new LineDataSet(entries, "Fecha");
ArrayList<String> labels = new ArrayList<String>();
labels.add("January");
labels.add("February");
labels.add("January");
labels.add("February");
replaceFragmentGraph(graphArrayList);
}
private void replaceFragment(ArrayList<JSONReport> jsonReports) {
reportsFragments = new ReportsFragments();
ReportsAdapter reportsAdapter;
reportsAdapter = new ReportsAdapter(MainActivity.this, jsonReports);
reportsAdapter.setReportAdapterCallBacks(MainActivity.this);
reportsFragments.setComplaintsAdapter(reportsAdapter);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.root_frame_actividades, reportsFragments)
.commit();
}
private void replaceFragmentGraph(ArrayList<Graph> datos) {
graphsFragment = new GraphsFragment();
//Bundle bundle=new Bundle();
//bundle.putParcelable("datos", (Parcelable) datos);
//graphsFragment.setArguments(bundle);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.root_frame_graphsa, graphsFragment)
//.addToBackStack(null)
.commit();
}
@Override
public void OnReportCallBacks(JSONReport jsonReport) {
Intent intent = new Intent(this, ReportsDetailActivity.class);
intent.putExtra(JSONKeys.KEY_FOLIOB, jsonReport.getFoliob());
startActivity(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_acitivity_main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
return super.onOptionsItemSelected(item);
}
}
The error started to appear when I added the GraphsFragment in the getSupportFragmentManager(), I double checked the layouts's ID are the correct ones and so, but since in the error the ID 0x7f0c00ae and later on the same line the same id appears again, forgive me for my bad english. Thank you all.
EDIT My GraphsFragment class
public class GraphsFragment extends Fragment {
LineChart chart;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.fragment_graphs, container, false);
chart = (LineChart) rootview.findViewById(R.id.linechart);
return rootview;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//code
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
}
fragments_graphs.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/linechart"
android:layout_width="match_parent"
android:layout_height="220dp"
android:layout_margin="5dp" />
</RelativeLayout
RootFragmentGraphs class
public class RootFragmentGraphs extends Fragment{
private static final String TAG = "RootFragmentGraphs";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_root_fragment_graficas, container, false);
return view;
}
}
fragment_root_fragment_graficas.xml
<?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:id="@+id/root_frame_graphs"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.RootFragmentGraphs">
</FrameLayout>