无法解析方法startActivity()

问题描述 投票:0回答:5

我是 Android 开发新手,在更改活动时遇到一些问题。我正在尝试从方法内更改活动,但收到错误

cannot resolve method startActivity
,并且在参数结束时出现错误
Cannot resolve constructor 'Intent (...)'
。我在这里发现了一个问题有同样的问题,并尝试将他们的回复应用到我的程序中,但没有喜悦。

这是代码:

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);
  }
}
android
5个回答
62
投票

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

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

17
投票

要启动一个新活动,您需要一个上下文来启动,而您当前的活动“BaseAdapter”不是一个上下文,幸运的是每个视图都有一个上下文,所以您可以这样做:

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

4
投票

首先你应该得到你的

Context
:

private Context context;

public CustomAdapter(Context context) {
    this.context = context;
}

然后:

context.startActivity(openThree);

0
投票

您还可以传递消息

 Intent i = new Intent( getContext(),Chat.class);
            i.putExtra("id",user.id);
            i.putExtra("name",user.name);
            getContext().startActivity(i);

0
投票

如果你不能使用这样的代码!

Intent intent = new Intent();
intent.setClass(MainActivity.this,MainActivity2.class);
startActivity(intent);

请在 build.gradle 文件“dependencies”中检查您的依赖版本 这是我的:

dependencies {
    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

它允许我使用上面的代码

© www.soinside.com 2019 - 2024. All rights reserved.