2017-03-02 17 views
0

よろしくお願いします。私は立ち往生して頭痛を抱えています...私は、膨張が働かないので、他のレイアウトのビューにアクセスする方法がわかりません。 これは私のコードです。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 first}

誰かがこれを助けることができますか?

+0

fragment_zasin.xml内部は何もありません、あなたのXMLレイアウト、fragment_zasin – curiousMind

+0

@curiousMind UMを投稿してください。 android:text = "hello"のテキストビューだけ –

答えて

1

私は答えを見つけました。 問題はフラグメントが 'ダイアログ'の子になり得ないことでした。 ポップアップダイアログを使用していたので、ダイアログ内にフラグメントを入れることができませんでした。

私はtablayoutでフラグメントを使用するのではなくビューを膨らませることで解決しました。

0

他にもエラーがありますが、これは何ですか?

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

これは、ツールバーではありません。

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

は、我々はこのnav_toolbarレイアウトの内部を見ることができますか?

また、これは "R.layout.fragment_zasin"です。これは断片について不平を言っているので、これがうまくいくかどうかは分かるはずです。

とにかく、一般的なトラブルシューティング戦略として、initView()メソッド内のすべての行をコメントアウトし、失敗するまで一度に1つずつ追加してみてください。または、エラーログをさらにスクロールすると、エラーの原因となっている行へのハイパーリンクが表示されます。

なぜ "frameLayout.getId()"は1行で、 "R.id.frame_layout"は別の行にありますか?後者をすべての行に使用し、それがそれであるかどうかを確認してください。

+0

彼はincludeタグを使ってツールバーを含んでいます。 – Raghunandan

+0

はい、そうです。そのツールバーを無視してください。私はAndroid Studioから自分のコードをコピーしました。そのレイアウトはうまくいきます... –

+0

残念ながら、そのハイパーリンクは表示されません。私はその戦略を試みたときに、この "frManager.beginTransaction()。((Fragment)Fragment_zasin.class.newInstance())、tag).commit()を追加すると、ランタイムエラーが発生することがわかりました。 " 1行が、「R.id.frame_layoutのいずれかで「R.id.frame_layout」 –

1

このエラーは、フラグメントマネージャがフラグメントを拡張する必要があるビューを見つけられなかったために発生します。

フラグメントトランザクションはアクティビティにリンクされているため、フレームレイアウトはメインアクティビティのxml.soの一部ではないため、このエラーが発生するため、フラグメントの追加先を見つけることができません。フラグメントをアクティビティ内に追加する必要があります。

あなたがしなければならないことは、メインアクティビティにビューのIDを指定することです。例えば、あなたのメインビュー

<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を通してチェックしたとき、それらの2つのIDは同じであり、それらはすべてNULLではありません。しかしframe_layoutがplaces_popup.xmlからであるから、私はその活動からアクセスする「frame_layout」のための方法がなければならないと思います。しかし、私はそれを行う方法を知らない。 –

+0

それはあなたはメインレイアウト内のレイアウトのトランザクションを開始する必要があります。 –

0

問題はあなたがレイアウト定義されているWriteRouteActivityで断片を膨張され、あなたの場合はsetContentView(layoutId)

ながら、あなたのonCreate()で定義されています。レイアウトで定義されていないViewFragmentを表示しようとしているあるactivity_write_routeフラグメントはFrameLayoutに追加されます。これはplaces_popup.xmlに定義されていますので、Activityのビューレイアウトにframelayoutを定義してください。

小概要:ここFrameLayout(view)

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

R.id.frame_layout

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

今あなたが追加しているフラグメントは、あなたのレイアウトactivity_write_routeに定義する必要があります。

+0

あなたはもっと説明できますか?私はこの部分を「アクティビティのコンテンツビューでframelayout」にしません –

+0

代替手段はありませんか?ビューからフレームレイアウトを膨張させることができないのはなぜですか?とても好奇心が強い –

0

Fragment_zasinフラグメントに、id 0x7f100173が見つかりビューはそう唯一の解決策があることであるため、このエラーを得た:その特定のレイアウトに存在するすべてのIDを確認し

1)。

2)すべてのidがその特定のレイアウトに存在し、同じ問題に直面していても、IDの名前を変更しないでください。名前を変更したり、この変更されたID名をフラグメントまたはアクティビティにアクセスしたりしないでください。

0

私は間違ったタイプのアクティビティを拡張していましたが、AppCompactが解決した問題を拡張しました。

関連する問題