2016-08-01 102 views
-3

我有這樣一段代碼在一個片段,但它說不能解析方法findViewByIdfindViewById沒有解決

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
    View fragmentLayout = inflater.inflate(R.layout.fragment_note_detail, container, false); 

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 

     } 
    }); 
} 
+2

你需要給根視圖:fragmentLayout .findViewById(..); – Chol

+1

使用fragmentLayout.findViewById(); –

回答

4

它應該是:

FloatingActionButton fab = (FloatingActionButton)fragmentLayout.findViewById(R.id.fab);