Ich mache eine App, um Wetterdaten von openweathermap zu ziehen und es in einer Listview anzeigen und es wird weiterhin eine Nullpointerexception Fehler und Absturz. Dies geschieht, wenn ich versuche, die Methode setListAdapter (Adapter) aufzurufen. Ich habe versucht, dieses Problem jetzt für ein paar Tage selbst zu beheben, aber ich kann das Problem nicht finden. Ich würde wirklich jede Hilfe schätzen, die ich bekommen konnte.NullPointerException auf android benutzerdefinierten Adapter
Hier ist meine Haupttätigkeit Klasse
public class MainActivity extends ListActivity {
String mainUrl = "http://api.openweathermap.org/data/2.5/forecast/daily?q=";
String accessCode = "&APPID=4907e373098f091c293b36b92d8f0886";
List<WeatherModel> weatherlist;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText getcityview = (EditText)findViewById(R.id.City);
Button searchButton = (Button) findViewById(R.id.search);
searchButton.setOnClickListener(search);
}
public String getcity(){
EditText getcityview = (EditText)findViewById(R.id.City);
String Cityname = getcityview.getText().toString().trim();
String result = deleteSpace(Cityname);
return result;
}
public String deleteSpace(String city){
int loc = city.indexOf(" ");
if (loc != -1){
String result = city.substring(0, loc) + city.substring(loc + 1);
return result;
}
else {
return city;
}
}
public void showForecast(){
EditText getcityview = (EditText)findViewById(R.id.City);
if (!isEmpty(getcityview)) {
Log.d("Status:", "running");
String cityName = getcity();
String url = mainUrl + cityName + accessCode;
JsonParser Jparser = new JsonParser();
Jparser.execute(url);
}
else {
Toast.makeText(getApplicationContext(), "No City entered", Toast.LENGTH_SHORT);
}
}
private boolean isEmpty(EditText etText) {
return etText.getText().toString().trim().length() == 0;
}
public View.OnClickListener search = new View.OnClickListener() {
@Override
public void onClick(View v) {
showForecast();
}
};
public void UpdateDisplay(){
WeatherAdapter weatheradapter = new WeatherAdapter(this, R.layout.weather_item, weatherlist);
setListAdapter(weatheradapter);
}
private class JsonParser extends AsyncTask<String, String, JSONObject>{
final String TAG = "JsonParser.java";
InputStream is = null;
JSONObject jObj = null;
String json = "";
HttpURLConnection UrlConnection = null;
List<WeatherModel> weatherList;
// make HTTP request
@Override
protected JSONObject doInBackground(String...params) {
try {
URL Url = new URL(params[0]);
UrlConnection = (HttpURLConnection) Url.openConnection();
UrlConnection.connect();
is = UrlConnection.getInputStream();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e(TAG, "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e(TAG, "Error parsing data " + e.toString());
}
// return JSON String
Log.d("Confirmed", "Got JSON");
return jObj;
}
@Override
protected void onPostExecute(JSONObject o) {
super.onPostExecute(o);
Log.d("Json:", o.toString());
weatherList = WeatherParser.ParseFeed(o);
UpdateDisplay();
}
}
}
meine benutzerdefinierte Adapterklasse Hier
public class WeatherAdapter extends ArrayAdapter<WeatherModel> {
private Context context;
private List<WeatherModel> weatherlist;
public WeatherAdapter(Context context, int resource, List<WeatherModel> objects){
super(context, resource, objects);
this.context = context;
this.weatherlist = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.weather_item, parent, false);
WeatherModel weather = weatherlist.get(position);
TextView day = (TextView) view.findViewById(R.id.day);
TextView temperature = (TextView) view.findViewById(R.id.temperature);
TextView description = (TextView) view.findViewById(R.id.description);
day.setText(weather.getDate());
temperature.setText(weather.getTemperature());
description.setText(weather.getDescription());
return view;
}
}
Hier ist meine logcat
06-15 16: 07: 47,912 30.849-30.849/com .beispiel.android.weatherforecast D/Status :: running 06-15 16: 07: 48.233 30849-6794/com.example .android.weatherforecast D/Bestätigt: Bekam JSON 06-15 16: 07: 48.253 30849-30849/com.beispiel.android.weatherforecast D/Json :: {"nachricht": 0.0079, "list": [{ "Wolken": 36, "dt": 1466010000, "Feuchtigkeit": 52, "Druck": 995,17, "Geschwindigkeit": 3,31, "Grad": 150, "Wetter": [{"id": 802, "Symbol ":" 03d "," description ":" verstreut Wolken "," main ":" Wolken "}]," temp ": {" Morgen ": 297,43," min ": 287,68," Nacht ": 287,68," Vorabend ": 294.14," max ": 297.43," Tag ": 297.43}}, {" Wolken ": 0," dt ": 14660964 ...... 06-15 16: 07: 48.253 30849-30849/com.example.android.weatherforecast D/Bestätigt: JSON geparst 06-15 16: 07: 48.273 30849-30849/com.beispiel.android.weatherforecast D/AndroidRuntime: Herunterfahren VM 06-15 16: 07: 48.273 30849-30849/com.beispiel.android.weatherforecast W/dalvikvm : threadid = 1: 07: 30.849 bis 30.849 48,293/com.example.android.weatherforecast E/Android Runtime: schwerwiegendes EXCEPTION: Hauptprozeß: com Gewinde mit abgefangene Ausnahme (group = 0x4181ada0) 06-15 16 austritt. example.android.weatherforecast, PID: 30849 java.lang.NullPointerException bei android.widget.ArrayAdapter.getCount (ArrayAdapter.java:330) bei android.widget.ListView.setAdapter (ListView.java:486) bei android .app.ListActivity.setListAdapter (ListActivity.java:265) bei com.example.android.weatherforecast.MainActivity.UpdateDisplay (MainActivity.java:89) bei com.example.android.weatherforecast.MainActivity $ JsonParser.onPostExecute (HauptAktivität.java:156) bei com.example.android.weatherforecast.MainActivity $ JsonParser.onPostExecute (MainActivity.java:93) bei android.os.AsyncTask.finish (AsyncTask.java:632) bei android.os.AsyncTask.access $ 600 (AsyncTask.java:177) um android.os.AsyncTask $ InternalHandler.handleMessage (AsyncTask.java:645) bei android.os.Handler.dispatchMessage (Handler.java:102) um android.os.Looper.loop (Looper.java:157) um android.app.ActivityThread.main (ActivityThread.java:5356) um java.lang.reflect.Method.invokeNative (Native Methode) um java.lang.reflect.Method.invoke (Methode .java: 515) um com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:1265) bei com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1081) bei dalvik.system.NativeStart.main (native Methode)
Danke:
Ihre Methode
UpdateDisplay
so sein könnte! Das hat für mich funktioniert !!! Schätze es wirklich. – majestyc54