2012-01-25 78 views
1

我已經關注this tutorial,但是當我嘗試運行應用程序時,我得到Unfortunately HelloListView has stopped IDE沒有提供警告或錯誤。不幸的是,HelloListView已停止

我HelloViewListActivity.java看起來是這樣的:

public class HelloListViewActivity extends ListActivity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     String[] countries = getResources().getStringArray(R.array.countries_array); 
     setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, countries)); 

     ListView lv = getListView(); 
     lv.setTextFilterEnabled(true); 

     lv.setOnItemClickListener(new OnItemClickListener() { 
      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 
       // When clicked, show a toast with the TextView text 
       Toast.makeText(getApplicationContext(), 
         ((TextView) view).getText(), Toast.LENGTH_SHORT).show(); 
      } 
     }); 
    } 
} 

我的strings.xml和list_item.xml都是identical to the tutorial.

Logcat log here 它是什麼,我做錯了什麼?

+2

看來你的list_content_simple.xml不正確。再次驗證一次。 – kosa

+0

@thinksteep list_content_simple.xml沒有在教程中提到。我確實找到了它的SDK文件夾,並將其添加到我的res /佈局,但仍然是相同的錯誤。 – darren

+0

你所指的教程有點零碎。現在查看我的答案。我已經包含了工作示例教程的鏈接,如果您不需要,可以忽略數據庫部分。 – kosa

回答

2

你有super.onCreate(savedInstanceState);

R.layout.yourlayout後添加setContentView(R.layout.yourlayout)應該像這樣(在文件夾RES /佈局):

<?xml version="1.0" encoding="utf-8"?> 
<ListView 
    android:id="@android:id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" /> 
+0

謝謝。在LinearLayout中添加ListView可以解決我的問題。奇怪的是,在兩篇教程中,我沒有提到這一步。 – darren

1

當你繼承ListActivity,你需要有一個包含一個xml ID爲@android:id/list,那麼在你的活動中你需要setContentView(R.layout.yourxmlName);這裏是tutorial

+0

當我有一個ListActivity,我必須總是有'@android:id/list'的東西?我可以不更改ID爲'@android:id/list2'之類的其他任何內容嗎? – darren

+0

不,如果你想使用自定義列表(或自定義id),你必須使用一個'Activity'而不是'ListActivity',然後用'findViewById(R.id.thelistid)' – louiscoquio