2

嗨,我是Android的業餘愛好者,在使用片段類中的活動方法時遇到了很大的問題。在片段中使用活動方法

我想實施setContentView,findViewByIdgetMenuInflater。不幸的是,我不知道該怎麼做。

public class WagaFragment extends Fragment{ 

    private Spinner fromSpinner, toSpinner; 
    private EditText fromEditText, toEditText; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     **setContentView**(R.layout.activity_main); 


     ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.units, android.R.layout.simple_spinner_item); 
     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

     fromSpinner = (Spinner) **findViewById**(R.id.spinner_from); 
     toSpinner = (Spinner) **findViewById**(R.id.spinner_to); 

     fromSpinner.setAdapter(adapter); 
     toSpinner.setAdapter(adapter); 

     fromEditText = (EditText) **findViewById**(R.id.editText_from); 
     toEditText = (EditText) **findViewById**(R.id.editText_to); 


     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     toolbar.setTitle(R.string.app_name); 
     toolbar.setTitleTextColor(Color.YELLOW); 


    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     **getMenuInflater()**.inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    public void konwertuj(View view) { 
     // Get the string from the Spinners and number from the EditText 
     String fromString = (String) fromSpinner.getSelectedItem(); 
     String toString = (String) toSpinner.getSelectedItem(); 
     double input = Double.valueOf(fromEditText.getText().toString()); 

     // Convert the strings to something in our Unit enu, 
     Konwerter.Jednostka fromJednostka = Konwerter.Jednostka.fromString(fromString); 
     Konwerter.Jednostka toJednostka = Konwerter.Jednostka.fromString(toString); 

     // Create a converter object and convert! 
     Konwerter konwerter = new Konwerter(fromJednostka, toJednostka); 
     double result = konwerter.convert(input); 
     toEditText.setText(String.valueOf(result)); 
    } 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.tab_fragment_1, container, false); 

    } 

} 
+1

你必須回去片段的基礎。這不是你如何創建和膨脹一個片段,並訪問你可以使用的一些Activity方法getActivity() – mobilepotato7

+0

你在設計時不應該在片段中使用activity的方法。你應該把你的片段設計成一個獨立的盒子,它有一個標準的和預定義的界面以及其父活動。片段不應該打破它的邊界,例如片段不應該使用片段的'setContentView'。片段只能對其自己的視圖負責,而不能對其父視圖的視圖負責。 –

回答

0

在碎片 「內容查看」(在你的meanign)在public View onCreateView過程中定義。

根據您的代碼: 你必須使用

View view = inflater.inflate(R.layout.tab_fragment_1, container, false); 
<your control> = <Control type> view.findViewById(<control id>) 

對於MenuInflater試試這個:

MenuInflater inflater = getActivity().getMenuInflater(); 
0

嘗試onCreateView:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_name, container, false); 
    fromSpinner = (Spinner) view.findViewById(R.id.spinner_from); 
    return view; 
    }