【问题标题】:Syntax error in Android serviceAndroid 服务中的语法错误
【发布时间】:2023-03-18 01:00:01
【问题描述】:

我使用此代码连接到我的服务,但是当我输入此行时

this.bindService(service, conn, flags);

我收到错误消息:syntax error on token "}", { expected after this token ... (1)

这是我班级的所有代码:

package com.flaxa.fixx;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;


public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void butonRunOnlyClickHandler(View target) {

        Intent activity = new Intent(this, com.flax.trainer.RunOnly.class);
        startActivity(activity);                
    }

    public void butonChallengeClickHandler(View target) {


    }

    public void butonProgramClickHandler(View target) {



    }

    public void butonCalculatorClickHandler(View target) {


    }


    ServiceConnection conn = new ServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName name) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            // TODO Auto-generated method stub

        }

        this.bindService(new Intent("") , conn, Context.BIND_AUTO_CREATE);

    }; 

}

有人可以帮助我使用 bindService 功能吗?我错了?

谢谢

【问题讨论】:

    标签: java android syntax


    【解决方案1】:

    我怀疑是这条线:

    this.bindService(new Intent("...") , conn, Context.BIND_AUTO_CREATE);
    

    您将此语句放入您的 ServiceConnection 声明中,但不在其中 一个方法。假设您正在使用来自ActivityServiceConnection,您应该移动 将此声明发送给您的onCreateonResume

    【讨论】:

    • 感谢 Erich Douglass 我这一行 this.bindService(new Intent("...") , conn, Context.BIND_AUTO_CREATE);在我的活动上 onCreate 再次感谢
    • 没问题!如果答案有帮助,请点击左侧的复选框将其标记为已接受答案。
    【解决方案2】:

    对我来说,您似乎正在尝试在任何方法之外调用方法。所以它确实是无效的,在我们的例子中,你必须将它移动到某个方法的主体中。

    【讨论】:

      猜你喜欢
      • 2014-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-23
      • 2016-09-22
      • 2015-12-21
      • 2016-08-03
      相关资源
      最近更新 更多