无法解析方法startActivity()

2024-01-12

我是 Android 开发新手,在更改活动时遇到了一些问题。我正在尝试从方法内更改活动,但收到错误cannot resolve method startActivity并在参数结束时出现错误Cannot resolve constructor 'Intent (...)'。我发现这里有一个问题 https://stackoverflow.com/questions/8266355/startactivity-red-highlighted遇到同样的问题,并尝试将他们的答复应用到我的程序中,但没有任何乐趣。

这是代码:

public void open301(View view) {
    startActivity(new Intent(CustomAdapter.this, ThreeZeroOne.class));
}

在查看上面链接的问题的答复之前,代码看起来像这样,但有相同的错误:

public void open301(View view) {
   Intent openThree = new Intent(this,ThreeZeroOne.class);
    startActivity(openThree);
}

完整代码:

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.content.Intent;

public class CustomAdapter extends BaseAdapter {
  String[] result;
  Context context;
  int[] imageId;

  private static LayoutInflater inflater = null;

  public CustomAdapter(selectGame SelectGame, String[] prgmNameList, int[] prgmImages) {
    result = prgmNameList;
    context = SelectGame;
    this.imageId = prgmImages;
    inflater = (LayoutInflater) context.getSystemService(Context.
        LAYOUT_INFLATER_SERVICE);
  }

  @Override
  public int getCount() {
    return result.length;
  }

  @Override
  public Object getItem(int position) {
    return position;
  }

  @Override
  public long getItemId(int position) {
    return position;
  }

  public class Holder {
    TextView tv;
    ImageView img;
  }

  @Override
  public View getView(final int position, View convertView, ViewGroup parent) {
    Holder holder = new Holder();
    View rowView;
    rowView = inflater.inflate(R.layout.game_selection, null);
    holder.tv = (TextView) rowView.findViewById(R.id.txt);
    holder.img = (ImageView) rowView.findViewById(R.id.img);
    holder.tv.setText(result[position]);
    holder.img.setImageResource(imageId[position]);
    rowView.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        Toast.makeText(context, "Beginning game " + result[position], Toast.LENGTH_SHORT).show();

      }
    });
    return rowView;
  }

  public void open301(View view) {
    Intent openThree = new Intent(this,ThreeZeroOne.class);
    startActivity(openThree);
  }
}

您应该使用适配器的上下文:

public void open301(View view) {
  Intent openThree = new Intent(context,ThreeZeroOne.class);
  context.startActivity(openThree);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法解析方法startActivity() 的相关文章

随机推荐