我正在尝试使用ListView和一些按钮创建DialogFragment。我在布局文件中声明了所有这些并且按钮工作正常但是当我使用fineViewByID()时ListView为null。
这是我的xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/category_picker"
android:layout_gravity="center"
android:orientation="vertical" >
<ListView
android:id="@+id/lvCategorys"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="0.5" >
</ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_gravity="bottom"
android:layout_weight="0.5"
android:orientation="horizontal" >
<Button
android:id="@+id/btnAddNew"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="@string/cpf_btnAddNew" />
<Button
android:id="@+id/btnConfig"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="@string/cpf_btnConfig" />
</LinearLayout>
</LinearLayout>
这是FragmentDialog: package com.example.spendo.fragments;
imports....
public class CategoryPickerFragment extends DialogFragment implements OnItemClickListener{
private CategoryGroup categoryGroup;
private ListView lvCategorys;
private ArrayAdapter<String> lvAdapter;
private Button btnAddNew, btnConfig;
public CategoryPickerFragment() {
// Empty constructor required for DialogFragment
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_category_picker, container);
getDialog().setTitle("Hello");
categoryGroup = General.getActiveCategoryGroup(this.getActivity());
System.out.println(categoryGroup);
btnAddNew = (Button) this.getActivity().findViewById(R.id.btnAddNew);
btnConfig = (Button) this.getActivity().findViewById(R.id.btnConfig);
lvCategorys = (ListView) this.getActivity().findViewById(R.id.lvCategorys);
if(lvCategorys == null){ //this returns "lvCategorys is null"
System.out.println("lvCategorys is null");
}else{
System.out.println(lvCategorys + " is not null");
}
lvAdapter = new ArrayAdapter<String>(this.getActivity(),android.R.layout.simple_list_item_1, categoryGroup.getNameArray());
System.out.println(lvAdapter);
ArrayList<String> nameArray = categoryGroup.getNameArray();
lvAdapter.clear();
for(int i = 0; i < nameArray.size(); i++){
lvAdapter.add((String) nameArray.get(i));
}
lvAdapter.notifyDataSetChanged();
lvCategorys.setAdapter(lvAdapter);
return view;
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
}
}
有没有人看到我的问题。 (我对android很新,但我过去做过一些java。)
// SVERKER
答案 0 :(得分:3)
我认为你需要更换
btnAddNew = (Button) this.getActivity().findViewById(R.id.btnAddNew);
btnConfig = (Button) this.getActivity().findViewById(R.id.btnConfig);
lvCategorys = (ListView) this.getActivity().findViewById(R.id.lvCategorys);
与
btnAddNew = (Button) view.findViewById(R.id.btnAddNew);
btnConfig = (Button) view.findViewById(R.id.btnConfig);
lvCategorys = (ListView) view.findViewById(R.id.lvCategorys);