Issue
Im trying to set image resource of ImageView inside a fragment. But when the setImageResource(R.id.figure3) is triggered inside figurina method app crashes with NullPointerException Does anyone know where can be the problem?
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);
}
}
Solution
You are calling findViewById()
on the wrong view. See below: (notice the final
on View v
and changed the setOnKeyListener
to 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;
}
Answered By - alenz316
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.