2011-01-22 22 views
8

Tôi gặp sự cố khi nhận được getIdentifier để hoạt động với biến lớp. Thật kỳ lạ, bởi vì công trình này:android/java getIdentifier với

public Drawable getStationIcon(Context context) { 
    int resId = context.getResources().getIdentifier("m1m2_16", "drawable", "com.mypackage.namehere"); 
    Drawable drawable = context.getResources().getDrawable(resId); 
    return drawable; 
} 

Nhưng điều này không:

public Drawable getStationIcon(Context context) { 
    int resId = context.getResources().getIdentifier(this.stationIcon, "drawable", "com.mypackage.namehere"); 
    Drawable drawable = context.getResources().getDrawable(resId); 
    return drawable; 
} 

Và cũng không thực hiện điều này:

public Drawable getStationIcon(Context context) { 
    String stationI = this.stationIcon; 
    int resId = context.getResources().getIdentifier(stationI, "drawable", "com.mypackage.namehere"); 
    Drawable drawable = context.getResources().getDrawable(resId); 
    return drawable; 
} 

this.stationIcon chắc chắn bằng m1m2_16. Tôi đã thử các lựa chọn thay thế khác, tức là sử dụng ""+this.stationIcon, nhưng không có gì hoạt động khi tham số đầu tiên là một biến. Có cái gì tôi đang mất tích?

+0

tôi sẽ đề nghị để đăng nhập 'Log.d (TAG, "stationIcon =" + this.stationIcon) ' 'this.stationIcon' ngay trước khi sử dụng để đảm bảo nó thực sự là" m1m2_16 ". –

+0

Tôi đã làm như vậy và chắc chắn là m1m2_16 – Ashley

Trả lời

Các vấn đề liên quan