2012-04-28 124 views
0

我試圖誇大捕獲異常膨脹的佈局

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
<Item android:id="@+id/ltArr" 
    android:layout_alignParentLeft="true" 
    android:icon="@android:drawable/arrow_up_float"/> 
<TextView android:id="@+id/ImText" 
    android:layout_toRightOf="@id/ltArr"/> 
<Item android:layout_alignParentRight="true" 
    android:layout_toRightOf="@id/ImText" 
    android:icon="@android:drawable/arrow_down_float"/> 
</RelativeLayout> 

在我打電話addTextView一個過程,它是在一個try catch塊

try{ 
    addTextView("Showtext", intID, addtothisLayout); 
    }catch (Exception ex){} 

當addText充氣上述佈局程序中有一個異常被捕獲,執行被回送到調用程序中的捕獲

void addTextView(String showTxt,integer id, ViewGroup addtoparent) 
{ 
View itemview = View.inflate(this, R.layout.itemlayout, addtoparent); //this causes the exception 
.. 
.. 
} 

爲什麼當拋出異常時,我不能在ex變量中看到任何東西?

我在想什麼傻事?

如何查看異常情況?我試圖檢查異常ex變量作爲調試器通過它,但它沒有任何東西。如果我在過程中使用SQL。我特意拋出SQLException,這將允許我看到錯誤描述,但在這裏我不知道要拋出什麼......?

我發現通過刪除try catch子句引用了LogCat中不存在的資源異常。但是,如何在try ex中捕獲這些描述?我必須拋出特定的例外嗎?

好的,這是我的答案,我試圖檢查變量的聲明,並需要在代碼塊中放置一些東西。 「ex.toString()」

catch (Exception ex){ 
      ex.toString(); 
     } 

感謝所有非常迅速的答覆。

+1

相對佈局缺少關閉'>' – Chopin 2012-04-28 21:57:27

+1

「拋出異常」:有[Logcat](http://www.droidnova.com/debugging-in-android-using-eclipse,541.html)和如果您將錯誤追加到您的問題中,應該可以找到答案。 – zapl 2012-04-28 21:58:06

+0

錯字,生病修復 – Kickaha 2012-04-28 21:59:09

回答

1

當我運行它的AVD 2.3 .3模擬器我得到:

04-28 22:03:36.537: E/AndroidRuntime(395): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.tests/com.android.tests.TestsActivity}: android.view.InflateException: Binary XML file line #5: Error inflating class Item 

也就是說,「項目」不被視爲視圖。

+0

另請注意,TextView缺少'android:layout_width'和'android:layout_height',兩者都是必需的屬性。 – Chopin 2012-04-28 22:12:57

+0

感謝您的幫助 – Kickaha 2012-04-28 22:48:28

+0

不客氣! – Chopin 2012-04-28 23:00:58

0

如果你想擡高整個佈局,而不僅僅是在它的視圖,您必須使用:

getLayoutInflater().inflate(... 

getLayoutInflater()是一種活動的方法

+0

catch(Exception ex){ \t ex.toString(); \t \t} – Kickaha 2012-04-28 22:45:01