有个在天气预报的问题,老是出错,要求只要在模拟器里面显示网上时时更新的预报图片即可。
import java.io.*;
import java.util.*;
import javax.microedition.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;public class Weather extends MIDlet implements CommandListener, Runnable { private Display mDisplay;
private Command mExitCommand;
private Command mForecastCommand;
private Image mImage1;
private Image mImage2;
private string mUrl1;
private string mUrl2;
mUrl1 = "http://www.weatherzone.com.au/wzservlet/wzwoys/button?postcode=2615&locality=FLOREY&type=0"; //weather image
mUrl2 = "http://www.weatherzone.com.au/wzservlet/wzwoys/button?postcode=2615&locality=FLOREY&type=1"; //forecast image private Form mForm; public Weather() {
mForm = new Form("Weather");
mImage1 = loadImage(mUrl1);
mImage2 = loadImage(mUrl2); mExitCommand = new Command("Exit", Command.EXIT, 0);
mWeatherCommand = new Command("Weather", Command.SCREEN, 0);
mForecastCommand = new Command("Forecast", Command.SCREEN, 0);
mForm.addCommand(mExitCommand);
mFrom.addCommand(mWeatherCommand);
mForm.addCommand(mForecastCommand);
mForm.setCommandListener(this);
} public void startApp() {
mDisplay = Display.getDisplay(this);
mForm.append(mImage1);
mForm.append(mImage2);
mDisplay.setCurrent(mForm);
} public void pauseApp() {} public void destroyApp(boolean unconditional) {} public void commandAction(Command c, Displayable s) {
if (c == mExitCommand) {
destroyApp(false);
notifyDestroyed();
} if (c == mWeatherCommand)
{
Alert mAlert = new Alert("Alert", "Weather clicked", mImage1, AlertType.INFO);
mAlert.setTimeout(2000);
mAlert.setImage(mImage1);
mDisplay.setCurrent(mAlert, mForm);
} if (c == mForecastCommand) {
Alert mAlert = new Alert("Alert", "Forecast clicked", mImage2, AlertType.INFO);
mAlert.setTimeout(2000);
mAlert.setImage(mImage2);
mDisplay.setCurrent(mAlert, mForm);
}
} public void run() {} private Image loadImage(URL url) {
Image image = null;
try {
image = Image.createImage(url);
}
catch (IOException ioe) {
System.out.println(ioe);
}
return image;
}
}这是错误提示。。请问该怎么改?
java.io.IOException
java.io.IOException
startApp threw an Exception
java.lang.NullPointerException
java.lang.NullPointerException
at javax.microedition.lcdui.Form.append(+11)
at Weather.startApp(+19)
at javax.microedition.midlet.MIDletProxy.startApp(+7)
at com.sun.midp.midlet.Scheduler.schedule(+270)
at com.sun.midp.main.Main.runLocalClass(+28)
at com.sun.midp.main.Main.main(+116)
import java.io.*;
import java.util.*;
import javax.microedition.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;public class Weather extends MIDlet implements CommandListener, Runnable { private Display mDisplay;
private Command mExitCommand;
private Command mForecastCommand;
private Image mImage1;
private Image mImage2;
private string mUrl1;
private string mUrl2;
mUrl1 = "http://www.weatherzone.com.au/wzservlet/wzwoys/button?postcode=2615&locality=FLOREY&type=0"; //weather image
mUrl2 = "http://www.weatherzone.com.au/wzservlet/wzwoys/button?postcode=2615&locality=FLOREY&type=1"; //forecast image private Form mForm; public Weather() {
mForm = new Form("Weather");
mImage1 = loadImage(mUrl1);
mImage2 = loadImage(mUrl2); mExitCommand = new Command("Exit", Command.EXIT, 0);
mWeatherCommand = new Command("Weather", Command.SCREEN, 0);
mForecastCommand = new Command("Forecast", Command.SCREEN, 0);
mForm.addCommand(mExitCommand);
mFrom.addCommand(mWeatherCommand);
mForm.addCommand(mForecastCommand);
mForm.setCommandListener(this);
} public void startApp() {
mDisplay = Display.getDisplay(this);
mForm.append(mImage1);
mForm.append(mImage2);
mDisplay.setCurrent(mForm);
} public void pauseApp() {} public void destroyApp(boolean unconditional) {} public void commandAction(Command c, Displayable s) {
if (c == mExitCommand) {
destroyApp(false);
notifyDestroyed();
} if (c == mWeatherCommand)
{
Alert mAlert = new Alert("Alert", "Weather clicked", mImage1, AlertType.INFO);
mAlert.setTimeout(2000);
mAlert.setImage(mImage1);
mDisplay.setCurrent(mAlert, mForm);
} if (c == mForecastCommand) {
Alert mAlert = new Alert("Alert", "Forecast clicked", mImage2, AlertType.INFO);
mAlert.setTimeout(2000);
mAlert.setImage(mImage2);
mDisplay.setCurrent(mAlert, mForm);
}
} public void run() {} private Image loadImage(URL url) {
Image image = null;
try {
image = Image.createImage(url);
}
catch (IOException ioe) {
System.out.println(ioe);
}
return image;
}
}这是错误提示。。请问该怎么改?
java.io.IOException
java.io.IOException
startApp threw an Exception
java.lang.NullPointerException
java.lang.NullPointerException
at javax.microedition.lcdui.Form.append(+11)
at Weather.startApp(+19)
at javax.microedition.midlet.MIDletProxy.startApp(+7)
at com.sun.midp.midlet.Scheduler.schedule(+270)
at com.sun.midp.main.Main.runLocalClass(+28)
at com.sun.midp.main.Main.main(+116)
mDisplay = Display.getDisplay(this);
mForm.append(mImage1);
mForm.append(mImage2);
mDisplay.setCurrent(mForm);
}看下你得到的mDisplay是撒东西?
你的mForm么有初始化吧?
没猜错应该是你的mForm的问题。你可以看下。
private string mUrl2;
类型都错了
注意大小写先
这个mWeatherCommand 哪里来的?
mForm.append(mImage1);
mForm.append(mImage2);
这3句别放在startApp里,应该放构造函数里,startApp里只留mDisplay.setCurrent(mForm);
你怎么把j2me的程序发这来了
mForm = new Form("Weather");//你new对象是在这个方法内new的。
mImage1 = loadImage(mUrl1);
mImage2 = loadImage(mUrl2); mExitCommand = new Command("Exit", Command.EXIT, 0);
mWeatherCommand = new Command("Weather", Command.SCREEN, 0);
mForecastCommand = new Command("Forecast", Command.SCREEN, 0);
mForm.addCommand(mExitCommand);
mFrom.addCommand(mWeatherCommand);
mForm.addCommand(mForecastCommand);
mForm.setCommandListener(this);
} public void startApp() {
mDisplay = Display.getDisplay(this);
mForm.append(mImage1);//你在这里可以用吗?不行的!变量的作用域问题
mForm.append(mImage2);
mDisplay.setCurrent(mForm);
}
搞错没有?忽忽~~那我不懂语法是不是一样了~~~