创建活动后,我收到此警告:
永远不会使用变量'station'
我不确定该警告意味着什么,也不确定如何解决。我猜测'station'变量尚未声明,但我不知道正确的声明方式。所有的帮助将不胜感激。
FragmentWCLine.java
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class FragmentWCLine extends android.support.v4.app.Fragment {
public final static String EXTRA_MESSAGE = "Station_key";
private class WC {
private CharSequence station;
private CharSequence zone;
private Class<? extends Activity> activityClass;
private Class<? extends android.support.v4.app.Fragment> fragmentClass;
public WC(int stationResId, int zoneResId, Class<? extends Activity> activityClass, Class<? extends android.support.v4.app.Fragment> fragmentClass) {
this.fragmentClass = fragmentClass;
this.activityClass = activityClass;
this.station = getResources().getString(stationResId);
this.zone = getResources().getString(zoneResId);
}
@Override
public String toString() { return station.toString(); }
public String getzone(){ return zone.toString(); }
}
private static WC[] mWC;
private boolean mTwoPane;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.fragment_wc_line, container, false);
// Instantiate the list of stations.
mWC = new WC[]{
new WC(R.string.bank, R.string.zone_1, WCBankActivity.class, FragmentWCBank.class),
new WC(R.string.wat, R.string.zone_1, WCWATActivity.class, FragmentWCWAT.class)
};
final ListView listView = (ListView)v.findViewById(R.id.list_wc);
listView.setAdapter(new MyAdapter(getActivity(), mWC));
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(mTwoPane){
setItemNormal();
View rowView = view;
setItemSelected(rowView);
}
else{
Intent intent = new Intent(getActivity(), mWC[position].activityClass);
String station = mWC[position].station.toString();
intent.putExtra(EXTRA_MESSAGE, station);
startActivity(intent);
}
}
public void setItemSelected(View view){
View rowView = view;
view.setBackgroundColor(Color.parseColor("#66CCCC"));
TextView tv0 = (TextView)rowView.findViewById(R.id.list_item_station);
tv0.setTextColor(Color.parseColor("#000099"));
TextView tv1 = (TextView)rowView.findViewById(R.id.list_item_zone);
tv1.setTextColor(Color.parseColor("#000099"));
}
public void setItemNormal()
{
for (int i=0; i< listView.getChildCount(); i++) {
View v = listView.getChildAt(i);
v.setBackgroundColor(Color.TRANSPARENT);
TextView tv0 = ((TextView) v.findViewById(R.id.list_item_station));
tv0.setTextColor(Color.WHITE);
TextView tv1 = ((TextView) v.findViewById(R.id.list_item_zone));
tv1.setTextColor(Color.parseColor("#B5B5B5"));
}
}
});
return v;
}
static class MyAdapter extends BaseAdapter {
static class ViewHolder {
TextView station;
TextView zone;
}
LayoutInflater inflater;
WC[] mWC;
public MyAdapter(Context contexts, WC[] samples) {
this.mWC = samples;
inflater = LayoutInflater.from(contexts);
}
@Override
public int getCount() {
return mWC.length;
}
@Override
public Object getItem(int position) {
return mWC[position];
}
@Override
public long getItemId(int position) {
return 0;
}
/**set selected position**/
private int selectPosition = -1;
public void setSelectPosition(int position){
if(position!=selectPosition){
selectPosition = position;
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item_dualline, null);
viewHolder = new ViewHolder();
viewHolder.station = (TextView) convertView.findViewById(R.id.list_item_station);
viewHolder.zone = (TextView) convertView.findViewById(R.id.list_item_zone);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.station.setText(mWC[position].station);
viewHolder.zone.setText(mWC[position].getzone());
//change item color
if(position==selectPosition){
convertView.setBackgroundColor(Color.parseColor("#000099"));
viewHolder.station.setTextColor(Color.parseColor("#000099"));
}else {
}
return convertView;
}
}
}
WCBankActivity.java
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
public class WCBankActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "Station_key";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_wc_bank);
if (savedInstanceState == null) {
// Get the message from the intent
Intent intent = getIntent();
// Notice to specify the sender Activity for the message
String station = intent.getStringExtra(WCBankActivity.EXTRA_MESSAGE);
FragmentWCBank newFragment = new FragmentWCBank();
FragmentTransaction transaction = this.getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.detail_container, newFragment);
transaction.commit();
}
}
}
FragmentWCBank.java
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentWCBank extends android.support.v4.app.Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_wc_bank, container, false);
return v;
}
}
答案 0 :(得分:3)
您看到的是警告而不是错误。差异是警告不会阻止您的程序运行,但会出现错误。
您看到的警告是由于您声明变量station
但从未使用过它。你的IDE很聪明,可以注意到这一点,并试图警告你,提醒你不要使用你创建的变量。
在这种情况下,这一行:
String station = intent.getStringExtra(WCBankActivity.EXTRA_MESSAGE);
你声明一个变量station
并给它一个值但是你永远不会读取它或用它做任何其他事情。因此,理论上您可以删除变量而不会对您的应用产生任何影响。