更改片段内的 ImageView 资源时出现空指针异常

Null pointer exception while changing ImageView resource inside fragment

我正在尝试在片段中设置 ImageView 的图像资源。但是当在 figurina 方法中触发 setImageResource(R.id.figure3) 时,app 会因 NullPointerException 而崩溃 有谁知道问题出在哪里?

public class WeatherStationFragment extends Fragment {

private EditText locationField;
private TextView zobrazovac;
private static ImageView cityConfirm;

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
    View v = inflater.inflate(R.layout.weather_station_fragment, parent, false);
    zobrazovac = (TextView)v.findViewById(R.id.zobrazovac);
    locationField = (EditText)v.findViewById(R.id.locationInput);

            // vstup pre lokaciu
    locationField.setGravity(CENTER);
    locationField.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                WeatherInfoRetrieval weatherInfoRetrieval = new WeatherInfoRetrieval();
                zobrazovac.setText(weatherInfoRetrieval.getJsonData(locationField.getText().toString()));
                if(zobrazovac.getText().toString().equals("nefacha")){
                    return false;
                }
                figurina(v);
                return true;
            }
            return false;
        }
    });

    return v;

}

public void figurina(View v){
    int temperature = 0;
    ImageView figura = (ImageView)v.findViewById(R.id.figure);
    temperature = Integer.parseInt(zobrazovac.getText().toString());
    zobrazovac.setText(temperature + "C");
    if(temperature < 0){
        figura.setImageResource(R.mipmap.figure6);
    }
    if(temperature < 10 && temperature >= 0){
        figura.setImageResource(R.mipmap.figure5);
    }
    if(temperature < 16 && temperature >= 10){
        figura.setImageResource(R.mipmap.figure4);
    }
    if(temperature < 23 && temperature >= 16){
        figura.setImageResource(R.mipmap.figure3);
    }
    if(temperature < 29 && temperature >= 23){
        figura.setImageResource(R.mipmap.figure2);
    }
    if(temperature < 35 && temperature >= 29){
        figura.setImageResource(R.mipmap.figure1);
    }
    if(temperature >= 35){
        figura.setImageResource(R.mipmap.figure0);
    }
}

您调用 findViewById() 的视图有误。见下文:(注意 View v 上的 final 并将 setOnKeyListener 更改为 View editText

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
    final View v = inflater.inflate(R.layout.weather_station_fragment, parent, false); 
    zobrazovac = (TextView)v.findViewById(R.id.zobrazovac); 
    locationField = (EditText)v.findViewById(R.id.locationInput); 

            // vstup pre lokaciu 
    locationField.setGravity(CENTER); 
    locationField.setOnKeyListener(new View.OnKeyListener() { 
        @Override 
        public boolean onKey(View editText, int keyCode, KeyEvent event) { 
            if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) { 
                WeatherInfoRetrieval weatherInfoRetrieval = new WeatherInfoRetrieval(); 
                zobrazovac.setText(weatherInfoRetrieval.getJsonData(locationField.getText().toString())); 
                if(zobrazovac.getText().toString().equals("nefacha")){ 
                    return false; 
                } 
                figurina(v); 
                return true; 
            } 
            return false; 
        } 
    }); 

    return v; 
}