我已经在这里和其他地方研究过这个错误,但原因似乎很多样化。我是新手,并整理了一个原始的天气应用程序进行练习。输入一个城市和州,它会输出到 Weatherunderground,提取 XML 提要,解析它并显示一些数据点。它是线程化的并且工作正常。今天我添加了一个意图来打开一个新的第二个屏幕并在那里显示数据。碰撞。不知道为什么。这是错误日志。我在日志中搜索了一些术语也没有成功。
08-14 14:03:27.314: E/AndroidRuntime(950): FATAL EXCEPTION: main
08-14 14:03:27.314: E/AndroidRuntime(950): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.glenngilchrist.weatherapp/com.glenngilchrist.weatherapp.WeatherMainActivity}: java.lang.NullPointerException
08-14 14:03:27.314: E/AndroidRuntime(950): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
08-14 14:03:27.314: E/AndroidRuntime(950): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
08-14 14:03:27.314: E/AndroidRuntime(950): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-14 14:03:27.314: E/AndroidRuntime(950): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
08-14 14:03:27.314: E/AndroidRuntime(950): at android.os.Handler.dispatchMessage(Handler.java:99)
08-14 14:03:27.314: E/AndroidRuntime(950): at android.os.Looper.loop(Looper.java:137)
08-14 14:03:27.314: E/AndroidRuntime(950): at android.app.ActivityThread.main(ActivityThread.java:5103)
08-14 14:03:27.314: E/AndroidRuntime(950): at java.lang.reflect.Method.invokeNative(Native Method)
08-14 14:03:27.314: E/AndroidRuntime(950): at java.lang.reflect.Method.invoke(Method.java:525)
08-14 14:03:27.314: E/AndroidRuntime(950): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-14 14:03:27.314: E/AndroidRuntime(950): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-14 14:03:27.314: E/AndroidRuntime(950): at dalvik.system.NativeStart.main(Native Method)
08-14 14:03:27.314: E/AndroidRuntime(950): Caused by: java.lang.NullPointerException
08-14 14:03:27.314: E/AndroidRuntime(950): at android.content.ContextWrapper.getPackageName(ContextWrapper.java:135)
08-14 14:03:27.314: E/AndroidRuntime(950): at android.content.ComponentName.<init>(ComponentName.java:75)
08-14 14:03:27.314: E/AndroidRuntime(950): at android.content.Intent.<init>(Intent.java:3662)
08-14 14:03:27.314: E/AndroidRuntime(950): at com.glenngilchrist.weatherapp.WeatherMainActivity.<init>(WeatherMainActivity.java:31)
08-14 14:03:27.314: E/AndroidRuntime(950): at java.lang.Class.newInstanceImpl(Native Method)
08-14 14:03:27.314: E/AndroidRuntime(950): at java.lang.Class.newInstance(Class.java:1130)
08-14 14:03:27.314: E/AndroidRuntime(950): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
08-14 14:03:27.314: E/AndroidRuntime(950): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
08-14 14:03:27.314: E/AndroidRuntime(950): ... 11 more
这是一些代码:
public class WeatherMainActivity extends Activity implements OnClickListener {
String WunderURL = "http://api.wunderground.com/api/1d4c3256ea6ea9fa/conditions/q/";
TextView tv; // text box for retrieved data
EditText city, state; // data entered by user
Intent i = new Intent(this, WeatherDetailDisplay.class);
//StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
tv = (TextView)findViewById(R.id.results);
Bundle bundle = msg.getData();
String string = bundle.getString("myKey");
if (string.equals("Valid")) {
// go to detail screen
String strcity = bundle.getString("myCity");
String strstate = bundle.getString("myState");
String strtempF = bundle.getString("myTempF");
i.putExtra("myCity", strcity);
i.putExtra("myState", strstate);
i.putExtra("myTempF", strtempF);
startActivity(i);
} else {
tv.setText(string);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_weather_main);
//Button b = (Button)findViewById(R.id.button1);
tv = (TextView)findViewById(R.id.results);
city = (EditText)findViewById(R.id.city);
state = (EditText)findViewById(R.id.state);
city.setText("Burlington");
state.setText("VT");
//StrictMode.setThreadPolicy(policy);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.weather_main, menu);
return true;
}
@Override
public void onClick(View view) {
// run this is a different thread
Runnable runnable = new Runnable() {
public void run() {
// build a URl and go out to the web for data
String c = city.getText().toString();
String s = state.getText().toString();
StringBuilder url = new StringBuilder(WunderURL);
url.append(s+"/"+c+".xml");
String fullUrl = url.toString();
Message msg = handler.obtainMessage();
Bundle bundle = new Bundle();
try{
URL weatherURL = new URL(fullUrl);
// set up an xmlreader to parse the data
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xreader = sp.getXMLReader();
// tell the reader to user our handler class
XMLHandler xh = new XMLHandler();
xreader.setContentHandler(xh);
// now open the stream
xreader.parse(new InputSource(weatherURL.openStream()));
//String text = xh.GetInfoString();
String city = xh.getCity();
String state = xh.getState();
String tempF = xh.getTemp();
if (city.contains("null")) {
//tv.setText("Invalid Location");
bundle.putString("myKey", "Invalid Location");
bundle.putString("myCity", "");
bundle.putString("myState", "");
bundle.putString("myTempF", "");
msg.setData(bundle);
handler.sendMessage(msg);
} else {
//tv.setText(text);
bundle.putString("myKey", "Valid");
bundle.putString("myCity", city);
bundle.putString("myState", state);
bundle.putString("myTempF", tempF);
msg.setData(bundle);
handler.sendMessage(msg);
}
} catch (Exception e) {
//tv.setText("error");
bundle.putString("myKey", "error");
bundle.putString("myCity", "");
bundle.putString("myState", "");
bundle.putString("myTempF", "");
msg.setData(bundle);
handler.sendMessage(msg);
}
}
};
Thread mythread = new Thread(runnable);
mythread.start();
}
}