2012-09-08 148 views
2

我試圖通過重寫Button類來創建3狀態切換按鈕。Android:問題創建自定義3狀態切換按鈕

儘管代碼中沒有錯誤,但Eclipse在佈局xml中顯示以下問題。

以下類不能被實例化:

- com.example.threewaytoggle.TriToggleButton (Open Class, Show Error Log) 
See the Error Log (Window > Show View) for more details. 
Tip: Use View.isInEditMode() in your custom views to skip code when shown in Eclipse 

,並在錯誤日誌,這是未來:

com.example.threewaytoggle.TriToggleButton failed to instantiate. 

java.lang.NullPointerException 
at android.view.View.mergeDrawableStates(View.java:7506) 
at com.example.threewaytoggle.TriToggleButton.onCreateDrawableState(TriToggleButton.java:42) 
at android.view.View.getDrawableState(View.java:7410) 
at android.view.View.setBackgroundDrawable(View.java:7583).... 

以下是代碼: 佈局的xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <com.example.threewaytoggle.TriToggleButton 
     android:id="@+id/triToggleButton1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="TriToggleButton" /> 
</LinearLayout> 

個自定義按鈕:

package com.example.threewaytoggle; 

import android.content.Context; 
import android.util.AttributeSet; 
import android.widget.Button; 

public class TriToggleButton extends Button { 

    int _state = 0; 
    public TriToggleButton(Context context) { 
     super(context); 
     _state = 0; 
     this.setText("1"); 
    } 

    public TriToggleButton(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     _state = 0; 
     this.setText("1"); 
    } 

    public TriToggleButton(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     _state = 0; 
     this.setText("1"); 
    } 

    private final int[] STATE_ONE_SET = { R.attr.state_one }; 
    private final int[] STATE_TWO_SET = { R.attr.state_two }; 
    private final int[] STATE_THREE_SET = { R.attr.state_three }; 

    @Override 
    protected int[] onCreateDrawableState(int extraSpace) { 
     final int[] drawableState = super.onCreateDrawableState(extraSpace + 3); 

     if (_state == 0) { 
      mergeDrawableStates(drawableState, STATE_ONE_SET); 
     } else if (_state == 1) { 
      mergeDrawableStates(drawableState, STATE_TWO_SET); 
     } else if (_state == 2) { 
      mergeDrawableStates(drawableState, STATE_THREE_SET); 
     } 

     return drawableState; 
    } 

    @Override 
    public boolean performClick() { 
     nextState(); 
     return super.performClick(); 
    } 

    private void nextState() { 
     _state++; 
     if (_state > 2) { 
      _state = 0; 
     } 
     setButtonText(); 
    } 

    private void setButtonText() { 
     //TODO  
    } 

    public int getState() { 
     return _state; 
    } 
} 
+0

我很好奇attr.xml和drawable xml的樣子。 –

+0

您可以使用模而不是++和邊界檢查。 –

+0

另外,我使用枚舉而不是整數。這使我可以將數組保存爲枚舉的一部分。 –

回答

3

發現....只有靜態的失蹤在:

private static final int[] STATE_ONE_SET = { R.attr.state_one }; 
private static final int[] STATE_TWO_SET = { R.attr.state_two }; 
private static final int[] STATE_THREE_SET = { R.attr.state_three }; 

但如何走到這造成了一個問題......?

+2

回答你的問題:這會產生一個問題,因爲'onCreateDrawableState'在父類的構造函數中被調用。這意味着你的對象的構造函數還沒有完成,所以當它們不是靜態時,它們還沒有被初始化。 –