OnClickListener 设置不正确

OnClickListener not setting up correctly

我试图让 OnClickListener 与添加的视图一起工作并打开 Activity 但它似乎不像我做的那样工作。它显示一条错误消息 'cannot resolve constructor ...'。谁知道怎么修它?顺便说一句,我对 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);
        }
    });
}

}

而不是:

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

替换为以下行:

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

你的构造函数

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

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

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