这是一个简单的应用程序,它使用okkhttp从forecast.io获取预测 只有当我声明文本视图并尝试将其文本设置为温度时,才会出现此问题。 这是活动:
public class MainActivity extends Activity {
private static String TAG = MainActivity.class.getSimpleName();
TextView temperatureTextView =(TextView) findViewById(R.id.temperatureTextView);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//https://api.forecast.io/forecast/APIKEY/LATITUDE,LONGITUDE,TIME
String apiKey = "29cfca9514567f6ac466b782adea8a2d";
double latitude =37.8267;
double longitude = -122.423;
if (isNetworkAvailable()) {
OkHttpClient client = new OkHttpClient();
String forecastUrl = "https://api.forecast.io/forecast/" + apiKey + "/" + latitude + "," + longitude;
Request request = new Request.Builder()
.url(forecastUrl)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(Response response) throws IOException {
try {
final String jsonData = response.body().string();
Log.v(TAG, jsonData);
if (response.isSuccessful()) {
Log.v(TAG , "request successful");
final CurrentWeather currentWeather = currentWeather(jsonData);
runOnUiThread(new Runnable() {
@Override
public void run() {
temperatureTextView.setText(currentWeather.getTemperature()+"");
}
});
} else {
alertDialog("Aw, Snap!","There was a problem with the request. Please try again later.");
}
} catch (IOException e) {
Log.e(TAG, "Exception caught");
}
}
});
}
else {
alertDialog("Aw, Snap!","Please make sure you are connected to the internet.");
}
}
这是清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ironman.stormy" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是日志:
04-27 02:41:48.376 24123-24123/com.example.ironman.stormy E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.ironman.stormy/com.example.ironman.stormy.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
at android.app.ActivityThread.access$600(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
at android.os.Handler.dispatchMessage(Handler.java)
at android.os.Looper.loop(Looper.java)
at android.app.ActivityThread.main(ActivityThread.java)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.app.Activity.findViewById(Activity.java)
at com.example.ironman.stormy.MainActivity.<init>(MainActivity.java:36)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.app.Instrumentation.newActivity(Instrumentation.java)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
at android.app.ActivityThread.access$600(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
at android.os.Handler.dispatchMessage(Handler.java)
at android.os.Looper.loop(Looper.java)
at android.app.ActivityThread.main(ActivityThread.java)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
在findViewById
之后调用setContentView
才有意义。移动电话。
(另请注意,如果在您旋转屏幕后HTTP响应到达,则文本字段不会更新。但这是另一个故事。)