2016-09-14 32 views
0

我正在使用Android Studio上下文菜單創建新的自定義視圖。當我添加新視圖時,構建被破壞。爲什麼Android Studio會添加破壞構建的代碼。Android Studio添加自定義視圖中斷

我正在運行Android Studio 2.1.3。

的過程如下...

  1. 我右鍵點擊我的src/JAVA/[my_package]文件夾中。
  2. 我點擊新建> UI組件>自定義視圖
  3. 在隨後的對話框中,我命名我的視圖並按回車鍵。
  4. 我打做項目,我碰到下面的錯誤...
Error:(7) No resource identifier found for attribute 'exampleColor' in package 'com.username.appname' 
Error:(7) No resource identifier found for attribute 'exampleDimension' in package 'com.username.appname' 
Error:(7) No resource identifier found for attribute 'exampleDrawable' in package 'com.username.appname' 
Error:(7) No resource identifier found for attribute 'exampleString' in package 'com.username.appname' 

下面是Android Studio中創建的類。除了重新編輯我的用戶和應用程序名稱信息之外,我還沒有觸及過單行代碼。

我好像錯誤是R.不能在文件中解決。

package com.username.appname; 

import android.content.Context; 
import android.content.res.TypedArray; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.drawable.Drawable; 
import android.text.TextPaint; 
import android.util.AttributeSet; 
import android.view.View; 
import android.widget.LinearLayout; 

import com.example.ringtonegenerator.R; 

/** 
* TODO: document your custom view class. 
*/ 
public class SwipeLayout extends LinearLayout { 
    private String mExampleString; // TODO: use a default from R.string... 
    private int mExampleColor = Color.RED; // TODO: use a default from R.color... 
    private float mExampleDimension = 0; // TODO: use a default from R.dimen... 
    private Drawable mExampleDrawable; 

    private TextPaint mTextPaint; 
    private float mTextWidth; 
    private float mTextHeight; 

    public SwipeLayout(Context context) { 
     super(context); 
     init(null, 0); 
    } 

    public SwipeLayout(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(attrs, 0); 
    } 

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

    private void init(AttributeSet attrs, int defStyle) { 
     // Load attributes 
     final TypedArray a = getContext().obtainStyledAttributes(
       attrs, R.styleable.SwipeLayout, defStyle, 0); 

     mExampleString = a.getString(
       R.styleable.SwipeLayout_exampleString); 
     mExampleColor = a.getColor(
       R.styleable.SwipeLayout_exampleColor, 
       mExampleColor); 
     // Use getDimensionPixelSize or getDimensionPixelOffset when dealing with 
     // values that should fall on pixel boundaries. 
     mExampleDimension = a.getDimension(
       R.styleable.SwipeLayout_exampleDimension, 
       mExampleDimension); 

     if (a.hasValue(R.styleable.SwipeLayout_exampleDrawable)) { 
      mExampleDrawable = a.getDrawable(
        R.styleable.SwipeLayout_exampleDrawable); 
      mExampleDrawable.setCallback(this); 
     } 

     a.recycle(); 

     // Set up a default TextPaint object 
     mTextPaint = new TextPaint(); 
     mTextPaint.setFlags(Paint.ANTI_ALIAS_FLAG); 
     mTextPaint.setTextAlign(Paint.Align.LEFT); 

     // Update TextPaint and text measurements from attributes 
     invalidateTextPaintAndMeasurements(); 
    } 

    private void invalidateTextPaintAndMeasurements() { 
     mTextPaint.setTextSize(mExampleDimension); 
     mTextPaint.setColor(mExampleColor); 
     mTextWidth = mTextPaint.measureText(mExampleString); 

     Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics(); 
     mTextHeight = fontMetrics.bottom; 
    } 

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

     // TODO: consider storing these as member variables to reduce 
     // allocations per draw cycle. 
     int paddingLeft = getPaddingLeft(); 
     int paddingTop = getPaddingTop(); 
     int paddingRight = getPaddingRight(); 
     int paddingBottom = getPaddingBottom(); 

