我试图在片段布局中显示列表视图;因为我使用了自定义适配器。请注意,在按钮单击时,片段已更改。但是在那个按钮上单击应用程序崩溃,我不知道实际问题是什么,请帮忙。
这是我的片段类:
public class Fragment1 extends Fragment {
public void Fragment1()
{
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_fragment1, container, false);
//return inflater.inflate(R.layout.fragment_fragment1, container, false);
ListView listView = (ListView)rootView.findViewById(R.id.listView);
CustomAdapter customAdapter = new CustomAdapter();
listView.setAdapter(customAdapter);
return rootView;
}
class CustomAdapter extends BaseAdapter
{
int img_arr[] = {R.drawable.emmastone,
R.drawable.chloegracemoretz,
R.drawable.salmankhan,
R.drawable.emilia,
R.drawable.kitharington,
R.drawable.scarlettjohansson,
R.drawable.sophie,
R.drawable.deepika,
R.drawable.leonardodicaprio};
String name_arr[] = {"Emma", "Chloe", "Salman", "Emilia", "Kit", "Scarlet", "Sophie", "Deepika", "Leonardo" };
String msg_arr[] = {"Free at 8pm?", "I miss you <3", "Mota ho raha hun kia karon?", "Not in mood", "Danny or yagrit?", "Where are you?", "got engaged <3", "Give my money", "50 is enough" };
@Override
public int getCount() {
return img_arr.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = getLayoutInflater().inflate(R.layout.chats,null);
ImageView img = (ImageView)convertView.findViewById(R.id.imageView);
TextView name = (TextView)convertView.findViewById(R.id.textView_name);
TextView msg = (TextView)convertView.findViewById(R.id.textView_msg);
img.setImageResource(img_arr[position]);
name.setText(name_arr[position]);
msg.setText(msg_arr[position]);
return convertView;
}
}
}
这是在logcat:
0-21 23:52:21.581 3040-3040/com.example.farhan.a13_f_8310_lab_10_oct_fragments E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.farhan.a13_f_8310_lab_10_oct_fragments, PID: 3040
java.lang.NoSuchMethodError: No virtual method getLayoutInflater()Landroid/view/LayoutInflater; in class
Lcom/example/farhan/a13_f_8310_lab_10_oct_fragments/Fragment1; or its super classes (declaration of 'com.example.farhan.a13_f_8310_lab_10_oct_fragments.Fragment1' appears in /data/app/com.example.farhan.a13_f_8310_lab_10_oct_fragments-1/split_lib_slice_1_apk.apk)
at com.example.farhan.a13_f_8310_lab_10_oct_fragments.Fragment1$CustomAdapter.getView(Fragment1.java:91)