2014-01-09 91 views
1

我有代碼來加載類擴展活動微調。它是成功的工作。這是代碼...錯誤在片段加載微調

package nielpoenya.blogspot.com; 

import.... 

public class MySQLite extends Activity implements OnItemSelectedListener { 
    Spinner spinner; 

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

     spinner = (Spinner) findViewById(R.id.spinner); 
     spinner.setOnItemSelectedListener(this); 

     loadSpinnerData(); 

    } 

    /** 
    * Function to load the spinner data from SQLite database 
    * */ 
    private void loadSpinnerData() { 
     // database handler 
     DatabaseHandler db = new DatabaseHandler(getApplicationContext()); 

     // Spinner Drop down elements 
     List<String> lables = db.getAllLabels(); 

     // Creating adapter for spinner 
     ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, 
       android.R.layout.simple_spinner_item, lables); 

     // Drop down layout style - list view with radio button 
     dataAdapter 
       .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

     // attaching data adapter to spinner 
     spinner.setAdapter(dataAdapter); 
    } 

    @Override 
    public void onItemSelected..... 

現在我想在課堂上嘗試擴展片段,但它讓它停止工作。什麼是缺乏與我的片段類

package com.tugasbesar.medantrain; 

import.... 

public class InfoJadwal extends Fragment implements OnItemSelectedListener { 

    Spinner spinner; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     View rootView = inflater.inflate(R.layout.info_jadwal, container, false); 

     spinner = (Spinner) getActivity().findViewById(R.id.spin_tujuan); 
     spinner.setOnItemSelectedListener(this); 

     loadSpinnerData(); 

     return rootView; 
    } 

    private void loadSpinnerData() { 
     // database handler 
     DatabaseHandler db = new DatabaseHandler(this.getActivity()); 

     // Spinner Drop down elements 
     List<String> lables = db.getAllLabels(); 

     // Creating adapter for spinner 
     ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(
       this.getActivity(), android.R.layout.simple_spinner_item, 
       lables); 

     // Drop down layout style - list view with radio button 
     dataAdapter 
       .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

     // attaching data adapter to spinner 
     spinner.setAdapter(dataAdapter); 
    } 

    @Override 
    public void onItemSelected.... 

?由於

回答

2

的錯誤是在你的onCreateView,則需要更換:

spinner = (Spinner) getActivity().findViewById(R.id.spin_tujuan); 

由:

spinner = (Spinner) rootView.findViewById(R.id.spin_tujuan); 

因爲getActivity().findViewById()它會嘗試搜索查看您Activity的內容查看,不在您的Fragment中。