2014-10-06 108 views
1

我正在使用Android studio进行我的开发,这是我的脸。我尝试了下面显示的提示,但仍然没有任何工作。唯一的错误行我得到的是“显示java.lang.NullPointerException”比这没有更多的解释,下面Android Studio无法识别自定义视图类

Rendering Problems The following classes could not be instantiated: 
- com.xxx.xxx.util.CircleImageView 
      (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 

窗口,这里是我的XML代码....希望你能帮助球员:/

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/white"> 

    <LinearLayout 
     android:layout_height="match_parent" 
     android:layout_width="match_parent"> 

     <LinearLayout 
      android:layout_height="240dp" 
      android:layout_width="match_parent" 
      android:orientation="vertical"> 

      <com.xxx.xxx.util.CustomImageButton 
       android:layout_height="match_parent" 
       android:layout_width="match_parent" 
       android:background="@drawable/ic_launcher" /> 

      <com.xxx.xxx.util.CircleImageView 
       android:layout_width="100dp" 
       android:layout_height="100dp" 
       android:background="@drawable/ic_plusone_tall_off_client" /> 
     </LinearLayout> 
    </LinearLayout> 

</FrameLayout> 

类框架

public class CircleImageView extends ImageView { 
    // Border & Selector configuration variables 
    private boolean hasBorder; 
    private boolean hasSelector; 
    private boolean isSelected; 
    private int borderWidth; 
    private int canvasSize; 
    private int selectorStrokeWidth; 

    // Objects used for the actual drawing 
    private BitmapShader shader; 
    private Bitmap image; 
    private Paint paint; 
    private Paint paintBorder; 
    private Paint paintSelectorBorder; 
    private ColorFilter selectorFilter; 

    public CircleImageView(Context context) { 
     this(context, null); 
    } 

    public CircleImageView(Context context, AttributeSet attrs) { 
     this(context, attrs, R.attr.CircleImageViewStyle); 
    } 

    public CircleImageView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     init(context, attrs, defStyle); 
    } 

    /** 
    * Initializes paint objects and sets desired attributes. 
    * 
    * @param context 
    * @param attrs 
    * @param defStyle 
    */ 
    private void init(Context context, AttributeSet attrs, int defStyle) { 
     ...code 
    } 

    /** 
    * Sets the CircleImageView's border width in pixels. 
    * 
    * @param borderWidth 
    */ 
    public void setBorderWidth(int borderWidth) { 
    ...code 
    } 

    /** 
    * Sets the CircleImageView's basic border color. 
    * 
    * @param borderColor 
    */ 
    public void setBorderColor(int borderColor) { 
    ...code 
    } 

    /** 
    * Sets the color of the selector to be draw over the 
    * CircleImageView. Be sure to provide some opacity. 
    * 
    * @param selectorColor 
    */ 
    public void setSelectorColor(int selectorColor) { 
    ...code 
    } 

    /** 
    * Sets the stroke width to be drawn around the CircleImageView 
    * during click events when the selector is enabled. 
    * 
    * @param selectorStrokeWidth 
    */ 
    public void setSelectorStrokeWidth(int selectorStrokeWidth) { 
    ...code 
    } 

    /** 
    * Sets the stroke color to be drawn around the CircleImageView 
    * during click events when the selector is enabled. 
    */ 
    public void setSelectorStrokeColor(int selectorStrokeColor) { 
    ...code 
    } 

    /** 
    * Adds a dark shadow to this CircleImageView. 
    */ 
    public void addShadow() { 
    ...code 
    } 

    @Override 
    public void onDraw(Canvas canvas) { 
     ...code 
    } 

    @Override 
    public boolean dispatchTouchEvent(MotionEvent event) { 
    ...code 
    } 

    public void invalidate(Rect dirty) { 
    ...code 
    } 

    public void invalidate(int l, int t, int r, int b) { 
    ...code 
    } 

    @Override 
    public void invalidate() { 
    ...code 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    ...code 
    } 

    private int measureWidth(int measureSpec) { 
    ...code 
    } 

    private int measureHeight(int measureSpecHeight) { 
    ...code 
    } 

    /** 
    * Convert a drawable object into a Bitmap 
    * 
    * @param drawable 
    * @return 
    */ 
    public Bitmap drawableToBitmap(Drawable drawable) { 
    ...code 
    } 

    /** 
    * Reinitializes the shader texture used to fill in 
    * the Circle upon drawing. 
    */ 
    public void refreshBitmapShader() { 
    ...code 
    } 

    /** 
    * Returns whether or not this view is currently 
    * in its selected state. 
    */ 
    public boolean isSelected() { 
    ...code 
    } 

    @Override 
    public boolean isInEditMode() { 
    ...code 
    } 
} 
+0

如果那些无法实例化的类也显示在您的问题中,这将会有所帮助。 – jsfrocha 2014-10-06 23:48:26

+0

单击“显示异常”并复制堆栈。 – kcoppock 2014-10-06 23:48:40

+0

堆栈中唯一的东西是 java.lang.NullPointerException – NW52 2014-10-06 23:49:29

回答

3

使用

if (!isInEditMode()) 
CircleImageView中init()之前的

有多个帖子在讨论这个问题,如this one

编辑:我不确定你是否使用过这样的提示,请提供一个你尝试过的例子,如果是这样的话。

+0

它在eclipse中工作得很好。这是一个不错的主意移动到Android工作室 – NW52 2014-10-07 16:50:06

+0

这种方法是在我的Android Studio工作,有你的最新版本?你能否提供一个给你NullPointerException的代码示例? – jujux789 2014-10-07 16:56:21

+0

是的,我确实有最新版本。如果(!isInEditMode())在eclipse中解决了这个问题:) – NW52 2014-10-07 17:00:56