Android“没有找到ID为片段的视图”

问题描述:

好吧。我卡住了,头痛......我不知道如何访问其他布局的视图,因为充气不起作用。 这是我的代码。Android“没有找到ID为片段的视图”

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的消息

致命异常:主要过程:suacuration.itgotravel,PID:20131 java.lang.IllegalArgumentException异常:未发现ID 0x7f100173用于视图片段Fragment_zasin {d4ac39c#0 id = 0x7f100173第一个}

有人可以帮助吗?

+0

请发布您的XML布局,fragment_zasin – curiousMind

+0

@curiousMind um里面没有什么fragment_zasin.xml。只是一个带有android:text =“hello”的textview –

好吧,我找到了答案。 问题是片段不能是'对话框'的孩子。 由于我使用弹出对话框,它无法将片段放入对话框中。

我通过膨胀视图解决,而不是在tablayout中使用片段。

可能有其他错误,但是这是什么?

tb = (Toolbar) findViewById(R.id.nav_toolbar); 

这是不是一个工具栏:

<include layout="@layout/nav_toolbar" /> 

我们可以看到这款nav_toolbar布局里面?

另外,这里是:“R.layout.fragment_zasin”。它抱怨一个片段,所以这是值得的,看看这是否可行。

无论如何,作为一般的故障诊断策略,请尝试在initView()方法中注释掉所有这些行,并将它们一次添加到一个,直到失败。或者,如果你进一步向下滚动错误日志,它会给你一个导致它失败的行的超链接。

为什么在一行上有“frameLayout.getId()”,而在另一行上是“R.id.frame_layout”?尝试使用后者的所有行,看看是否是这样。

+0

他正在使用include标签包含工具栏。 – Raghunandan

+0

是的,没错。忽略那个工具栏。我刚从Android Studio复制我的代码。该布局效果不错... –

+0

不幸的是,该超链接没有出现。当我尝试这个策略时,我发现当这个“frManager.beginTransaction().add(frameLayout.getId(),((Fragment)Fragment_zasin.class.newInstance()),tag).commit(); “需要运行并且编译器(?)无法找到“framelayout.getid()”视图“R.id.frame_layout” –

发生此错误是因为片段管理器无法找到其必须对片段进行膨胀的视图。

片段事务链接到活动,所以出现此错误,因为框架布局不是该主要活动的xml.so的一部分,所以它无法找到添加fragment.fragment的位置必须添加到活动内部。

你必须做的是在你的主要活动中提供一个视图的ID。 为e.g您的主视图

<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> 

,你试图膨胀fragemnt必须是内部活动的看法。 现在,当您尝试

frManager.beginTransaction().add(R.id.mainView, ((Fragment) Fragment_zasin.class.newInstance()), tag).commit(); 

片段将在此查看您的活动的加载。

+0

我已经试过了,发生了同样的错误,logcat的错误信息也是一样的。 –

+0

当我通过Logcat检查时,这两个ID是相同的,他们都不是NULL。然而,由于frame_layout来自places_popup.xml,我想应该有一个方法来从该活动访问“frame_layout”。但我不知道该怎么做.. –

+0

它会。你必须开始交易的主布局内的布局。 –

问题是你试图表现出对未在布局定义的View,你已经在你的onCreate()setContentView(layoutId)

在你的情况你是充气片段WriteRouteActivity定义的Fragment其中定义布局activity_write_route并在FrameLayout上添加片段,这在places_popup.xml中定义,因此在Activity的视图布局中定义您的framelayout

小说明:

--------code------ 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
-------layout define for activity------- 
setContentView(R.layout.activity_write_route); 
} 

FrameLayout(view)

frManager.beginTransaction().add(R.id.frame_layout, ((Fragment) Fragment_zasin.class.newInstance()), tag).commit(); 

这里R.id.frame_layout现在你在哪里添加片段应该在你的布局activity_write_route来定义。

+0

你能解释更多吗?我没有得到这部分“framelayout在活动内容视图” –

+0

没有任何替代?为什么从视图膨胀框架布局不起作用?非常好奇 –

你得到这个错误,因为你的观点找到了ID 0x7f100173为碎片Fragment_zasin所以唯一的解决办法是:

1)检查存在于该特定布局中的所有ID。

2)如果所有的id都存在于特定的布局中,但仍面临同样的问题做一件事改变id的名称不要重构更改名称并在片段或活动中访问此更改的id名称。

我正在扩展错误类型的活动,扩展了AppCompact解决的问题。