我有Flutter应用程序,并在本机Android上使用服务。 现在,当我按下“切换应用程序”按钮停止应用程序时,我的应用程序因以下错误而崩溃:
java.lang.RuntimeException: Unable to stop activity {de.karlw.time_management/de.karlw.time_management.MainActivity}: java.lang.IllegalArgumentException: Service not registered: de.karlw.time_management.MainActivity$1@f404173
E/AndroidRuntime( 8660): at android.app.ActivityThread.callActivityOnStop(ActivityThread.java:4861)
E/AndroidRuntime( 8660): at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:4826)
E/AndroidRuntime( 8660): at android.app.ActivityThread.handleStopActivity(ActivityThread.java:4906)
E/AndroidRuntime( 8660): at android.app.servertransaction.StopActivityItem.execute(StopActivityItem.java:41)
E/AndroidRuntime( 8660): at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
这是我的MainActivity:
public class MainActivity extends FlutterActivity implements MethodChannel.MethodCallHandler {
static final String TAG = "rest";
static final String CHANNEL = "de.karlw.time_management/service";
AppService appService;
boolean serviceConnected = false;
MethodChannel.Result keepResult = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(this::onMethodCall);
}
@Override
public void onMethodCall(MethodCall call, MethodChannel.Result result) {
try {
if (call.method.equals("connect")) {
connectToService();
keepResult = result;
} else if (serviceConnected) {
if (call.method.equals("start")) {
appService.startTimer();
result.success(null);
} else if (call.method.equals("stop")) {
appService.stopTimer();
result.success(null);
} else if (call.method.equals("getCurrentSeconds")) {
int sec = appService.getCurrentSeconds();
result.success(sec);
}
} else {
result.error(null, "App not connected to service", null);
}
} catch (Exception e) {
result.error(null, e.getMessage(), null);
}
}
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className,
IBinder service) {
AppService.AppServiceBinder binder = (AppService.AppServiceBinder) service;
appService = binder.getService();
serviceConnected = true;
Log.i(TAG, "Service connected");
if (keepResult != null) {
keepResult.success(null);
keepResult = null;
}
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
serviceConnected = false;
Log.i(TAG, "Service disconnected");
}
};
private void connectToService() {
if (!serviceConnected) {
Intent service = new Intent(this, AppService.class);
startService(service);
getApplicationContext().bindService(service, connection, Context.BIND_AUTO_CREATE);
} else {
Log.i(TAG, "Service already connected");
if (keepResult != null) {
keepResult.success(null);
keepResult = null;
}
}
}
@Override
protected void onStop() {
super.onStop();
getApplicationContext().unbindService(connection);
serviceConnected = false;
}
}
我不知道为什么会这样。但是我建议错误是我使用的服务。但是我不知道为什么会这样。
感谢您的帮助。