我应该如何更正代码以摆脱这些“未使用的值”警告?

时间:2012-06-25 16:47:04

标签: android eclipse location

这些是我收到的错误消息,请指教。

未使用字段LocationListActivity.adapter的值

未使用字段LocationListActivity.lv1的值

未使用字段LocationListActivity.titleString的值

public class LocationListActivity extends ExpandableListActivity {

private ArrayAdapter<String> adapter = null;
private String name;
private ArrayList<List<LocationData>> locations; //Array list of all locations
private ArrayList<LocationData> locationList; // locations for each group
private ListView lv1;
private ArrayList<String> locationSection = new ArrayList<String>();
private ArrayList<String> sectionLocations = new ArrayList<String>();
private ArrayList<ArrayList<String>> allLocations = new ArrayList<ArrayList<String>>();
private String titleString;
private ExpandableListAdapter mAdapter;
private Button search;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.location_list_activity);

    search = (Button)findViewById(R.id.search_button);
    search.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            startActivity(new Intent(LocationListActivity.this, LocationListSearchActivity.class));
        }
    });

    //PARSE locations.xml
    try { 
        this.locations = new ArrayList<List<LocationData>>();
        this.locationList = new ArrayList<LocationData>();

        XmlResourceParser xrp = this.getResources().getXml(R.xml.locations);

        LocationData currentLocation = null;
        while(xrp.getEventType() != XmlResourceParser.END_DOCUMENT) {
            currentLocation = new LocationData();
            if(xrp.getEventType() == XmlResourceParser.START_TAG) {
                if(xrp.getName().equals("section")) {
                    locationSection.add(xrp.getAttributeValue(null, "title"));

                } 
                else if(xrp.getName().equals("location")) {
                    currentLocation.setBuilding((xrp.getAttributeValue(null, "building")));
                    name = xrp.getAttributeValue(null, "name");

                    sectionLocations.add(name);
                    currentLocation.setName(name);
                    currentLocation.setDetailName((xrp.getAttributeValue(null, "detailName")));
                    currentLocation.setLat(Double.parseDouble(xrp.getAttributeValue(null, "lat")));
                    currentLocation.setLng(Double.parseDouble(xrp.getAttributeValue(null, "lng")));
                    currentLocation.setPhone(xrp.getAttributeValue(null, "phone"));
                    locationList.add(currentLocation);
                }
            }
            else if(xrp.getEventType() == XmlResourceParser.END_TAG) {
                if(xrp.getName().equals("section")) {
                    allLocations.add(new ArrayList<String>(sectionLocations));
                    sectionLocations.clear();
                    locations.add(new ArrayList<LocationData>(locationList));
                    locationList.clear();
                }
            }
            xrp.next();
        }   
        xrp.close();}
        catch (XmlPullParserException  xppe) {} 
        catch (IOException e) {}

     // Set up our adapter
        mAdapter = new MyExpandableListAdapter();
        setListAdapter(mAdapter);


}


public class MyExpandableListAdapter extends BaseExpandableListAdapter {

    public Object getChild(int groupPosition, int childPosition) {
        return allLocations.get(groupPosition).get(childPosition);
    }

    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    public int getChildrenCount(int groupPosition) {
        return allLocations.get(groupPosition).size();
    }

    public TextView getGenericView() {
        // Layout parameters for the ExpandableListView
        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.FILL_PARENT, 80);

        TextView textView = new TextView(LocationListActivity.this);
        textView.setLayoutParams(lp);
        // Center the text vertically
        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        // Set the text starting position
        textView.setBackgroundResource(R.drawable.button_click);
        textView.setTypeface(null, android.graphics.Typeface.BOLD);
        textView.setTextColor(0xFF000000);
        textView.setPadding(55, 0, 0, 0);
        return textView;
    }

    public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
            View convertView, ViewGroup parent) {
        TextView textView = getGenericView();
        textView.setText(getChild(groupPosition, childPosition).toString());
        textView.setTypeface(null, android.graphics.Typeface.NORMAL);
        return textView;
    }

    public Object getGroup(int groupPosition) {
        return locationSection.get(groupPosition);
    }

    public int getGroupCount() {
        return locationSection.size();
    }

    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
            ViewGroup parent) {
        TextView textView = getGenericView();
        textView.setText(getGroup(groupPosition).toString());
        return textView;
    }

    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    public boolean hasStableIds() {
        return true;
    }

}

  public boolean onChildClick (ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
    Intent i = new Intent( LocationListActivity.this, LocationInfoActivity.class );

        i.putExtra("name", locations.get(groupPosition).get(childPosition).getName());
        i.putExtra("building", locations.get(groupPosition).get(childPosition).getBuilding());
        i.putExtra("detailName", locations.get(groupPosition).get(childPosition).getDetailName());
        i.putExtra("lat", locations.get(groupPosition).get(childPosition).getLat());
        i.putExtra("lng", locations.get(groupPosition).get(childPosition).getLng());
        i.putExtra("phone", locations.get(groupPosition).get(childPosition).getPhone());
        startActivity(i);
    return false;
  }

@Override
public boolean onCreateOptionsMenu(Menu menu){
    MenuInflater inflater = this.getMenuInflater();
    inflater.inflate(R.menu.menu_home, menu);
    return true;
}

 //Called when user selects a menu item;
@Override
public boolean onOptionsItemSelected( MenuItem item ){

    switch(item.getItemId()) {
    case R.id.menu_search:
        startActivity(new Intent(this, LocationListSearchActivity.class));
        finish();
    break;  
    case R.id.menu_about:
        startActivity(new Intent(this, AboutActivity.class));
        finish();
    break;              
    }   
    return true;
}

}

3 个答案:

答案 0 :(得分:3)

您可以使用

@SuppressWarnings("unused")


注意:类似的事情发生在我身上。我有变量,所有都被使用但警告仍然出现。所以在这种情况下这种方法非常好。

但是如果你有一些你还没有使用过的变量,我建议删除或评论它们。

答案 1 :(得分:1)

您可以删除或注释掉这些变量。如果您不想这样做,您也可以在Eclipse中更改警告。

答案 2 :(得分:1)

有些时候我有变量,我知道使用过,我无法弄清楚为什么它一直说它们不是。出于某种原因,程序其他部分的某些错误可能会导致这种情况发生。或者,如果您不打算使用突出显示的内容,那么我只想删除它。