我想找出原因,但我不确定。我看了看,我很肯定我输入的东西都是我想的。我将在下面发布我的代码。
适配器类:
public class MyAgendaAdapter extends BaseAdapter {
ArrayList<Agenda> agendas = new ArrayList<Agenda>();
public MyAgendaAdapter() {
agendas.add(new Agenda());
}
@Override
public int getCount() {
return agendas.size();
}
@Override
public Object getItem(int position) {
return agendas.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
convertView = inflater.inflate(R.layout.agenda, parent, false);
}
Agenda agenda = agendas.get(position); //get the agenda from the ArrayList
TextView nameTextView = (TextView) convertView.findViewById(R.id.agenda_name);
nameTextView.setText(agenda.getEvent());
TextView timeTextView = (TextView) convertView.findViewById(R.id.agenda_time);
timeTextView.setText(agenda.getTime());
TextView locationTextView = (TextView) convertView.findViewById(R.id.agenda_location);
locationTextView.setText(agenda.getLocation());
return convertView;
}
}
主要活动类:
public class MainDisplayActivity extends Activity {
MyAgendaAdapter agendaAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView list = (ListView) findViewById(R.id.agenda_list);
agendaAdapter = new MyAgendaAdapter();
list.setAdapter(agendaAdapter);
}
}
我省略了导入。假设我的布局和ID是正确的,有谁知道问题是什么?
编辑(logcat上的确切错误):
05-27 17:29:54.839: W/dalvikvm(1065): threadid=1: thread exiting with uncaught exception(group=0x409c01f8)
05-27 17:29:54.860: E/AndroidRuntime(1065): FATAL EXCEPTION: main
05-27 17:29:54.860: E/AndroidRuntime(1065): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.zeroe/com.zeroe.MainDisplayActivity}: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
05-27 17:29:54.860: E/AndroidRuntime(1065): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
05-27 17:29:54.860: E/AndroidRuntime(1065): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-27 17:29:54.860: E/AndroidRuntime(1065): at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-27 17:29:54.860: E/AndroidRuntime(1065): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-27 17:29:54.860: E/AndroidRuntime(1065): at android.os.Handler.dispatchMessage(Handler.java:99)
05-27 17:29:54.860: E/AndroidRuntime(1065): at android.os.Looper.loop(Looper.java:137)
05-27 17:29:54.860: E/AndroidRuntime(1065): at android.app.ActivityThread.main(ActivityThread.java:4424)
05-27 17:29:54.860: E/AndroidRuntime(1065): at java.lang.reflect.Method.invokeNative(Native Method)
05-27 17:29:54.860: E/AndroidRuntime(1065): at java.lang.reflect.Method.invoke(Method.java:511)
05-27 17:29:54.860: E/AndroidRuntime(1065): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-27 17:29:54.860: E/AndroidRuntime(1065): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-27 17:29:54.860: E/AndroidRuntime(1065): at dalvik.system.NativeStart.main(Native Method)
05-27 17:29:54.860: E/AndroidRuntime(1065): Caused by: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
05-27 17:29:54.860: E/AndroidRuntime(1065): at android.widget.AdapterView.addView(AdapterView.java:471)
05-27 17:29:54.860: E/AndroidRuntime(1065): at android.view.LayoutInflater.rInflate(LayoutInflater.java:743)
05-27 17:29:54.860: E/AndroidRuntime(1065): at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
05-27 17:29:54.860: E/AndroidRuntime(1065): at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
05-27 17:29:54.860: E/AndroidRuntime(1065): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
05-27 17:29:54.860: E/AndroidRuntime(1065): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
05-27 17:29:54.860: E/AndroidRuntime(1065): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
05-27 17:29:54.860: E/AndroidRuntime(1065): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
05-27 17:29:54.860: E/AndroidRuntime(1065): at android.app.Activity.setContentView(Activity.java:1835)
05-27 17:29:54.860: E/AndroidRuntime(1065): at com.zeroe.MainDisplayActivity.onCreate(MainDisplayActivity.java:21)
05-27 17:29:54.860: E/AndroidRuntime(1065): at android.app.Activity.performCreate(Activity.java:4465)
05-27 17:29:54.860: E/AndroidRuntime(1065): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-27 17:29:54.860: E/AndroidRuntime(1065): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
05-27 17:29:54.860: E/AndroidRuntime(1065): ... 11 more
答案 0 :(得分:1)
修改强>
所以在对话后编辑MyAgendaAdapter
课程并按照以下方式编辑conctructor
:
public MyAgendaAdapter(Context c) {
this.context = c;
agendas.add(new Agenda());
}
所以你会:
public class MyAgendaAdapter extends BaseAdapter {
Context context;
ArrayList<Agenda> agendas = new ArrayList<Agenda>();
public MyAgendaAdapter() {
agendas.add(new Agenda());
}
...
然后将其添加到您的getView
方法
LayoutInflater inflater = LayoutInflater.from(context);
if(convertView == null) {
convertView = inflater.inflate(R.layout.agenda, null);
}
然后你会这样打电话:
ListView list = (ListView) findViewById(R.id.agenda_list);
agendaAdapter = new MyAgendaAdapter(this);
list.setAdapter(agendaAdapter);
然后告诉我。
内部类的替代方式:
public class MainDisplayActivity extends Activity {
MyAgendaAdapter agendaAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView list = (ListView) findViewById(R.id.agenda_list);
agendaAdapter = new MyAgendaAdapter();
list.setAdapter(agendaAdapter);
}
private class MyAgendaAdapter extends BaseAdapter {
ArrayList<Agenda> agendas = new ArrayList<Agenda>();
public MyAgendaAdapter() {
agendas.add(new Agenda());
}
@Override
public int getCount() {
return agendas.size();
}
@Override
public Object getItem(int position) {
return agendas.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
if(convertView == null) {
convertView = inflater.inflate(R.layout.agenda, null);
}
Agenda agenda = agendas.get(position); //get the agenda from the ArrayList
TextView nameTextView = (TextView) convertView.findViewById(R.id.agenda_name);
nameTextView.setText(agenda.getEvent());
TextView timeTextView = (TextView) convertView.findViewById(R.id.agenda_time);
timeTextView.setText(agenda.getTime());
TextView locationTextView = (TextView) convertView.findViewById(R.id.agenda_location);
locationTextView.setText(agenda.getLocation());
return convertView;
}
}
}