Java Android spinner抛出空指针异常
所以我有一个包含计时器的服务。该服务意味着即使应用程序在后台运行,也要在计时器从另一个活动执行方法时将用户拉到应用程序。该服务由切换按钮触发打开和关闭。目前,我对微调器的R.id引用不断抛出一个我不知道如何修复的NullPointerException。有人能帮我吗 计时器正在运行的方法:Java Android spinner抛出空指针异常,java,android,nullpointerexception,Java,Android,Nullpointerexception,所以我有一个包含计时器的服务。该服务意味着即使应用程序在后台运行,也要在计时器从另一个活动执行方法时将用户拉到应用程序。该服务由切换按钮触发打开和关闭。目前,我对微调器的R.id引用不断抛出一个我不知道如何修复的NullPointerException。有人能帮我吗 计时器正在运行的方法: public String TemperatureCatch() { /*this line throws the error */ Spinner reefercho
public String TemperatureCatch()
{
/*this line throws the error */ Spinner reeferchoice = (Spinner)findViewById(R.id.optionselecti);
String reeferChoicei = reeferchoice.getSelectedItem().toString();
if (reeferChoicei.equals("Yes")) {
final ToneGenerator tg = new ToneGenerator(AudioManager.STREAM_NOTIFICATION, 500);
tg.startTone(ToneGenerator.TONE_CDMA_ABBR_ALERT);
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Temperature");
alert.setMessage("Input Temperature in F° (-20 to 65) ");
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_PHONE);
alert.setView(input);
alert.setPositiveButton("Check-In", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
temperaturei = input.getText().toString();
Toast.makeText(getBaseContext(), "Updated", Toast.LENGTH_SHORT).show();
Updater(temperaturei);
}
});
alert.show();
} else if (reeferChoicei.equals("No")) {
temperaturei = "DRY";
Updater(temperaturei);
}
return temperaturei;
}
我的服务代码:
import android.app.IntentService;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import java.util.Timer;
import java.util.TimerTask;
public class MyService extends Service {
Locator locator;
Timer myTimer;
@Override
public void onCreate() {
locator = new Locator();
myTimer = new Timer();
}
private class MyTimerTask extends TimerTask
{
@Override
public void run() {
Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(new Runnable() {
@Override
public void run() {
locator.TemperatureCatch();
}
}, 1000);
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
MyTimerTask myTimerTask = new MyTimerTask();
myTimer.scheduleAtFixedRate(myTimerTask, 0, 15000);
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
myTimer.cancel();
}
}
按钮:
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if (isChecked)
{
startService(new Intent(this, MyService.class));
Toast.makeText(this,"Check-In will be done every 15 seconds",Toast.LENGTH_SHORT).show();
}
else
{
stopService(new Intent(this, MyService.class));
Toast.makeText(this,"Manual Check-In enabled",Toast.LENGTH_SHORT).show();
}
}
堆栈跟踪:
Process: com.example.adrian.trucktracker, PID: 9617
java.lang.NullPointerException
at android.app.Activity.findViewById(Activity.java:1952)
at com.example.adrian.trucktracker.Locator.TemperatureCatch(Locator.java:192)
at com.example.adrian.trucktracker.MyService$MyTimerTask$1.run(MyService.java:32)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5872)
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:852)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:668)
at dalvik.system.NativeStart.main(Native Method)
不应创建活动类的实例
locator = new Locator(); // do not do this
这会在初始化视图时导致NullPointerException
另外,您所做的似乎是一个设计问题。正如前面的评论所指出的,服务不会有任何UI,所以您不能进行findViewById方法调用
如果您希望您的服务更改UI,请从活动绑定到此服务,并使用回调/委托,您可以通知活动从服务更改UI。活动对象不应通过调用其构造函数进行初始化。这样做,将永远不会调用重写的onCreate方法,因此视图不会初始化,findViewById将失败 您应该在服务中启动活动,而不是调用其构造函数,如下所示:
Intent t = new Intent (this, Locator.class);
t.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(t);
您可以在Locator类中使用静态getter来获取活动的实例,以便在服务中使用它。第192行和第32行是什么?TemperatureCatch定义在哪里?服务没有ui。那么,您在locator.TemperatureCatch中所做的是什么;此定位器=新定位器;。定位器是活动定位器确实是活动@Raghunandan@bhargavgTemperatureCatch是方法的名称,它是第一组code@AdamPraiswater取决于你想做什么。服务应该做什么?服务应该包含一个计时器,该计时器在名为Locator的活动中执行一个方法。该服务也应该在应用程序处于后台时运行。我已经尝试过这样做,但我找不到适合与服务一起使用的代码,也找不到如何使用警报实现该方法Manager@AdamPraiswater这似乎是一个设计问题。您不希望服务长时间运行,因为它会消耗电池。你需要想一个更好的设计。这是我告诉客户的,但这是他想要的设置方式。可以打开和关闭服务的切换按钮您能举个例子吗?没有比@bhargavg更好的例子了NPE是因为创建活动类的实例会导致空上下文。