自定义ArrayAdapter不工作?

时间:2009-12-30 03:27:01

标签: java android

这是我使代码工作,尝试添加其他内容,然后发现我之前的代码已停止工作的情况之一。程序启动时崩溃。错误似乎在于ArrayAdapter自定义类的定义,就像我尝试使用内置适配器一样,它工作正常。有什么帮助吗?

public class MainEntry extends ListActivity{    

private String[] items = {"yay","yay2","asdfa","nlah","lipsum","orum","rawr"};



/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setListAdapter(new IconicAdapter());
}


class IconicAdapter extends ArrayAdapter {
    IconicAdapter(){
        super(MainEntry.this, R.layout.post_obj, items);
    }

    public View getView(int pos, View convertView, ViewGroup parent){
        LayoutInflater inf = getLayoutInflater();

        View row = null;
        if(convertView==null){
            inf.inflate(R.layout.post_obj, parent, false);
        }else{
            row = convertView;
        }
        inf.inflate(R.layout.post_obj, parent, false);

        TextView label = (TextView) row.findViewById(R.id.label);
        label.setText(items[pos]);

        ImageView image = (ImageView) row.findViewById(R.id.icon);
        image.setImageResource(((Math.random()>.5)?R.drawable.wolfire:R.drawable.android)); //temp

        return row;
    }

}

}

如果有帮助,这是LogCat日志:

12-29 23:43:49.968: INFO/ActivityManager(564): Start proc com.test.TestApp for 

activity com.test.TestApp/.MainEntry: pid=783 uid=10023 gids={1015}
12-29 23:43:50.118: INFO/jdwp(783): received file descriptor 10 from ADB
12-29 23:43:50.188: DEBUG/ddm-heap(783): Got feature list request
12-29 23:43:50.318: WARN/ActivityThread(783): Application com.test.TestApp is waiting for the debugger on port 8100...
12-29 23:43:50.348: INFO/System.out(783): Sending WAIT chunk
12-29 23:43:50.348: INFO/dalvikvm(783): Debugger is active
12-29 23:43:50.577: INFO/System.out(783): Debugger has connected

12-29 23:43:50.577: INFO/System.out(783): waiting for debugger to settle...
12-29 23:43:50.638: INFO/ARMAssembler(564): generated scanline__00000077:03515104_00001001_00000000 [ 64 ipp] (84 ins) at [0x2c2900:0x2c2a50] in 1189200 ns
12-29 23:43:50.778: INFO/System.out(783): waiting for debugger to settle...
12-29 23:43:50.987: INFO/System.out(783): waiting for debugger to settle...
12-29 23:43:51.187: INFO/System.out(783): waiting for debugger to settle...
12-29 23:43:51.387: INFO/System.out(783): waiting for debugger to settle...
12-29 23:43:51.597: INFO/System.out(783): waiting for debugger to settle...
12-29 23:43:51.800: INFO/System.out(783): waiting for debugger to settle...
12-29 23:43:52.003: INFO/System.out(783): waiting for debugger to settle...
12-29 23:43:52.206: INFO/System.out(783): waiting for debugger to settle...
12-29 23:43:52.218: INFO/ActivityManager(564): Killing process com.test.TestApp (pid=783) at user's request
12-29 23:43:52.228: INFO/Process(564): Sending signal. PID: 783 SIG: 9
12-29 23:43:52.238: INFO/ActivityManager(564): Process com.test.TestApp (pid 783) has died.
12-29 23:43:52.298: WARN/UsageStats(564): Unexpected resume of com.android.launcher while already resumed in com.test.TestApp
12-29 23:43:52.348: WARN/InputManagerService(564): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@43944340
12-29 23:43:57.517: DEBUG/dalvikvm(719): GC freed 726 objects / 64112 bytes in 83ms

2 个答案:

答案 0 :(得分:0)

啊......我是个白痴。大声笑。我实际上没有将'row'设置为inf.inflate()的值。

Facepalm 是的。无视这一点。希望其他人会发现代码很有用。

答案 1 :(得分:0)

你是对的(row = inf.inflate ...),你应该有NullPointerException。 此外,您必须删除第二个电话inf.inflate(R.layout.post_obj, parent, false);。 否则,您将失去convertView ...

授予的优化