The crash is when i switch the page from 2 to 0 or 1.
And when i Comment the auto Complete Fragment at activity_maps.xml
Please help me.
--------- beginning of crash
12-04 01:31:01.005 12693-12693/com.handsomelee.gotroute E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.handsomelee.gotroute, PID: 12693
android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
Caused by: java.lang.IllegalArgumentException: Binary XML file line #10: Duplicate id 0x7f0f00e4, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.location.places.ui.PlaceAutocompleteFragment
at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2315)
at android.app.FragmentController.onCreateView(FragmentController.java:98)
at android.app.Activity.onCreateView(Activity.java:5901)
at android.support.v4.app.BaseFragmentActivityApi14.onCreateView(BaseFragmentActivityApi14.java:41)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:67)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:777)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at com.handsomelee.gotroute.Services.GoogleMapSystem.onCreateView(GoogleMapSystem.java:41)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2261)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1750)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:792)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2590)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2377)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2332)
at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:2209)
at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:649)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:145)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1238)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:663)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:625)
at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:617)
at com.handsomelee.gotroute.MainActivity$1.onTabSelected(MainActivity.java:73)
at android.support.design.widget.TabLayout.dispatchTabSelected(TabLayout.java:1165)
at android.support.design.widget.TabLayout.selectTab(TabLayout.java:1158)
at android.support.design.widget.TabLayout.selectTab(TabLayout.java:1128)
at android.support.design.widget.TabLayout$Tab.select(TabLayout.java:1427)
at android.support.design.widget.TabLayout$TabView.performClick(TabLayout.java:1537)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
--------- beginning of system
this is my MainActivity.java
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
if(tab1 == null){
tab1 = new MapsActivity(R.id.mapView, R.layout.activity_maps, GoogleMap.MAP_TYPE_NORMAL);
}
return tab1;
case 1:
if(tab2 == null)
tab2 = new CarParkingActivity();
return tab2;
case 2:
if(tab3 == null)
tab3 = new ReportActivity();
return tab3;
default:
return null;
}
}
#Override
public int getCount() {
return 3;
}
MapsActivity.java
#Override
public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
rootView = layoutInflater.inflate(layoutActivityId, viewGroup, false);
mapView = ((MapView) rootView.findViewById(mapViewId));
mapView.onCreate(bundle);
mapView.getMapAsync(this);
addOn();
return rootView;
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMapType(googleMapType);
mMap.setOnMyLocationButtonClickListener(this);
mMap.setOnMyLocationClickListener(this);
mMap.setOnMapLongClickListener(this);
}
}
activity_maps.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.handsomelee.gotroute.Controller.ReportActivity">
<fragment
android:layout_width="0dp"
android:layout_height="50dp"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
android:id="#+id/placeSearch"
android:layout_weight="0.67"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"/>
<com.google.android.gms.maps.MapView
android:layout_width="0dp"
android:layout_height="0dp"
android:id="#+id/mapView"
android:layout_weight="0.78"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:apiKey="#string/test_google_api"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintTop_toBottomOf="#+id/placeSearch"/>
</android.support.constraint.ConstraintLayout>
ReportActivity.java
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_report, container, false);
spinner = (Spinner) rootView.findViewById(R.id.spinner);
String[] array_Spiner = getResources().getStringArray(R.array.reports_array);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(rootView.getContext(), R.layout.support_simple_spinner_dropdown_item, array_Spiner);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
~
});
return rootView;
}
activity_report.xml
<?xml version="1.0" encoding="utf-8"?>`
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.handsomelee.gotroute.Controller.ReportActivity">
<Spinner
android:layout_width="0dp"
android:layout_height="50dp"
android:id="#+id/spinner"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:overlapAnchor="false"
android:dropDownWidth="match_parent"
app:layout_constraintHorizontal_bias="0.0"/>
</android.support.constraint.ConstraintLayout>
SOLVED.
The problem seems like is the fragment id duplicated when the view is created.
so I add the Code below at my MapActivity to remove it.
#Override
public void onDestroyView() {
super.onDestroyView();
android.app.FragmentManager fm = getActivity().getFragmentManager();
android.app.Fragment fragment = fm.findFragmentById(R.id.placeSearch);
android.app.FragmentTransaction ft = fm.beginTransaction();
ft.remove(fragment);
ft.commit();
}
Related
I'm making an app that uses Android studio to write and call messages. This error message appears during the process of inflating the recycler view into the fragment.
How can I fix it? Why can't this be inflated?
here is error messages...
java.lang.RuntimeException: Unable to start activity ComponentInfo{kr.hnu.project/kr.hnu.project.NavigationActivity}: android.view.InflateException: Binary XML file line #11 in kr.hnu.project:layout/activity_navigation: Binary XML file line #10 in kr.hnu.project:layout/content_navigation: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: android.view.InflateException: Binary XML file line #11 in kr.hnu.project:layout/activity_navigation: Binary XML file line #10 in kr.hnu.project:layout/content_navigation: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #10 in kr.hnu.project:layout/content_navigation: Error inflating class fragment
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void kr.hnu.project.MyRecyclerAdapter.notifyDataSetChanged()' on a null object reference
at kr.hnu.project.ui.home.HomeFragment.setArray(HomeFragment.java:83)
at kr.hnu.project.ui.home.HomeFragment.onCreateView(HomeFragment.java:52)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2995)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:523)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:261)
at androidx.fragment.app.FragmentStore.moveToExpectedState(FragmentStore.java:113)
and this HomeFragment that the recycler view should see.
public class HomeFragment extends Fragment {
private final static String selectMsg = "SELECT sender, receiver, title, date, content FROM MessageDB";
RelativeLayout rel;
LinearLayout lin;
DBHelper dbHelper;
SQLiteDatabase readDB;
Cursor cursor;
ArrayList<MyItem> mailItem;
MyRecyclerAdapter myAdapter;
private FragmentHomeBinding binding;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
HomeViewModel homeViewModel =
new ViewModelProvider(this).get(HomeViewModel.class);
binding = FragmentHomeBinding.inflate(inflater, container, false);
View root = binding.getRoot();
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_home, container, false);
dbHelper = new DBHelper(inflater.getContext());
readDB = dbHelper.getReadableDatabase();
mailItem = new ArrayList<MyItem>();
setArray();
myAdapter = new MyRecyclerAdapter(getActivity(), mailItem);
RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(myAdapter);
return root;
}
public void setArray() {
cursor = readDB.rawQuery(selectMsg, null);
mailItem.clear();
while (cursor.moveToNext()) {
mailItem.add(new MyItem(cursor.getString(cursor.getColumnIndexOrThrow("sender")), cursor.getString(cursor.getColumnIndexOrThrow("receiver")),
cursor.getString(cursor.getColumnIndexOrThrow("title")), cursor.getString(cursor.getColumnIndexOrThrow("date")),
cursor.getString(cursor.getColumnIndexOrThrow("content"))));
}
cursor.close();
//myAdapter.notifyDataSetChanged();
}
This is Navigation Activity with home fragment.
public class NavigationActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
private AppBarConfiguration mAppBarConfiguration;
private ActivityNavigationBinding binding;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dataToFragment();
binding = ActivityNavigationBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
setSupportActionBar(binding.appBarNavigation.toolbar);
DrawerLayout drawer = binding.drawerLayout;
NavigationView navigationView = binding.navView;
navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
//
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_send, R.id.nav_set, R.id.nav_home)
.setOpenableLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_navigation);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
this is activity_navigation.xml in error message
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
android:id="#+id/app_bar_navigation"
layout="#layout/app_bar_navigation"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_navigation"
app:menu="#menu/activity_navigation_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>
this is content_navigation.xml in error message
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/app_bar_navigation">
<fragment
android:id="#+id/nav_host_fragment_content_navigation"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/mobile_navigation" /
</androidx.constraintlayout.widget.ConstraintLayout>
and this is homeFragment's xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/linear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ui.home.HomeFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
because my reputation is less then 50 ,I can't comment on your post so i will comment here .
What does this method do? dataToFragment();
you are calling it before inflating the layout
I have an application which which uses swipe views. I used the template provided by android studio to create the swipe views. The template provided by android studio provides 3 fragments to swipe between which are working fine, I can swipe back and fourth between these no problem.
The problem I am having is creating a 4th fragment. I have created the Java file, the XML and have updated my MainActivity to account for the 4th tab. The 4th tab is showing in the menu and I can swipe to the 4th tab. But, when I try to swipe back to any of the other tabs from the 4th tab the app crashes.
The error I am getting in the Android Monitor is:
12-07 14:08:11.951 2586-2586/com.example.application.placepicker
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.application.placepicker, PID: 2586
android.view.InflateException: Binary XML file line #11: Binary XML file
line #11: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #11: Error
inflating class fragment
Caused by: java.lang.IllegalArgumentException: Binary XML file line #11:
Duplicate id 0x7f0d00a1, tag null, or parent id 0xffffffff with another
fragment for com.google.android.gms.maps.SupportStreetViewPanoramaFragment
at
android.support.v4.app.FragmentManagerImpl.onCreateView
(FragmentManager.java:3447)
at
android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView
(LayoutInflaterCompatHC.java:47)
at android.view.LayoutInflater$FactoryMerger.onCreateView
(LayoutInflater.java:189)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:769)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at com.example.application.placepicker.Tab2.onCreateView(Tab2.java:29)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2192)
at android.support.v4.app.FragmentManagerImpl.moveToState
(FragmentManager.java:1299)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState
(FragmentManager.java:1528)
at android.support.v4.app.BackStackRecord.executeOps
(BackStackRecord.java:753)
at android.support.v4.app.FragmentManagerImpl.executeOps
(FragmentManager.java:2363)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether
(FragmentManager.java:2149)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps
(FragmentManager.java:2103)
at android.support.v4.app.FragmentManagerImpl.execSingleAction
(FragmentManager.java:1984)
at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss
(BackStackRecord.java:626)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate
(FragmentPagerAdapter.java:143)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1268)
at android.support.v4.view.ViewPager.setCurrentItemInternal
(ViewPager.java:668)
at android.support.v4.view.ViewPager.setCurrentItemInternal
(ViewPager.java:630)
at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:611)
at android.support.design.widget.TabLayout
$ViewPagerOnTabSelectedListener.onTabSelected(TabLayout.java:2191)
at android.support.design.widget.TabLayout.dispatchTabSelected
(TabLayout.java:1164)
at android.support.design.widget.TabLayout.selectTab(TabLayout.java:1157)
at android.support.design.widget.TabLayout.selectTab(TabLayout.java:1127)
at android.support.design.widget.TabLayout$Tab.select(TabLayout.java:1426)
at android.support.design.widget.TabLayout$TabView.performClick
(TabLayout.java:1536)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit
$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
My MainActivity is:
public class TabbedActivity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabbed);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_tabbed, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Tab1 tab1 = new Tab1();
return tab1;
case 1:
Tab2 tab2 = new Tab2();
return tab2;
case 2:
Tab3 tab3 = new Tab3();
return tab3;
case 3:
Tab4 tab4 = new Tab4();
return tab4;
}
return null;
}
#Override
public int getCount() {
return 4;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Places";
case 1:
return "Street View";
case 2:
return "Steps";
case 3:
return "Weather";
}
return null;
}
}
}
My Tab4.java is:
public class Tab4 extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab4, container, false);
return rootView;
}
}
Anything else you need to know to find a solution please let me know
Thanks
Edit:
This is the XML file of tab4
<RelativeLayout 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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.application.placepicker.TabbedActivity$PlaceholderFragment">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Weather Fragment" />
</RelativeLayout>
This is the XML of Tab1 which is working fine:
<RelativeLayout 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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.application.placepicker.TabbedActivity$PlaceholderFragment">
<TextView
android:id="#+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/button_places"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/mybutton"
android:textColor="#android:color/white"
android:text="Launch place picker"
android:width="350dp"
android:layout_centerVertical="true"
android:layout_toEndOf="#+id/section_label" />
<TextView
android:id="#+id/tvPlace"
android:layout_marginTop="64dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif"
android:textAlignment="center"
android:textColor="#3F51B5"
android:textSize="24dp"
android:layout_below="#+id/textViewUserEmail"
android:layout_alignParentStart="true" />
</RelativeLayout>
Can somebody explain what is the problem here? The error is bellow:
Code in MainAcitivity:
FragmentManager fm = getSupportFragmentManager();
SvePonudeFragment fragment = (SvePonudeFragment) fm.findFragmentById(R.id.ponudice);
fragment.reloadData();
Fragment Class:
public class SvePonudeFragment extends Fragment {
private RecyclerView rv;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.sve_ponude_fragment, container, false);
rv = (RecyclerView) rootView.findViewById(R.id.rv);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
rv.setLayoutManager(llm);
ArrayList<Ponuda> listaPonuda = null;
initializeAdapter(listaPonuda);
return rootView;
}
private void initializeAdapter(List<Ponuda> preuzetePonude){
RVAdapter adapter = new RVAdapter(preuzetePonude);
rv.setAdapter(adapter);
}
public void reloadData(){
System.out.println("I ENTEEERED");
}
}
XML of fragment:
<?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"
android:id="#+id/ponudice">
<android.support.v7.widget.RecyclerView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/rv"
>
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
What is wrong with this? Can somebody explain me?
this is the error:
W/System.err: java.lang.NullPointerException
W/System.err: at com.example.filip.dajsve.Activities.MainActivity.onDataLoaded(MainActivity.java:190)
W/System.err: at com.example.filip.dajsve.Loaders.DatabaseDataLoader.loadData(DatabaseDataLoader.java:22)
I found the problem. The problem is that you don't link your fragment with mainActivity. first you should add your fragment in xml of mainActivty and give the fragment an id then access it in the main activity with the id of fragment not using this id android:id="#+id/ponudice"> because this id related with relative layout not the fragment. check the xml code of main activity you should find fragment tags like code below
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="theNameOfYourPackage.theNameOfFragment"
android:id="#+id/ponudice_in_main_activity"/>
Do it like this
FragmentManager fm = getSupportFragmentManager();
SvePonudeFragment fragment = new SvePonudeFragment();
fm.beginTransaction().replace(R.id.framecontainer,fragment).commit();
and your activity_main.xml file will be
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/framecontainer"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:padding="16dp"
>
</FrameLayout>
I have a fragment and I am inflating it like below, but it is giving runtime error:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View jobsView = inflater.inflate(R.layout.fragment_job_information, container, false);
textView = (TextView) jobsView.findViewById(R.id.textView);
if (jobInfo == null) {
EmployerHistory jobsQuery=new EmployerHistory();
jobInfo = jobsQuery.getJobInfo(employerName, position);
}
textView.setText(jobInfo.get("jobName").toString());
return jobsView;
}
Its corresponding layout is:
<fragment 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" android:id="#+id/jobInfoFragment">
<RelativeLayout 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="com.example.ankit.job_depot.employer.view.JobInformation">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</fragment>
Actually I am already on a fragment with listview and when user clicks on an element of listview that fragment needs to get changed by a new fragment, in my new fragment I have:
l
istViewJobHistory.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
JobInformation newFragment = new JobInformation();
android.support.v4.app.FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.listViewJobHistory, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
I am getting the following exception:
07-28 18:49:39.681 14474-14474/com.example.ankit.job_depot
E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.ankit.job_depot, PID: 14474
android.view.InflateException: Binary XML file line #1: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at com.example.ankit.job_depot.employer.view.JobInformation.onCreateView(JobInformation.java:45)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
I think your main Activity should extend FragmentActivity
import android.support.v4.app.FragmentActivity;
I have created 2 fragment and their layout,
when i click button on profile fragment it should open flat layout, but
it force stop application and its not opening the second fragment,
i have included fragment code and its layout code..
profilefragment.java
public class ProfileFragment extends Fragment {
View rootView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.profile, container, false);
Button button = (Button) rootView.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
onButtonClicked(v);
}
});
return rootView;
}
public void onButtonClicked(View view)
{
//do your stuff here..
final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.button, new flatview(), "NewFragmentTag");
ft.commit();
}
profile.xml
<RelativeLayout 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="${packageName}.${activityClass}"
android:id="#+id/semester">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
flatview.java
public class flatview extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.flat, container, false);
}
flat.xml
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="second fragement"
android:id="#+id/textView3"
android:layout_gravity="center_horizontal" />
logcat
06-19 07:57:56.945 26457-26457/com.idealdeveloper.saltechnical E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.idealdeveloper.saltechnical, PID: 26457
java.lang.ClassCastException: android.widget.Button cannot be cast to android.view.ViewGroup
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:917)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Try this I guess it will work for you
<RelativeLayout 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="${packageName}.${activityClass}"
android:id="#+id/semester">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/fragmentLayout"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
Class
public class ProfileFragment extends Fragment {
View rootView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.profile, container, false);
Button button = (Button) rootView.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
onButtonClicked(v);
}
});
return rootView;
}
public void onButtonClicked(View view)
{
//do your stuff here..
final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.fragmentLayout, new flatview(), "NewFragmentTag");
ft.commit();
}
I hope this one will helps you
The fragment transaction inserts a View into a ViewGroup, so you can't just replace a Button. You could just replace the parent semester, since it is a ViewGroup.
public void onButtonClicked(View view) {
final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.semester, new flatview(), "NewFragmentTag");
ft.commit();
}
If your intention is also to remove the button, you will need to do it manually since the FragmentManager can only modify runtime transactions:
ViewGroup parent = (ViewGroup) getActivity().findViewById(R.id.semester);
parent.removeView(view);
You have to provide a ViewGroup root for the flat.xml layout , for example you can surround the TextView with a LinearLayout
flat.xml :
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="second fragement"
android:id="#+id/textView3"
android:layout_gravity="center_horizontal" />
</LinearLayout>