2012-06-25 42 views
0

這些是我收到的錯誤消息,請指教。如何更正代碼以擺脫這些「未使用的值」警告?

領域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; 
} 

}

+2

只需刪除不在使用的值/變量。 – Raykud

回答

3

您可以使用

@SuppressWarnings("unused") 


注意:類似的事情發生在我身上。我有變量,所有使用,但警告仍然出現。所以在這種情況下,這種方法非常好。

但是,如果你有變量,你已經不使用無處不在,我建議刪除它們或評論他們。

1

您可以刪除或註釋掉這些變量。如果你不想這樣做,你也可以改變Eclipse中的警告。

1

有些時候,我有變量,我知道被使用,我不明白爲什麼它一直說,他們不是。出於某種原因,程序其他部分的某些錯誤可能會導致這種情況發生。或者,如果您不打算使用它突出顯示的內容,那麼我會刪除它。