2013-12-10 21 views
1

在ImageView上使用setBackgroundDrawable方法時,Eclipse提供了以下錯誤: 「類型View中的方法setBackgroundDrawable(Drawable)不適用於參數(int)」 My ImageView的是XML定義爲:在ImageView上使用setBackgroundDrawable時出現Eclipse錯誤

<ImageView 
       android:id="@+id/ivCheck1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentRight="true" 
       android:layout_centerVertical="true" 
       android:src="@drawable/box" 
       android:contentDescription=" " /> 

定義和Java作爲實現:

public class QuizList extends Activity implements View.OnClickListener { 

ImageView check1; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.theView); 
    IntializeVariables(); 
    IntializeChecks() 
} 

private void IntializeVariables() { 
    // TODO Auto-generated method stub 

    check1 = (ImageView) findViewById(R.id.ivCheck1); 

} 

private void IntializeChecks() { 
    // TODO Auto-generated method stub 
    boolean check = getPrefs.getBoolean("quiz1pref", false); 
    if (check == true) { 
     check1.setBackgroundDrawable(R.drawable.check); 
    } 
} 

任何人有任何想法,爲什麼它給這個錯誤,或者如何解決呢?

回答

3
check1.setBackgroundDrawable(R.drawable.check); 

這是錯誤。請改爲

check1.setBackgroundResource(R.drawable.check); 

check1.setBackgroundDrawable(getResources().getDrawable(R.drawable.check)); 
0

試試這個:

check1.setBackgroundDrawable(getResources().getDrawable(R.drawable.check)); 
0

相反的: -

check1.setBackgroundDrawable(R.drawable.check); 

使用此: -

check1.setImageResource(R.drawable.check); 
+0

@JoelFernandes,你有什麼編輯,我沒有得到它? –

+0

每當您在回答或評論中使用代碼時,請用代碼標記附上代碼,以便代碼顯示格式。這就是我編輯的。 – JoelFernandes

+0

哦,謝謝你的回答。 :) –

相關問題