【发布时间】:2014-10-07 15:58:58
【问题描述】:
我快疯了,我收到这个错误:在这一行 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 的内容取决于 spinner selected item 。 感谢您的宝贵时间。
【问题讨论】:
-
HomeActivity 的第 698 行是什么?
-
listView.setAdapter(dataClassificationAdapter);
-
那么你的 listView 为空。
ListView listView = (ListView) getView().findViewById(R.id.classification_list);这条线导致了问题。请注意您是如何在那里创建 2 个列表视图变量的。 -
感谢您纠正该错误。但是我的问题仍然存在:我在删除一个listView的同一行中有同样的错误,使用:ListView listView = (ListView) getView().findViewById(R.id.listview_classification);
-
确保在更改某些内容后继续编辑您的答案。你说你正在使用“rootView”。在该方法上 rootView 不存在。另外你确定
listview_classification存在吗?
标签: android listview fragment android-listfragment onitemclicklistener