OnClickListener未正确设置

时间:2015-04-06 14:20:08

标签: java android onclicklistener

我正在尝试使用添加的视图使OnClickListener工作并打开一个Activity,但它似乎不像我做的那样工作。它显示一个错误,上面写着“无法解析构造函数......”。谁知道怎么修它?顺便说一句,我对Android开发很新。

编辑错误日志:

Error:(56, 36) error: no suitable constructor found for Intent(<anonymous OnClickListener>,Class<teamTasks>)
constructor Intent.Intent(String,Uri,Context,Class<?>) is not applicable
(actual and formal argument lists differ in length)
constructor Intent.Intent(Context,Class<?>) is not applicable
(actual argument <anonymous OnClickListener> cannot be converted to Context by method invocation conversion)
constructor Intent.Intent(String,Uri) is not applicable
(actual argument <anonymous OnClickListener> cannot be converted to String by method invocation conversion)
constructor Intent.Intent(String) is not applicable 
(actual and formal argument lists differ in length)
constructor Intent.Intent(Intent) is not applicable
(actual and formal argument lists differ in length)
constructor Intent.Intent() is not applicable
(actual and formal argument lists differ in length)

这是最底层的代码OnClickListener:

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

public class teamCreateScreen extends Activity {

private int counter = 0;
private int lastId = -1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.team_locate_layout);
}
public void createTeam(View view) {
    final RelativeLayout rlTeam = (RelativeLayout) findViewById(R.id.rlTeam);
    RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    Button tv = new Button(getApplicationContext());
    tv.setWidth(720);
    tv.setHeight(480);

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {

        tv.setId(Utils.generateViewId());

    } else {

        tv.setId(View.generateViewId());

    }

    if(lastId == -1) {
        relativeParams.addRule(RelativeLayout.BELOW, view.getId());
        counter++;
    } else if(counter == 1) {
        relativeParams.addRule(RelativeLayout.RIGHT_OF, lastId);
        relativeParams.addRule(RelativeLayout.ALIGN_TOP, lastId);
        counter++;
    } else if(counter == 2) {
        relativeParams.addRule(RelativeLayout.BELOW, lastId);
        counter = 1;
    }

    tv.setText("New Team");
    lastId = tv.getId();
    rlTeam.addView(tv, relativeParams);

    tv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent showTasks = new Intent(this, teamTasks.class);
            startActivity(showTasks);
        }
    });
}

}

2 个答案:

答案 0 :(得分:0)

而不是:

        Intent showTasks = new Intent(this, teamTasks.class);
        startActivity(showTasks);

替换为以下行:

        Intent showTasks = new Intent(teamCreateScreen.this, teamTasks.class);
        startActivity(showTasks);

答案 1 :(得分:-1)

你的构造函数

Intent showTasks = new Intent(this, teamTasks.class);

正在将此解析为OnClickListener。您需要更改它以引用外部活动的上下文,如下所示:

Intent showTasks = new Intent(teamCreateScreen.this, teamTasks.class);