Android "No view found for id for fragment"
Solution 1
Okay i found an answer. The problem was that fragment cannot be a child of a 'Dialog'. Since i used popup dialog, it was unable to put fragments inside the dialog.
i solved by inflating views rather than using fragments in tablayout.
Solution 2
This error occurs because the fragment manager could not find the view on which it has to inflate the fragment.
The fragment transaction is linked to the activity so this error occurs as the frame layout is not a part of that main activity's xml.so it cant find where to add the fragment.fragment has to be added inside the activity.
what you have to do is provide the id of a view in your main activity. for e.g your main view
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mainView"//like this
android:orientation="vertical">
<include layout="@layout/nav_toolbar" />
<fragment
android:id="@+id/google_map"
class="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<include
android:id="@+id/footer_layout"
layout="@layout/footer_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="@+id/footer_for_places"
android:layout_width="match_parent"
android:layout_height="@dimen/keyboard_height"
android:background="@android:color/transparent"
android:orientation="vertical"
android:visibility="gone" />
</LinearLayout>
The view where you are trying to inflate the fragemnt must be inside activity. Now when you try
frManager.beginTransaction().add(R.id.mainView, ((Fragment) Fragment_zasin.class.newInstance()), tag).commit();
the fragment will be loaded on this view of your activity.
Solution 3
Problem is you are trying to show a Fragment
on a View
which is not defined in your layout which you have defined in your onCreate()
while setContentView(layoutId)
In your case you are inflating fragments in WriteRouteActivity
where layout defined is activity_write_route
and fragments are added on FrameLayout
which is defined in places_popup.xml
so define your framelayout
in a view layout of Activity.
Small Description :
--------code------
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
-------layout define for activity-------
setContentView(R.layout.activity_write_route);
}
now where you are adding fragment on a FrameLayout(view)
frManager.beginTransaction().add(R.id.frame_layout, ((Fragment) Fragment_zasin.class.newInstance()), tag).commit();
here R.id.frame_layout
should be define in your layout activity_write_route
.
The Dongster
Updated on June 17, 2022Comments
-
The Dongster about 2 years
Okay. I am stuck and having a headache... I am not sure how to access the other layout's view, since inflating does not work. Here are my codes.
WriteRouteActivity.java
public class WriteRouteActivity extends AppCompatActivity { private Toolbar tb; private TextView txt_toolbar_title; private Button btnSearchPlaces; private LinearLayout parentLayout, placesCoverLayout; private View popupView; private ImageView imgShowPlaces; private boolean isKeyBoardVisible; private int keyboardHeight; private EditText edtSearchPlaces; private PopupWindow popupWindow; //popupView private TabLayout tabLayout; private FrameLayout frameLayout; //prework private int minusVal; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_write_route); initView(); } private void initView() { //for activity and native back button tb = (Toolbar) findViewById(R.id.nav_toolbar); setSupportActionBar(tb); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayShowTitleEnabled(false); txt_toolbar_title = (TextView) findViewById(R.id.txt_toolbar); parentLayout = (LinearLayout) findViewById(R.id.layout_parent); placesCoverLayout = (LinearLayout) findViewById(R.id.footer_for_places); imgShowPlaces = (ImageView) findViewById(R.id.img_show_places); edtSearchPlaces =(EditText) findViewById(R.id.edt_search_place); btnSearchPlaces = (Button) findViewById(R.id.btn_search_place); popupView = getLayoutInflater().inflate(R.layout.places_popup, null); tabLayout = (TabLayout) popupView.findViewById(R.id.tab_layout); frameLayout = (FrameLayout) popupView.findViewById(R.id.frame_layout); doWorkForLayotus(); } private void doWorkForLayotus(){ final float popUpheight = getResources().getDimension(R.dimen.keyboard_height); changeKeyboardHeight((int) popUpheight); enablePopUpView(); setTabLayout(); checkKeyboardHeight(parentLayout); enableFooterView(); } public void setCurrentTabFragment(int position) throws IllegalAccessException, InstantiationException { String tag=""; Fragment fr = null; Class frClass = null; FragmentManager frManager = getSupportFragmentManager(); switch (position) { case 0: tag = "first"; //hide if(frManager.findFragmentByTag("second")!=null){ frManager.beginTransaction().hide(frManager.findFragmentByTag("second")).commit(); } if(frManager.findFragmentByTag("third")!=null){ frManager.beginTransaction().hide(frManager.findFragmentByTag("third")).commit(); } if(frManager.findFragmentByTag("fourth")!=null){ frManager.beginTransaction().hide(frManager.findFragmentByTag("fourth")).commit(); } //show if(frManager.findFragmentByTag("first")!=null){ frManager.beginTransaction().show(frManager.findFragmentByTag("first")).commit(); }else{ //add try { frManager.beginTransaction().add(frameLayout.getId(), ((Fragment) Fragment_zasin.class.newInstance()), tag).commit(); }catch(Exception e){ Log.e("why", e.getMessage().toString()); } } break; case 1: tag = "second"; //hide if(frManager.findFragmentByTag("first")!=null){ frManager.beginTransaction().hide(frManager.findFragmentByTag("first")).commit(); } if(frManager.findFragmentByTag("third")!=null){ frManager.beginTransaction().hide(frManager.findFragmentByTag("third")).commit(); } if(frManager.findFragmentByTag("fourth")!=null){ frManager.beginTransaction().hide(frManager.findFragmentByTag("fourth")).commit(); } //show if(frManager.findFragmentByTag("second")!=null){ frManager.beginTransaction().show(frManager.findFragmentByTag("second")).commit(); }else{ //add frManager.beginTransaction().add(frameLayout.getId(), ((Fragment) Fragment_zasin.class.newInstance()), tag).commit(); } break; case 2: tag = "third"; //hide if(frManager.findFragmentByTag("first")!=null){ frManager.beginTransaction().hide(frManager.findFragmentByTag("first")).commit(); } if(frManager.findFragmentByTag("second")!=null){ frManager.beginTransaction().hide(frManager.findFragmentByTag("second")).commit(); } if(frManager.findFragmentByTag("fourth")!=null){ frManager.beginTransaction().hide(frManager.findFragmentByTag("fourth")).commit(); } //show if(frManager.findFragmentByTag("third")!=null){ frManager.beginTransaction().show(frManager.findFragmentByTag("third")).commit(); }else{ //add frManager.beginTransaction().add(frameLayout.getId(), ((Fragment) Fragment_zasin.class.newInstance()), tag).commit(); } break; case 3: tag = "fourth"; //hide if(frManager.findFragmentByTag("first")!=null){ frManager.beginTransaction().hide(frManager.findFragmentByTag("first")).commit(); } if(frManager.findFragmentByTag("second")!=null){ frManager.beginTransaction().hide(frManager.findFragmentByTag("second")).commit(); } if(frManager.findFramentByTag("third")!=null){ frManager.beginTransaction().hide(frManager.findFragmentByTag("third")).commit(); } //show if(frManager.findFragmentByTag("fourth")!=null){ frManager.beginTransaction().show(frManager.findFragmentByTag("fourth")).commit(); }else{ //add frManager.beginTransaction().add(R.id.frame_layout, ((Fragment) Fragment_zasin.class.newInstance()), tag).commit(); } break; } //frManager.beginTransaction().replace(R.id.frame_container, fr, tag).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN).commit(); } private void setTabLayout(){ tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { try { setCurrentTabFragment(tab.getPosition()); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } } @Override public void onTabUnselected(TabLayout.Tab tab) { } @Override public void onTabReselected(TabLayout.Tab tab) { } }); } private void enablePopUpView() { // Creating a pop window for emoticons keyboard popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.MATCH_PARENT, (int) keyboardHeight, false); popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { placesCoverLayout.setVisibility(LinearLayout.GONE); } }); } int previousHeightDiffrence = 0; private void checkKeyboardHeight(final View parentLayout) { parentLayout.getViewTreeObserver().addOnGlobalLayoutListener( new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { Rect r = new Rect(); parentLayout.getWindowVisibleDisplayFrame(r); int screenHeight = parentLayout.getRootView() .getHeight(); minusVal=screenHeight-r.bottom; int heightDifference = screenHeight - (r.bottom+(minusVal)); if (previousHeightDiffrence - heightDifference > 50) { popupWindow.dismiss(); } previousHeightDiffrence = heightDifference; if (heightDifference > 100) { isKeyBoardVisible = true; changeKeyboardHeight(heightDifference); } else { isKeyBoardVisible = false; } } }); } private void changeKeyboardHeight(int height) { if (height > 100) { keyboardHeight = height; LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, keyboardHeight); placesCoverLayout.setLayoutParams(params); } } private void enableFooterView() { edtSearchPlaces.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (popupWindow.isShowing()) { popupWindow.dismiss(); } } }); btnSearchPlaces.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { hideSoftKeyboard(WriteRouteActivity.this); if(!popupWindow.isShowing()){ popupWindow.setHeight((int) (keyboardHeight)); if (isKeyBoardVisible) { placesCoverLayout.setVisibility(LinearLayout.GONE); } else { placesCoverLayout.setVisibility(LinearLayout.VISIBLE); } popupWindow.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED); popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); popupWindow.showAtLocation(parentLayout, Gravity.BOTTOM, 0, 0); try { setCurrentTabFragment(0); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } } else { //popupWindow.dismiss(); } } }); } @Override protected void onDestroy() { popupWindow.dismiss(); super.onDestroy(); } public static void hideSoftKeyboard(Activity activity) { InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService( Activity.INPUT_METHOD_SERVICE); inputMethodManager.hideSoftInputFromWindow( activity.getCurrentFocus().getWindowToken(), 0); } @Override public void onBackPressed() { if(popupWindow.isShowing()){ popupWindow.dismiss(); }else { super.onBackPressed(); } } }
activity_write_wroute.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout_parent" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <include layout="@layout/nav_toolbar" /> <fragment android:id="@+id/google_map" class="com.google.android.gms.maps.MapFragment" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <include android:id="@+id/footer_layout" layout="@layout/footer_edittext" android:layout_width="match_parent" android:layout_height="wrap_content" /> <LinearLayout android:id="@+id/footer_for_places" android:layout_width="match_parent" android:layout_height="@dimen/keyboard_height" android:background="@android:color/transparent" android:orientation="vertical" android:visibility="gone" /> </LinearLayout>
Fragment_Zasin
public class Fragment_zasin extends Fragment { public Fragment_zasin newInstance() { Fragment_zasin fr = new Fragment_zasin(); return fr; } public Fragment_zasin() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_zasin, container, false); return rootView; } }
places_popup.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/linear_layout_top" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#fff" android:orientation="vertical"> <android.support.design.widget.TabLayout android:id="@+id/tab_layout" android:layout_width="fill_parent" android:layout_height="?attr/actionBarSize" android:background="#ffffff" app:tabGravity="fill" app:tabIndicatorColor="@color/colorPrimary" app:tabIndicatorHeight="4dp" app:tabMode="fixed" app:tabSelectedTextColor="@color/colorPrimary"> <android.support.design.widget.TabItem android:layout_width="wrap_content" android:layout_height="wrap_content" android:icon="@drawable/tab_pin_selector" android:text="11"> </android.support.design.widget.TabItem> <android.support.design.widget.TabItem android:layout_width="wrap_content" android:layout_height="wrap_content" android:icon="@drawable/tab_mainroute_selector" android:text="22" /> <android.support.design.widget.TabItem android:layout_width="wrap_content" android:layout_height="wrap_content" android:icon="@drawable/tab_talk_selector" android:text="33" /> <android.support.design.widget.TabItem android:layout_width="wrap_content" android:layout_height="wrap_content" android:icon="@drawable/tab_my_selector" android:text="44" /> </android.support.design.widget.TabLayout> <FrameLayout android:id="@+id/frame_layout" android:layout_width="match_parent" android:layout_height="0px" android:layout_weight="1" /> </LinearLayout>
LOGCAT Message
FATAL EXCEPTION: main Process: suacuration.itgotravel, PID: 20131 java.lang.IllegalArgumentException: No view found for id 0x7f100173 for fragment Fragment_zasin{d4ac39c #0 id=0x7f100173 first}
Can somebody help this?