未使用局部变量的值警告

时间:2013-12-14 20:12:20

标签: android

我有这个适配器类:

package com.example.awesomefilebuilderwidget;

IMPORTS

public class GridViewAdapter extends BaseAdapter {
private Context Context;

// Keep all Images in array list
public ArrayList<Integer> drawables = new ArrayList<Integer>();

CheckBox mCheckBox=null;

// Constructor
public GridViewAdapter(Context c){
    Context = c;
    Log.d("GridViewAdapter", "Constructor is set");

    drawables.add(R.drawable.pattern1);
    Log.d("GridViewAdapter", "pattern1 added");

    drawables.add(R.drawable.pattern2);
    Log.d("GridViewAdapter", "pattern2 added");

    drawables.add(R.drawable.trashcan);
    Log.d("GridViewAdapter", "trashcan added");

    drawables.add(R.drawable.ic_launcher);
    Log.d("GridViewAdapter", "ic_launcher added");
}

public void setCheckBox(CheckBox checkbox){
    mCheckBox=checkbox;
}

@Override
// How many items are in the data set represented by this Adapter
public int getCount() {
    return drawables.size();
}

@Override
// Get the data item associated with the specified position in the
// data set
public Object getItem(int position) {
    return drawables.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

public boolean isSdReadable() {

    boolean mExternalStorageAvailable = false;
    String state = Environment.getExternalStorageState();

    if (Environment.MEDIA_MOUNTED.equals(state)) {
    // We can read and write the media
    mExternalStorageAvailable = true;
    Log.i("isSdReadable", "External storage card is readable.");
    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
    // We can only read the media
    Log.i("isSdReadable", "External storage card is readable.");
    mExternalStorageAvailable = true;
    } else {
    // Something else is wrong. It may be one of many other
    // states, but all we need to know is we can neither read nor write
    mExternalStorageAvailable = false;
    }

    return mExternalStorageAvailable;
    }

public Bitmap getThumbnail() {

    final String APP_PATH_SD_CARD = "/TEST/";
    final String APP_THUMBNAIL_PATH_SD_CARD = "thumbnails";
    String filename = "AFBWIcon.png";

    String fullPath = Environment.getExternalStorageDirectory().getAbsolutePath() + APP_PATH_SD_CARD + APP_THUMBNAIL_PATH_SD_CARD;
    Bitmap thumbnail = null;

    // Look for the file on the external storage
    try {
    if (isSdReadable() == true) {
    thumbnail = BitmapFactory.decodeFile(fullPath + "/" + filename);
    }
    } catch (Exception e) {
    Log.e("getThumbnail() on external storage", e.getMessage());
    }

    // If no file on external storage, look in internal storage
    if (thumbnail == null) {
    try {
    File filePath = Context.getFileStreamPath(filename);
    FileInputStream fi = new FileInputStream(filePath);
    thumbnail = BitmapFactory.decodeStream(fi);
    } catch (Exception ex) {
    Log.e("getThumbnail() on internal storage", ex.getMessage());
    }
    }
    return thumbnail;
    }



@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Try to reuse the views
    ImageView view = (ImageView) convertView;
    boolean checked = (mCheckBox==null)?false:(((CheckBox)  mCheckBox).isChecked());
    // if convert view is null then create a new instance else reuse it
    if (view == null) {
        view = new ImageView(Context);
        Log.d("GridViewAdapter", "new imageView added");
    }
    if(checked = true){
        isSdReadable();
        try {
            FileInputStream in = Context.openFileInput("BitmapImage");
            // Load back the image file to confirm it works
            Bitmap bitmap = BitmapFactory.decodeStream(in);
            view.setImageBitmap(bitmap);
            in.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // getThumbnail();
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } else {
        Log.e("GridView", "Icons not for use");
    }
    view.setImageResource(drawables.get(position));
    view.setScaleType(ImageView.ScaleType.CENTER_CROP);
    view.setLayoutParams(new android.widget.GridView.LayoutParams(70, 70));
    view.setTag(String.valueOf(position));
    return view;
}

}

在变量checked上(此处:

boolean checked = (mCheckBox==null)?false:(((CheckBox)  mCheckBox).isChecked()); 

) , 我得到一个警告说它没有被使用,即使我显然在接下来的几行中使用它。我不只是想添加supress警告,因为这是我的应用程序的一个功能,如果它工作可能非常有用(仅在选中复选框时运行编码)

我该怎么办?

1 个答案:

答案 0 :(得分:4)

您写道:

if (checked = true)

指定而不是比较checked。你的意思是:

if (checked == true)

也可以更简洁地写成:

if (checked)