【问题标题】:Set method return type to 'void'. Dont quite understand将方法返回类型设置为“void”。不太明白
【发布时间】:2013-11-08 17:27:12
【问题描述】:

做一个基本的android后台服务应用。 不太明白为什么会报错(MainActivity.java). Error is at btnStart = (Button)findViewById(R.id.btnStart);

提供的快速修复将返回类型设置为“void”。而对于 btnStop 则没有错误。

package com.example.backgroundservice;
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.app.Service;

public class MainActivity extends Activity implements OnClickListener{




    btnStart = (Button)findViewById(R.id.btnStart); (ERROR HERE)
    btnStop = (Button) findViewById(R.id.btnStop);

btnStart.setonClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        Intent serviceIntent = new Intent(MainActivity.this,MyService.class);
        startService(serviceIntent);
    }
});

btnStop.setonClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        Intent serviceIntent = new Intent (MainActivity.this,MyService.class);
        stopService(serviceIntent);
    }
});

}

@Override
public void onClick(DialogInterface dialog, int which) {
    // TODO Auto-generated method stub
}
}

【问题讨论】:

  • 它在onCreate 里面吗?错误是什么?

标签: java android eclipse background void


【解决方案1】:

您的代码应该在onCreate 方法中。

在调用该方法之前,您的活动没有初始化,因此没有布局,并且您无法通过 id 找到任何 UI 元素。

编辑:类似的东西会起作用:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout);  // this layout must contain btnStart and btnStop
    Button btnStart = (Button) findViewById(R.id.btnStart); // variables are declared then allocated
    Button btnStop = (Button) findViewById(R.id.btnStop)
    // ...<rest of your code>....

【讨论】:

  • 如果在尝试分配 btnStart 之前没有声明它会出现编译时错误...
  • 我同意,但这不是你写的
  • 尝试:Button btnStart = (Button)findViewById(R.id.btnStart);
  • 实际上他会拥有,而不是我
  • 如果初始化失败你会得到NullPointerException
【解决方案2】:

应该在onCreate里面。

Button btnStart,btnStop; // should be delcared. i guess you do not have this
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
            setContentView(R.layout.yourlayout);
            btnStart = (Button)findViewById(R.id.btnStart); // initialize here
            btnStop = (Button) findViewById(R.id.btnStop)
            ...// rest of the code

我猜你在onCreate 之外有以下内容(在任何方法之外)并且你还没有声明btnStart

      btnStart = (Button)findViewById(R.id.btnStart);

但是即使你声明你需要先膨胀布局然后初始化按钮,否则你会得到NUllPointerException

所以将按钮声明为类成员并在onCreate中初始化,如上所示

编辑:

由于您已经拥有监听器匿名内部类,因此您无需实现OnClickListener,因此您可以删除它

@Override
public void onClick(DialogInterface dialog, int which) {
    // TODO Auto-generated method stub
}

你的导入语句也是错误的

替换这个

   import android.content.DialogInterface.OnClickListener;

作者

   import android.view.View.OnClickListener;

【讨论】:

  • 注意实现的接口不是View.OnClickListener。但是,是的,看起来像一个初学者代码,并且可能他只是错误地导入了 DialogInterface.OnClickListener 而不是 View.OnClickListener
【解决方案3】:

你必须重写activity的onCreate方法并设置它们的内容:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.theLayoutThatContains_R.id.btnStart);
}

【讨论】:

    【解决方案4】:

    您收到错误是因为您在类体内有属于方法体的代码。 Eclipse 注意到语法错误并建议通过将代码更改为方法声明来“修复”它。您不会再遇到任何语法错误,因为编译在第一个语法错误处停止。

    正如其他人所指示的,此类代码的正确位置是活动的onCreate() 方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-15
      • 2023-03-15
      • 1970-01-01
      • 2012-03-19
      相关资源
      最近更新 更多