2015-02-11 49 views
0

在我的Android項目中,我只是試圖添加一個正常的Button和一個ToggleButton,以便它們可以水平並排。因此,我確保了水平方向並使用了權重和layout_weight。但我得到的圖形佈局異常:在Android項目中添加ToggleButton導致異常

異常渲染期間提出:-1

異常詳細信息記錄在窗口>顯示視圖>錯誤日誌 和圖形佈局只有按鈕就可以可見。 任何幫助將不勝感激。

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:weightSum="100" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal" 
> 
<Button 
    android:layout_weight="20" 
    android:id="@+id/button1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Button" /> 
<ToggleButton 
    android:layout_weight="80" 
    android:id="@+id/toggleButton1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="ToggleButton" /></LinearLayout> 
+0

發佈您的logcat異常。 – 2015-02-11 18:21:42

回答

1

既然你有這樣的例外:

異常渲染期間提出:-1

轉到您的圖形佈局和定位降API水平的下降,很可能你是使用最後一個21,並且您沒有安裝它,根據您的應用更改爲其他控制桿。

enter image description here

+0

這實際上有幫助。謝謝很多。但有一件事我不明白,但爲什麼代碼片段運行在api 2.2中,但不是在5.0.1中? – user3430220 2015-02-11 21:47:23

+0

您的應用程序將在所有操作系統版本上運行,具體取決於您在AndroidManifest.xml中配置了哪些內容,尤其是由於您沒有Android SDK 21而導致此問題,請轉到Windows - > Android SDK Manager並安裝SDK :) – Jorgesys 2015-02-11 22:01:19

+0

我不認爲這是問題,因爲我已經安裝了5.0.1,4.4w和2.2.Now我看我是否設置5.0.1,它會導致像我提到的異常,但如果我設置2.2,它說。 無法解析文件G:\ android \ appcompat_v7 \ res \ drawable \ abc_edit_text_material.xml – user3430220 2015-02-12 09:53:26

0

我想你的佈局,我沒有得到任何的異常。試圖改善它,最後用這個來了:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal"> 

<Button 
    android:id="@+id/button1" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="8" 
    android:text="Button" /> 

<ToggleButton 
    android:id="@+id/toggleButton1" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="2" 
    android:text="ToggleButton" /> 

你能嘗試一下,檢查它是否仍然崩潰?

+0

我嘗試了你的方式,它的工作。其實我從來沒有注意到,在我的圖形佈局API是21,但我的AVD是2.2。2.2,你的代碼完美運行。謝謝 – user3430220 2015-02-11 21:46:18

相關問題