2013-08-15 10 views
5

मैं एंड्रॉयड स्टूडियो का उपयोग करें और अपने कस्टम दृश्य बनाया है:का उपयोग करते हुए कस्टम कस्टम दृश्य में जिम्मेदार बताते हैं, जबकि पूर्वावलोकन लेआउट में

<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> 

अलावा मैं निम्नलिखित कस्टम परिभाषित किया है विशेषताओं का

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

मैं का उपयोग इस तरह के निर्माता में दिनांक टाइल के लेबल और पृष्ठभूमि को परिभाषित करने के लिए कस्टम विशेषताओं।

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 

एप्लिकेशन ठीक काम करता है, लेकिन पूर्वावलोकन कार्य नहीं करेंगे:

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(); 
    } 
//... 
//... 
} 

जब एक और एक्सएमएल में तारीख टाइल का उपयोग करके मैं इस त्रुटि recive। कस्टम विशेषताओं के साथ पूर्वावलोकन दिखाने के लिए कस्टम विशेषताओं को लोड करने का कोई तरीका है?

+0

एक ही समस्या,: डिजाइनर, OnDraw() ओवरराइड , यहाँ की तरह? –

उत्तर

-1

जब आप एप्लिकेशन नहीं चल रहे हैं तो आप 35 लाइन में जो कुछ भी कर रहे हैं, वह नहीं कर सकते हैं, क्योंकि डिज़ाइन मोड को कुछ ऑब्जेक्ट के लिए एक शून्य संदर्भ मिलेगा जो केवल रनटाइम पर तत्काल होता है।

आप isInEditMode (का उपयोग करते हुए डिजाइन मोड वाले के निष्पादन) से बचने के लिए, यहाँ की तरह है:

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

आप ध्यान में रखते हुए कुछ परिवर्तन प्रदर्शन करने की जरूरत है, और आप उस पर दिखाई देना चाहते हैं यहां किसी भी समाधान

@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(); 
} 
संबंधित मुद्दे