     int contentWidth = getWidth() - paddingLeft - paddingRight; 
     int contentHeight = getHeight() - paddingTop - paddingBottom; 

     // Draw the text. 
     canvas.drawText(mExampleString, 
       paddingLeft + (contentWidth - mTextWidth)/2, 
       paddingTop + (contentHeight + mTextHeight)/2, 
       mTextPaint); 

     // Draw the example drawable on top of the text. 
     if (mExampleDrawable != null) { 
      mExampleDrawable.setBounds(paddingLeft, paddingTop, 
        paddingLeft + contentWidth, paddingTop + contentHeight); 
      mExampleDrawable.draw(canvas); 
     } 
    } 

    /** 
    * Gets the example string attribute value. 
    * 
    * @return The example string attribute value. 
    */ 
    public String getExampleString() { 
     return mExampleString; 
    } 

    /** 
    * Sets the view's example string attribute value. In the example view, this string 
    * is the text to draw. 
    * 
    * @param exampleString The example string attribute value to use. 
    */ 
    public void setExampleString(String exampleString) { 
     mExampleString = exampleString; 
     invalidateTextPaintAndMeasurements(); 
    } 

    /** 
    * Gets the example color attribute value. 
    * 
    * @return The example color attribute value. 
    */ 
    public int getExampleColor() { 
     return mExampleColor; 
    } 

    /** 
    * Sets the view's example color attribute value. In the example view, this color 
    * is the font color. 
    * 
    * @param exampleColor The example color attribute value to use. 
    */ 
    public void setExampleColor(int exampleColor) { 
     mExampleColor = exampleColor; 
     invalidateTextPaintAndMeasurements(); 
    } 

    /** 
    * Gets the example dimension attribute value. 
    * 
    * @return The example dimension attribute value. 
    */ 
    public float getExampleDimension() { 
     return mExampleDimension; 
    } 

    /** 
    * Sets the view's example dimension attribute value. In the example view, this dimension 
    * is the font size. 
    * 
    * @param exampleDimension The example dimension attribute value to use. 
    */ 
    public void setExampleDimension(float exampleDimension) { 
     mExampleDimension = exampleDimension; 
     invalidateTextPaintAndMeasurements(); 
    } 

    /** 
    * Gets the example drawable attribute value. 
    * 
    * @return The example drawable attribute value. 
    */ 
    public Drawable getExampleDrawable() { 
     return mExampleDrawable; 
    } 

    /** 
    * Sets the view's example drawable attribute value. In the example view, this drawable is 
    * drawn above the text. 
    * 
    * @param exampleDrawable The example drawable attribute value to use. 
    */ 
    public void setExampleDrawable(Drawable exampleDrawable) { 
     mExampleDrawable = exampleDrawable; 
    } 
} 
+0

你能顯示出現這些錯誤的來源嗎? –

+0

我的問題是在這個線程中的答案。這裏指導的協議是什麼? http://stackoverflow.com/questions/5819369/error-no-resource-identifier-found-for-attribute-adsize-in-package-com-googl – ScottF

+0

只需關閉你自己的問題。您也可以編輯它以鏈接到另一個問題,並解釋您將其作爲副本關閉。 –

回答

0

當您通過此菜單添加新視圖時,Studio會使用一些示例代碼和資源填充它。它出現了一些它沒有添加的示例代碼引用屬性。您將需要在資源文件中提供它需要的資源(exampleColor等)或將示例引用更改爲真實資源。

+0

嗯。我寧願如果它或者正確地創建屬性,或者根本就不參考它們。看起來像這樣比使用創建不可編譯代碼的工具更有意義... – ScottF

+0

@ScottF - 我也使用AS 2.1.3,它生成的代碼對我來說沒有錯誤。也許其他事情正在發生。在其中一個資源文件中查找錯誤。也許在生成的代碼和現有的代碼之間存在名稱衝突。 (如果這不是您爲您生成的第一個新的自定義視圖代碼,肯定很有可能。) –

+0

嗯有趣。這絕對是我在這個項目中所做的第一個自定義視圖。但是知道AS有可能正確做到這一點是很好的。你的代碼與我的相匹配嗎? – ScottF

相關問題