无法parsing方法startActivity()

我是新来的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); } 

}

Solutions Collecting From Web of "无法parsing方法startActivity()"

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

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

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

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

首先得到你的上下文:

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

接着:

  context.startActivity(openThree);