2013-08-15 16 views
5

Używam Android Studio stworzyli mój własny pogląd:Używanie atrybutów niestandardowych w widokach niestandardowych natomiast w układzie podglądu

<LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:gravity="center_horizontal|center_vertical" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="4dp"> 

    <Button 
      android:id="@+id/dateTile" 
      android:layout_height="@dimen/date_tile_height" 
      android:layout_width="@dimen/date_tile_width" 
      android:background="@drawable/bg_january"/> 

    <CheckBox 
      android:id="@+id/dateTileCheckbox" 
      android:button="@drawable/check_green" 
      android:layout_width="30dp" 
      android:layout_height="30dp" 
      android:layout_marginLeft="15dp" 
      android:layout_marginTop="-20dp" 
      android:focusable="false"/> 

    <TextView 
      android:id="@+id/dateTileLabel" 
      android:layout_width="wrap_content" 
      android:layout_marginTop="-10dp" 
      android:layout_height="wrap_content"/> 

</LinearLayout> 

dodatkowo mam zdefiniowane następujące atrybuty niestandardowe

<resources> 
    <declare-styleable name="DateTileView"> 
     <attr name="monthLabel" format="string" localization="suggested" /> 
     <attr name="tileBackground" format="reference" /> 
    </declare-styleable> 
</resources> 

używam niestandardowe atrybuty, aby zdefiniować etykietę i tło kafelka daty w konstruktorze takim jak ten.

public class DateTileView extends LinearLayout 
{ 
//.... 
//.... 
public DateTileView(Context context, AttributeSet attrs) 
    { 
     super(context, attrs); 
     TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DateTileView, 0, 0); 

     String monthLabel = a.getString(R.styleable.DateTileView_monthLabel); 
     Drawable monthBG = a.getDrawable(R.styleable.DateTileView_tileBackground); 

     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     inflater.inflate(R.layout.custom_view_date_tile, this, true); 

     TextView dateTileLabel = (TextView) findViewById(R.id.dateTileLabel); 
     dateTileLabel.setText(monthLabel); 

     Button dateTileButton = (Button) findViewById(R.id.dateTile); 
     dateTileButton.setBackgroundDrawable(monthBG); //TODO: remove deprecated use if JELLY BEAN 

     a.recycle(); 
    } 
//... 
//... 
} 

podczas korzystania datę płytki w innym formacie XML i recive ten błąd:

The following classes could not be instantiated: 
- com.endilo.widget.DateTileView (Open Class, Show Exception) 
    Tip: Use View.isInEditMode() in your custom views to skip code 
    or show sample data when shown in the IDE 

Exception Details java.lang.NullPointerException   at 
com.endilo.widget.DateTileView.<init>(DateTileView.java:35)   at 
java.lang.reflect.Constructor.newInstance   at 
android.view.LayoutInflater.rInflate_Original   at 
android.view.LayoutInflater_Delegate.rInflate   at 
android.view.LayoutInflater.rInflate   at 
android.view.LayoutInflater.rInflate_Original   at 
android.view.LayoutInflater_Delegate.rInflate   at 
android.view.LayoutInflater.rInflate   at 
android.view.LayoutInflater.rInflate_Original   at 
android.view.LayoutInflater_Delegate.rInflate   at 
android.view.LayoutInflater.rInflate   at 
android.view.LayoutInflater.inflate   at android.view.LayoutInflater.inflate 

Aplikacja działa dobrze, ale podgląd nie będzie. Czy istnieje sposób na załadowanie niestandardowych atrybutów, aby wyświetlić podgląd z niestandardowymi atrybutami?

+0

sam problem tutaj jakieś rozwiązanie? –

Odpowiedz

-1

Nie możesz zrobić wszystkiego, co masz w swojej linii 35, gdy aplikacja nie jest uruchomiona, ponieważ tryb projektowania otrzyma zerowe odniesienie dla jakiegoś obiektu, który jest tworzony tylko w środowisku wykonawczym.

Musisz unikać wykonanie tych w trybie projektowania przy użyciu isInEditMode(), tak jak tutaj:

if(!isInEditMode()) { 
    whatever there is in the line 35 of your class 
} 

Jeśli trzeba wykonać pewne zmiany w widoku, a ty chcesz być widoczny na projektant, przesłonić OnDraw(), jak tutaj:

@Override 
protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 

    TypedArray a = this.getContext().obtainStyledAttributes(attrs, R.styleable.DateTileView, 0, 0); 

    String monthLabel = a.getString(R.styleable.DateTileView_monthLabel); 
    Drawable monthBG = a.getDrawable(R.styleable.DateTileView_tileBackground); 

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    inflater.inflate(R.layout.custom_view_date_tile, this, true); 

    TextView dateTileLabel = (TextView) findViewById(R.id.dateTileLabel); 
    dateTileLabel.setText(monthLabel); 

    Button dateTileButton = (Button) findViewById(R.id.dateTile); 
    dateTileButton.setBackgroundDrawable(monthBG); //TODO: remove deprecated use if JELLY BEAN 

    a.recycle(); 
} 
Powiązane problemy