2011-02-16 139 views
4

基本免責聲明;我一般都是新手機應用程序編程,特別是android。Android startActivity()崩潰

我有一個按鈕,點擊後,應打開下一個活動:

bCustom.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View view) { 
     Intent i = new Intent(ctx, DiceCustomList.class); 
     startActivity(i); 
    } 
}); 

(其中「私有上下文CTX =本;」因爲把「本」,其中「CTX」是沒拿到上下文時onClick)

該程序在當前活動被遮蓋之前崩潰(儘管我不確定轉換是如何影響的)。註釋掉幾乎一切後,這裏是它調用了活動:

public class DiceCustomList extends ListActivity { 
    @Override 
    public void onCreate(Bundle savedInstanceState){ 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.custom_list); 
    } 
} 

而且custom_list.xml:我認爲問題出在按鈕代碼

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TextView android:text="@string/rollText" 
     android:id="@+id/textRoll2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textSize="11pt" 
     android:gravity="center"/> 
    <ListView android:layout_height="wrap_content" 
     android:id="@+id/listView1" 
     android:layout_width="fill_parent"/> 
</LinearLayout> 

,但我似乎無法得到接近回答比這個更重要。

編輯:Android清單文件中確實有:

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

編輯2:嗯,後,終於找到其中Eclipse中隱藏的堆棧跟蹤,它告訴我:「你的內容必須有一個ListView,其id屬性是'android.R.id.list'「,它們實際上是指」@ + id/android:list「。那很有趣。 (Edit3:我的意思是,這是答案,謝謝提示。)

+1

在Eclipse中使用`adb logcat`,DDMS或DDMS透視圖來檢查LogCat並查看與您的錯誤相關的堆棧跟蹤。 – CommonsWare 2011-02-16 00:47:07

+0

Logcat是您調試Android應用程序的最佳朋友。 adb位於SDK的'平臺工具'目錄中。作爲`adb logcat`從控制檯運行它。您可以從Eclipse內部查看Logcat消息,但是我發現它使事情變得非常混亂,並且更容易分開運行。 – Farray 2011-02-16 01:02:53

回答

9

檢查以確保DiceCustomList活動在清單XML文件中有條目。

+0

它的確如此。這夠了嗎? AlbeyAmakiir 2011-02-16 00:52:19

1

(其中 「私有上下文CTX =本;」 因爲把 「本」,其中 「CTX」 是 沒有得到裏面的時候 的onClick上下文)

您可以使用YourActivity.this獲取上下文。簡單的'this'不起作用,因爲在這個方法裏它指向了OnClickListener對象。

你能給我們一個LogCat錯誤日誌嗎?

0

問題是您的DiceCustomList類正在擴展ListActivity但佈局文件缺少一些必需的部分。

ListActivity的佈局文件必須包含以下2個具有特定ID的元素。 ListActivity類使用這些元素將數據適配器綁定到活動中的列表(使用setListAdapter(myAdapter););

<ListView android:id="@+id/android:list" 
      {other attributes ommitted} /> 
<TextView android:id="@+id/android:empty" 
      {other attributes ommitted} /> 

Android的文檔具有很好的借鑑XML和例子結合光標:雖然http://developer.android.com/reference/android/app/ListActivity.html

在一般情況下,你應該熟悉adblogcat。由於模擬器調試應用程序的方式,很多時候您的應用程序會崩潰,您只會在Eclipse中看到「未找到源」消息。在try/catch塊中封裝問題代碼並記錄異常(Log.e(TAG, MESSAGE);)是一種快速查看實際情況的簡單方法。