2009-12-30 172 views
0

這是我的代碼工作,試圖添加別的東西,然後發現我以前的代碼已停止工作的情況之一。程序啓動時崩潰。這個錯誤似乎與ArrayAdapter自定義類的定義有關,就像我嘗試使用內置適配器一樣,它工作正常。任何幫助?自定義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: [email protected] 
12-29 23:43:57.517: DEBUG/dalvikvm(719): GC freed 726 objects/64112 bytes in 83ms 
+1

您可以詳細說明什麼現在不起作用,或者您使用上述代碼看到了什麼? – 2009-12-30 04:02:03

+0

啊,對不起。它只是在啓動時關閉。 – GuyNoir 2009-12-30 04:40:43

+0

考慮添加註釋和JavaDoc,特別是如果您發佈代碼以供其他人閱讀。其次,編寫單元測試。這將幫助您檢測行爲變化。 – 2009-12-30 09:39:22

回答

0

唉唉......我是這樣一個白癡。大聲笑。我沒有真正將'row'設置爲inf.inflate()的值。

Facepalm是的。忽視這一點。希望別人能夠找到有用的代碼。

0

你是對的(row = inf.inflate ...),你應該有一個NullPointerException。 另外,你必須刪除第二個電話inf.inflate(R.layout.post_obj, parent, false);。 否則,您將失去由convertView授予的優化...

相關問題