English 中文(简体)
我的 ListView 的适应器给了我这个错误: 不支持的操作例外 : 添加 View( View, 版式Params) 在适应器View 中不支持 。
原标题:My Adapter for my ListView is giving me this error: UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView

我试图弄清楚原因,但我不确定。我看了一看,我肯定我输入了所有我应该输入的内容。我会在下面张贴我的代码。

适应器类 :

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);
    }

}

假设我的布局和身份证是正确的 有人知道问题是什么吗?

编辑( 日志上的精确错误) :

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
最佳回答

<强度 > EDIT:

所以在对话编辑 My AgendaAdapter 类之后, 像这样编辑它 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;
     }

}

}
问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签