问题描述
我是android开发的新手,更改活动时遇到了一些问题.我正在尝试从方法内部更改活动,但出现错误无法解析方法startActivity
,并且在参数结束处出现错误无法解析构造函数'Intent(...)'代码>.我发现此处有一个问题,具有相同类型的问题,并尝试将其答复实施到我的程序中,但没有欢乐.
I'm new to the android development and having a bit of a problem changing activities. I am trying to change activities from within a method but I am getting the error cannot resolve method startActivity
and on the parameter end the error Cannot resolve constructor 'Intent (...)'
. I found a question here with the same sort of problem and tried to implement their replies into my program but no joy.
这是代码:
public void open301(View view) {
startActivity(new Intent(CustomAdapter.this, ThreeZeroOne.class));
}
在查看代码上方链接的问题的答复之前,看起来是这样,但有相同的错误:
before looking at the replies from the question linked above the code looked like this with the same errors:
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);
}
}
推荐答案
您应使用适配器的上下文:
You should use the context of your adapter:
public void open301(View view) {
Intent openThree = new Intent(context,ThreeZeroOne.class);
context.startActivity(openThree);
}
这篇关于无法解析方法startActivity()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!