2015-01-02 115 views
1

我創建了一個新的xml文件「test.xml」來測試ToggleButton,所以如果我在圖形佈局中拖動一個ToggleButton。佈局變得有點暗,但我無法在圖形佈局中看到拖動的ToggleButton。在text.xml中我可以看到它的代碼!如何在xml文件中創建ToggleButton?

拖動後,顯示此錯誤:

Exception raised during rendering: -1 
Exception details are logged in Window > Show View > Error Log 

在錯誤日誌文件中的錯誤:

java.lang.ArrayIndexOutOfBoundsException: -1 
 
\t at android.view.ShadowPainter.createDropShadow(ShadowPainter.java:116) 
 
\t at android.view.ShadowPainter.createDropShadow(ShadowPainter.java:48) 
 
\t at android.view.ViewGroup_Delegate.getPathShadow(ViewGroup_Delegate.java:140) 
 
\t at android.view.ViewGroup_Delegate.drawShadow(ViewGroup_Delegate.java:77) 
 
\t at android.view.ViewGroup_Delegate.drawChild(ViewGroup_Delegate.java:61) 
 
\t at android.view.ViewGroup.drawChild(ViewGroup.java:3404) 
 
\t at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198) 
 
\t at android.view.View.draw(View.java:15017) 
 
\t at android.view.ViewGroup.drawChild_Original(ViewGroup.java:3404) 
 
\t at android.view.ViewGroup_Delegate.drawChild(ViewGroup_Delegate.java:53) 
 
\t at android.view.ViewGroup.drawChild(ViewGroup.java:3404) 
 
\t at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198) 
 
\t at android.view.View.draw(View.java:15017) 
 
\t at android.view.ViewGroup.drawChild_Original(ViewGroup.java:3404) 
 
\t at android.view.ViewGroup_Delegate.drawChild(ViewGroup_Delegate.java:53) 
 
\t at android.view.ViewGroup.drawChild(ViewGroup.java:3404) 
 
\t at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198) 
 
\t at android.view.View.draw(View.java:15117) 
 
\t at android.view.View.draw(View.java:15022) 
 
\t at android.view.ViewGroup.drawChild_Original(ViewGroup.java:3404) 
 
\t at android.view.ViewGroup_Delegate.drawChild(ViewGroup_Delegate.java:53) 
 
\t at android.view.ViewGroup.drawChild(ViewGroup.java:3404) 
 
\t at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198) 
 
\t at android.view.View.draw(View.java:15117) 
 
\t at com.android.layoutlib.bridge.impl.RenderSessionImpl.render(RenderSessionImpl.java:587) 
 
\t at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:334) 
 
\t at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:332) 
 
\t at com.android.ide.eclipse.adt.internal.editors.layout.gle2.RenderService.createRenderSession(RenderService.java:517) 
 
\t at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.renderWithBridge(GraphicalEditorPart.java:1584) 
 
\t at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.recomputeLayout(GraphicalEditorPart.java:1309) 
 
\t at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.onXmlModelChanged(GraphicalEditorPart.java:1267) 
 
\t at com.android.ide.eclipse.adt.internal.editors.layout.LayoutEditorDelegate.delegateXmlModelChanged(LayoutEditorDelegate.java:402) 
 
\t at com.android.ide.eclipse.adt.internal.editors.common.CommonXmlEditor.xmlModelChanged(CommonXmlEditor.java:329) 
 
\t at com.android.ide.eclipse.adt.internal.editors.AndroidXmlEditor$XmlModelStateListener.modelChanged(AndroidXmlEditor.java:1653) 
 
\t at com.android.ide.eclipse.adt.internal.editors.AndroidXmlEditor.wrapEditXmlModel(AndroidXmlEditor.java:1114) 
 
\t at com.android.ide.eclipse.adt.internal.editors.AndroidXmlEditor.wrapUndoEditXmlModel(AndroidXmlEditor.java:1190) 
 
\t at com.android.ide.eclipse.adt.internal.editors.layout.gle2.MoveGesture.drop(MoveGesture.java:396) 
 
\t at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GestureManager$CanvasDropListener.drop(GestureManager.java:679) 
 
\t at org.eclipse.swt.dnd.DNDListener.handleEvent(DNDListener.java:91) 
 
\t at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84) 
 
\t at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4353) 
 
\t at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1061) 
 
\t at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1085) 
 
\t at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1070) 
 
\t at org.eclipse.swt.widgets.Widget.notifyListeners(Widget.java:782) 
 
\t at org.eclipse.swt.dnd.DropTarget.Drop(DropTarget.java:456) 
 
\t at org.eclipse.swt.dnd.DropTarget.Drop_64(DropTarget.java:392) 
 
\t at org.eclipse.swt.dnd.DropTarget$3.method6(DropTarget.java:260) 
 
\t at org.eclipse.swt.internal.ole.win32.COMObject.callback6(COMObject.java:119) 
 
\t at org.eclipse.swt.internal.ole.win32.COM.DoDragDrop(Native Method) 
 
\t at org.eclipse.swt.dnd.DragSource.drag(DragSource.java:363) 
 
\t at org.eclipse.swt.dnd.DragSource.access$0(DragSource.java:289) 
 
\t at org.eclipse.swt.dnd.DragSource$1.handleEvent(DragSource.java:172) 
 
\t at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84) 
 
\t at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4353) 
 
\t at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1061) 
 
\t at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4172) 
 
\t at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3761) 
 
\t at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$9.run(PartRenderingEngine.java:1151) 
 
\t at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332) 
 
\t at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1032) 
 
\t at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:148) 
 
\t at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:636) 
 
\t at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332) 
 
\t at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:579) 
 
\t at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:150) 
 
\t at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:135) 
 
\t at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196) 
 
\t at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:134) 
 
\t at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104) 
 
\t at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:380) 
 
\t at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:235) 
 
\t at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
 
\t at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
 
\t at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
 
\t at java.lang.reflect.Method.invoke(Method.java:606) 
 
\t at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:648) 
 
\t at org.eclipse.equinox.launcher.Main.basicRun(Main.java:603) 
 
\t at org.eclipse.equinox.launcher.Main.run(Main.java:1465)

<?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="match_parent" 
 
    android:orientation="vertical" > 
 

 
    <ToggleButton 
 
     android:id="@+id/toggleButton1" 
 
     android:layout_width="wrap_content" 
 
     android:layout_height="wrap_content" 
 
     android:text="ToggleButton" /> 
 
    
 
</LinearLayout>

+0

你看錯誤日誌中的錯誤嗎? –

+0

我將其添加到問題中。對我來說沒有什麼特別的錯誤。 –

+0

有同樣的問題 - 嘗試使用CheckBox或support-v7開關,至少在找到合適的解決方案之前。 –

回答

5

我的解決方案是在圖形佈局中切換API。我從API 21切換到API 19.

+0

你的答案適合我。在我的情況下,我設置了一個自定義形狀drawable.xml文件作爲按鈕的背景。它使用普通的.png編譯按鈕背景,但是當我嘗試使用shape.xml時,它失敗了。我的堆棧跟蹤的前幾行:'java.lang.ArrayIndexOutOfBoundsException:-1 \t at android.view.ShadowPainter.createDropShadow(ShadowPainter.java:116)' – GraSim

+0

只是好奇這是爲什麼,並且有我的自定義組件我需要做的是讓這個錯誤消失API21? – JPM

-1

它對我有用。另外android:text不是ToggleButton的有效屬性。使用android:textOnandroid:textOff來設置按鈕狀態的文本。