我疯了,我得到这个错误:在这行listView.setAdapter(dataClassificationAdapter); :
10-07 08:03:24.592: E/AndroidRuntime(2396): FATAL EXCEPTION: main
10-07 08:03:24.592: E/AndroidRuntime(2396): Process: com.tfg.webquest, PID: 2396
10-07 08:03:24.592: E/AndroidRuntime(2396): java.lang.NullPointerException
10-07 08:03:24.592: E/AndroidRuntime(2396): at com.tfg.webquest.HomeActivity$ClassificationTab.onCreateView(HomeActivity.java:698)
以及更多......
我有这个:
public class HomeActivity extends ActionBarActivity implements ActionBar.TabListener, OnItemSelectedListener {
public static class ClassificationTab extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home_classification, container,false);
//Get the subject spinner
spinnerSubjectClassification = (Spinner) rootView.findViewById(R.id.home_classification_spinner);
//subjectSpinnerArray will show "(Choose a subject)"+all the subjects
String[] subjectSpinnerArray = new String[ss.length+1];
//Insert in the subjectSpinnerArray "(Choose a subject)"
subjectSpinnerArray[0]=getString(R.string.home_select_subject_string);
//Insert in the subjectSpinnerArray all the subjects
for(int i=0;i<ss.length;i++){
subjectSpinnerArray[i+1]=ss[i];
}
//Insert the information of the array on a list so as to introduce it in the adapter
List<String> list = new ArrayList<String>();
for(int i=0;i<subjectSpinnerArray.length;i++){
list.add(subjectSpinnerArray[i]);
}
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item,list);
//Dropdown the adapter
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Show the adapter
spinnerSubjectClassification.setAdapter(dataAdapter2);
//It activates the retrocalled method, called when we choose a subject
spinnerSubjectClassification.setOnItemSelectedListener(mySubjectClassificationListener);
}
private OnItemSelectedListener mySubjectClassificationListener = new Spinner.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
//Fill the subjectList
for(int i=0;i<classification[pos-1].length;i++){
ClassificationModel classificationModel = new ClassificationModel(classification[pos-1][i]);
classificationList.add(classificationModel);
}
//create an ArrayAdaptar from the String Array
dataClassificationAdapter = new MyCustomAdapter(ctx,R.layout.classification,classificationList);
ListView listView = (ListView) getView().findViewById(R.id.classification_list);
ArrayAdapter<String> stringAdapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_2, classification[pos-1]);
ListView listView = (ListView) getView().findViewById(R.id.listview_classification);
// Assign adapter to ListView
listView.setAdapter(dataClassificationAdapter);
}
}
}
我需要在onClickListener中扩展listView,因为listview的内容取决于微调器选择的项目。 谢谢你的时间。
答案 0 :(得分:0)
将getView().findViewById()
替换为getActivity().findViewById()
。
但您应该考虑将ListView创建移动到onCreateView
(然后使用rootView.findViewById()
),而在OnItemSelected中只更改适配器内的数据