0

FragmentfindViewByIdを使用するとエラーが発生します。findViewByIdを使用しようとしたときにフラグメントにエラーが発生しました

どうすれば解決できますか?

public class Discover extends Fragment { 

private static final String TAG = "Discover"; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View root = inflater.inflate(R.layout.fragment_discover, container, false); 
    Log.d(TAG, "onCreate: starting."); 
    setupBottomNavigationView(); 
    return root; 
} 

private void setupBottomNavigationView(){ 

    Log.d(TAG, "setupBottomNavigationView: setting up BottomNavigationView"); 
    BottomNavigationViewEx bottomNavigationViewEx = (BottomNavigationViewEx) findViewById(R.id.bottomNavViewBar); 
    BottomNavigationViewHelper.setupBottomNavigationView(bottomNavigationViewEx); 

} 

public void onResume(){ 
    super.onResume(); 

    // Set title bar 
    ((LandingPage) getActivity()) 
      .setActionBarTitle("Discover Photos"); 

    } 

    } 
} 

答えて

0

あなたはfindViewByIdを使用するためのルートビューを使用する必要が

setupBottomNavigationView(View view) { 
    Log.d(TAG, "setupBottomNavigationView: setting up BottomNavigationView"); 
    BottomNavigationViewEx bottomNavigationViewEx = (BottomNavigationViewEx) view.findViewById(R.id.bottomNavViewBar); 
    BottomNavigationViewHelper.setupBottomNavigationView(bottomNavigationViewEx); 

} 

したり、ビューのルートがonCreateView

View root; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     root = inflater.inflate(R.layout.fragment_discover, container, false); 
     Log.d(TAG, "onCreate: starting."); 

     setupBottomNavigationView(root); 

     return root; 

} 
+0

をそれがエラーをもたらす(それがブラケットを強調)on setupBottomNavigationView(); –

+0

これをあなたのsetupBottomNavigationView(root)に追加するだけです。 –

+0

ありがとう、これは –

2

の上に置くあなたのパラメータでビューを追加します。

を想定すると、このビューid.bottomNavViewBarはこのレイアウトに存在しますlayout.fragment_discoverまたは含ま:

public class Discover extends Fragment { 
View root; 

private static final String TAG = "Discover"; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) { 
// Inflate the layout for this fragment 
root = inflater.inflate(R.layout.fragment_discover, container, false); 
Log.d(TAG, "onCreate: starting."); 
setupBottomNavigationView(); 
return root; 
} 

private void setupBottomNavigationView(){ 
Log.d(TAG, "setupBottomNavigationView: setting up BottomNavigationView"); 
BottomNavigationViewEx bottomNavigationViewEx = (BottomNavigationViewEx) root.findViewById(R.id.bottomNavViewBar); 
BottomNavigationViewHelper.setupBottomNavigationView(bottomNavigationViewEx); 

} 

public void onResume(){ 
super.onResume(); 

// Set title bar 
((LandingPage) getActivity()) 
     .setActionBarTitle("Discover Photos"); 

    } 

    } 
} 

は今、あなたはfindViewByIdを使用する必要がある時はいつでも、あなたがこのようなrootビューを使用することができますonCreateView上記のビューのルートで

root.findViewById(R.id.yourview) 
+0

ありがとう。これは役に立ちます –

+0

私は助けてくれると喜んで、あなたの問題を解決すれば答えの一つを受け入れることを忘れないでください。 –

関連する問題