Java 为什么我从AsyncTask获取NullPointerException?
我正在尝试为天气创建一个Android应用程序。在一些失败的结果之后,我决定尝试一个来自互联网的代码。它仍然不起作用。 我在这里找到的: 我得到的错误是: 我已经用我实际的API密钥浏览器检查了url,它可以正常工作 主要活动:Java 为什么我从AsyncTask获取NullPointerException?,java,android,nullpointerexception,Java,Android,Nullpointerexception,我正在尝试为天气创建一个Android应用程序。在一些失败的结果之后,我决定尝试一个来自互联网的代码。它仍然不起作用。 我在这里找到的: 我得到的错误是: 我已经用我实际的API密钥浏览器检查了url,它可以正常工作 主要活动: public class MainActivity extends AppCompatActivity { TextView selectCity, cityField, detailsField, currentTemperatureField, humidi
public class MainActivity extends AppCompatActivity {
TextView selectCity, cityField, detailsField, currentTemperatureField, humidity_field, pressure_field, weatherIcon, updatedField;
ProgressBar loader;
Typeface weatherFont;
String city = "Rome, IT";
/* Please Put your API KEY here */
String OPEN_WEATHER_MAP_API = "f2b6e17d5a21b6580934286ac8fa696a";
/* Please Put your API KEY here */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loader = (ProgressBar) findViewById(R.id.loader);
selectCity = (TextView) findViewById(R.id.selectCity);
cityField = (TextView) findViewById(R.id.city_field);
updatedField = (TextView) findViewById(R.id.updated_field);
detailsField = (TextView) findViewById(R.id.details_field);
currentTemperatureField = (TextView) findViewById(R.id.current_temperature_field);
humidity_field = (TextView) findViewById(R.id.humidity_field);
pressure_field = (TextView) findViewById(R.id.pressure_field);
weatherIcon = (TextView) findViewById(R.id.weather_icon);
weatherFont = Typeface.createFromAsset(getAssets(), "fonts/weathericons-regular-webfont.ttf");
weatherIcon.setTypeface(weatherFont);
taskLoadUp(city);
selectCity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
alertDialog.setTitle("Change City");
final EditText input = new EditText(MainActivity.this);
input.setText(city);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
input.setLayoutParams(lp);
alertDialog.setView(input);
alertDialog.setPositiveButton("Change",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
city = input.getText().toString();
taskLoadUp(city);
}
});
alertDialog.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
});
}
public void taskLoadUp(String query) {
if (Function.isNetworkAvailable(getApplicationContext())) {
Log.w("myApp", "network available in main 91");
DownloadWeather task = new DownloadWeather();
task.execute(query);
} else {
Toast.makeText(getApplicationContext(), "No Internet Connection", Toast.LENGTH_LONG).show();
}
}
class DownloadWeather extends AsyncTask < String, Void, String > {
@Override
protected void onPreExecute() {
super.onPreExecute();
loader.setVisibility(View.VISIBLE);
}
protected String doInBackground(String...args) {
String xml = Function.excuteGet("http://api.openweathermap.org/data/2.5/weather?q=" + args[0] +
"&units=metric&appid=" + OPEN_WEATHER_MAP_API);
Log.w("myApp", "xml is " +xml);
return xml;
}
@Override
protected void onPostExecute(String xml) {
try {
JSONObject json = new JSONObject(xml);
if (json != null) {
JSONObject details = json.getJSONArray("weather").getJSONObject(0);
JSONObject main = json.getJSONObject("main");
DateFormat df = DateFormat.getDateTimeInstance();
cityField.setText(json.getString("name").toUpperCase(Locale.US) + ", " + json.getJSONObject("sys").getString("country"));
detailsField.setText(details.getString("description").toUpperCase(Locale.US));
currentTemperatureField.setText(String.format("%.2f", main.getDouble("temp")) + "°");
humidity_field.setText("Humidity: " + main.getString("humidity") + "%");
pressure_field.setText("Pressure: " + main.getString("pressure") + " hPa");
updatedField.setText(df.format(new Date(json.getLong("dt") * 1000)));
weatherIcon.setText(Html.fromHtml(Function.setWeatherIcon(details.getInt("id"),
json.getJSONObject("sys").getLong("sunrise") * 1000,
json.getJSONObject("sys").getLong("sunset") * 1000)));
loader.setVisibility(View.GONE);
}else{
Toast.makeText(getApplicationContext(), "Json variable is null", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "Error, Check City", Toast.LENGTH_SHORT).show();
}
}
}
}
public类MainActivity扩展了AppCompatActivity{
text查看选择城市、城市字段、详细信息字段、当前温度字段、湿度字段、压力字段、天气图标、更新字段;
钢筋装载机;
字体;
String city=“罗马,IT”;
/*请把你的API密钥放在这里*/
字符串打开天气图API=“f2b6e17d5a21b6580934286ac8fa696a”;
/*请把你的API密钥放在这里*/
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loader=(ProgressBar)findviewbyd(R.id.loader);
selectCity=(文本视图)findViewById(R.id.selectCity);
cityField=(TextView)findViewById(R.id.city_字段);
updatedField=(TextView)findViewById(R.id.updated_字段);
detailsField=(TextView)findViewById(R.id.details\u字段);
currentTemperatureField=(TextView)findViewById(R.id.当前温度场);
湿度\字段=(文本视图)findViewById(R.id.湿度\字段);
压力_字段=(文本视图)findViewById(R.id.压力_字段);
weatherIcon=(TextView)findViewById(R.id.weatherIcon);
weatherFont=Typeface.createFromAsset(getAssets(),“fonts/weathericons常规webfont.ttf”);
weatherIcon.setTypeface(weatherFont);
任务加载(城市);
选择city.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
AlertDialog.Builder AlertDialog=新建AlertDialog.Builder(MainActivity.this);
alertDialog.setTitle(“更改城市”);
最终编辑文本输入=新编辑文本(MainActivity.this);
input.setText(城市);
LinearLayout.LayoutParams lp=新的LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_父级,
LinearLayout.LayoutParams.MATCH_PARENT);
input.setLayoutParams(lp);
alertDialog.setView(输入);
alertDialog.setPositiveButton(“更改”,
新建DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which){
city=input.getText().toString();
任务加载(城市);
}
});
alertDialog.setNegativeButton(“取消”,
新建DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which){
dialog.cancel();
}
});
alertDialog.show();
}
});
}
公共void任务加载(字符串查询){
if(Function.isNetworkAvailable(getApplicationContext())){
Log.w(“myApp”,“main 91中提供的网络”);
DownloadWeather任务=新建DownloadWeather();
任务执行(查询);
}否则{
Toast.makeText(getApplicationContext(),“无Internet连接”,Toast.LENGTH_LONG.show();
}
}
类DownloadWeather扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
loader.setVisibility(View.VISIBLE);
}
受保护的字符串doInBackground(字符串…args){
字符串xml=Function.excuteGet(“http://api.openweathermap.org/data/2.5/weather?q=“+args[0]+
“&units=metric&appid=“+OPEN\u WEATHER\u MAP\u API”);
w(“myApp”,“xml是”+xml);
返回xml;
}
@凌驾
受保护的void onPostExecute(字符串xml){
试一试{
JSONObject json=新的JSONObject(xml);
if(json!=null){
JSONObject details=json.getJSONArray(“天气”).getJSONObject(0);
JSONObject main=json.getJSONObject(“main”);
DateFormat df=DateFormat.getDateTimeInstance();
cityField.setText(json.getString(“name”).toUpperCase(Locale.US)+,“+json.getJSONObject(“sys”).getString(“country”);
detailsField.setText(details.getString(“description”).toUpperCase(Locale.US));
currentTemperatureField.setText(String.format(“%.2f”,main.getDouble(“temp”))+“°”;
湿度_field.setText(“湿度:+main.getString(“湿度”)+“%”);
压力_field.setText(“压力:”+main.getString(“压力”)+“hPa”);
updatefield.setText(df.format(新日期(json.getLong(“dt”)*1000));
weatherIcon.setText(Html.fromHtml(Function.setWeatherIcon(details.getInt(“id”)),
json.getJSONObject(“sys”).getLong(“日出”)*1000,
json.getJSONObject(“sys”).getLong(“日落”)*1000);
loader.setVisibility(View.GONE);
}否则{
Toast.makeText(getApplicationContext(),“Json变量为空”,Toast.LENGTH_SHORT.show();
}
}捕获(JSONException e){
Toast.makeText(getApplicationContext(),“错误,检查城市”,Toast.LENGTH\u SHORT.show();
}
}
}
}
功能:
public class Function {
// Project Created by Ferdousur Rahman Shajib
// www.androstock.com
public static boolean isNetworkAvailable(Context context)
{
return ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo() != null;
}
public static String excuteGet(String targetURL)
{
URL url;
HttpURLConnection connection = null;
try {
//Create connection
url = new URL(targetURL);
connection = (HttpURLConnection)url.openConnection();
connection.setRequestProperty("content-type", "application/json; charset=utf-8");
connection.setRequestProperty("Content-Language", "en-US");
connection.setUseCaches (false);
connection.setDoInput(true);
connection.setDoOutput(false);
InputStream is;
int status = connection.getResponseCode();
if (status != HttpURLConnection.HTTP_OK)
is = connection.getErrorStream();
else
is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
return response.toString();
} catch (Exception e) {
return null;
} finally {
if(connection != null) {
connection.disconnect();
}
}
}
public static String setWeatherIcon(int actualId, long sunrise, long sunset){
int id = actualId / 100;
String icon = "";
if(actualId == 800){
long currentTime = new Date().getTime();
if(currentTime>=sunrise && currentTime<sunset) {
icon = "";
} else {
icon = "";
}
} else {
switch(id) {
case 2 : icon = "";
break;
case 3 : icon = "";
break;
case 7 : icon = "";
break;
case 8 : icon = "";
break;
case 6 : icon = "";
break;
case 5 : icon = "";
break;
}
}
return icon;
}
}
公共类函数{
//由Ferdousur Rahman Shajib创建的项目
//www.androstock.com
公共静态布尔值isNetworkAvailable(上下文)
{
返回((连接)
FATAL EXCEPTION: main
Process: com.example.weatherapp, PID: 30032
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
at org.json.JSONTokener.nextValue(JSONTokener.java:94)
at org.json.JSONObject.<init>(JSONObject.java:159)
at org.json.JSONObject.<init>(JSONObject.java:176)
at com.example.weatherapp.MainActivity$DownloadWeather.onPostExecute(MainActivity.java:117)
at com.example.weatherapp.MainActivity$DownloadWeather.onPostExecute(MainActivity.java:101)
at android.os.AsyncTask.finish(AsyncTask.java:695)
at android.os.AsyncTask.access$600(AsyncTask.java:180)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:712)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2019-02-09 18:27:45.489 30032-30032/com.example.weatherapp I/Process: Sending signal. PID: 30032 SIG: 9
?xml version="1.0" encoding="utf-8"?>
<manifest ...>
<uses-permission android:name="android.permission.INTERNET" />
<application
...
android:usesCleartextTraffic="true"
...>
...
</application>
</manifest>