当我尝试运行它时,模拟器崩溃了,我得到了这个例外
方法调用可能会产生java.lang.nullpointerexception
这是我的代码,我知道错误是在最后一行,但我不知道如何解决它
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import java.net.MalformedURLException;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
// FIXME: Experimenting
try {
ReadingFactory parser = new BOMFactory();
WeatherService s = new WeatherService(
"BOM", "http://www.bom.gov.au/fwo/IDV60801/IDV60801.94839.json", parser
);
s.run();
// Thread test = new Thread(s);
// test.start();
} catch (MalformedURLException e) {
e.printStackTrace();
}
BOMResponse bomResponse = new BOMResponse();
TextView temperature = (TextView)findViewById(R.id.temperature);
temperature.setText(String.format("%d", bomResponse.getObservations().getData().get(0).getAirTemp().toString()));
}
}
这是我完整的Stacktrace
FATAL EXCEPTION: main
Process: com.example.teamredrmit.weatherapp, PID: 1792
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.teamredrmit.weatherapp/com.example.teamredrmit.weathe
app.MainActivity}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.teamredrmit.weatherapp.MainActivity.onCreate(MainActivity.java:41)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
BOMResponse
这里只是初始化bomResponse.getObservations()
对象。您没有在自定义对象中设置值,因此null
将为Arapiraca A. José da Silva Manoel Teles 57305-075 Rua
Maceió Abelardo Pugliese Jatiúca 57036-020 Rua
。你必须先设定值然后再使用它。