2011-06-22 89 views
0

你好夥計我試圖從我的主要活動切換到新的活動,但我得到'the application has stopped unexpectedly. Please try again'。我不知道爲什麼。新的android活動不開始

這裏是我目前的活動:

public class EditActivity extends ListActivity { 
    TextView selection; 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListContent)); 
    } 
    private static String[] mListContent={"Fishing Reports", "Choose a Fishing Spot", "Prediction","Item 1", "Item 2", "Item 3","Item 1", "Item 2", "Item 3","Item 1", "Item 2", "Item 3"}; 

    @Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 

      switch (position){ 
       case 0 :  Toast.makeText(this, "You pressed item 1 !", Toast.LENGTH_LONG).show(); 
          break; 
       case 1 :  
           Intent myIntent = new Intent(EditActivity.this, LocationActivity.class); 
           startActivity(myIntent); 
          break; 
    } 

     super.onListItemClick(l, v, position, id); 
    } 

這裏是新的活動是在我的清單

<activity android:name=".LocationActivity"></activity> 

這裏是佈局聲明的活動我的呼喚

public class LocationActivity extends ListActivity { 
    //Your member variable declaration here 

    // Called when the activity is first created. 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.location); 
    } 
} 

我的新活動:

當前活動工作正常,但是當我在第二個項目點擊mListContent (case 1),沒有啓動的第二個活動,我得到的錯誤'the application has stopped unexpectedly. Please try again'任何想法,爲什麼? 非常感謝!

+0

好吧,謝謝你們, 我發現我的問題。我試圖調用的新活動是擴展ListActivity。所以我改變它,所以它擴大了活動,這似乎已經解決了問題, 再次感謝這個論壇!但你知道這是怎麼回事,在我遇到另一個問題之前不久,它就是生命哈哈 – Joshua

回答

0

當您使用ListActivity時,您需要確保您的佈局具有ID爲「@android:id/list」的ListView,以便ListActivity可以找到它。如果你不這樣做,它會在啓動時強制關閉。

事情是這樣的:

<ListView android:id="@android:id/list" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:fastScrollEnabled="true" /> 
</LinearLayout> 

順便說一句,如果你使用Eclipse,這裏有一個技巧,以幫助調試。

在Eclipse菜單中,選擇窗口>顯示視圖>其他...,展開Android類別,選擇LogCat,然後單擊確定。這將打開LogCat視圖,該視圖捕獲仿真器或設備的所有日誌輸出。

我喜歡在LogCat中設置過濾器,因此更容易找到應用程序消息;點擊小綠加號以創建一個新過濾器,併爲「過濾器名稱」和「按日誌標記」字段輸入「AndroidRuntime」。此過濾器將顯示爲LogCat視圖中的另一個選項卡,您可以隨時通過切換到該選項卡來過濾內容。

+0

我也遇到了同樣的問題。這支部隊接近來臨的原因是什麼?但我只使用活動而不是列出活動。我仍然接近力量。日誌貓根本沒有幫助。 – Ashwin