这是我自己做的一个小实验,想通过点击按键事件来打开第二个活动,可是运行的时候有按键打开第二活动的动作时候,程序就自动退出了,不晓得为什么。
public class FruitTitleFragment extends Fragment {
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fruit_title,container,false);
return view;
}
static class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder>{
List<Fruit> FruitList;
class ViewHolder extends RecyclerView.ViewHolder{
ImageView FruitImage;
TextView FruitTitle;
View FruitView;
public ViewHolder(View view){
super(view);
FruitImage = (ImageView) view.findViewById(R.id.fruit_picture);
FruitTitle = (TextView) view.findViewById(R.id.fruit_title);
FruitView = view;
}
}
public FruitAdapter(List<Fruit> FruitList){
this.FruitList = FruitList;
}
public FruitAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
final Context context = parent.getContext();
final View view = LayoutInflater.from(context).inflate(R.layout.fruit_item,parent,false);
final ViewHolder holder = new ViewHolder(view);
holder.FruitView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int position = holder.getAdapterPosition();
Fruit fruit = FruitList.get(position);
actionStart(context,fruit.getFruit_Produce(),fruit.getFruit_Name());
}
});
return holder;
}
public void onBindViewHolder(FruitAdapter.ViewHolder holder, int position) {
Fruit fruit = FruitList.get(position);
holder.FruitTitle.setText(fruit.getFruit_Name());
holder.FruitImage.setImageResource(fruit.getFruit_image_Resource());
}
public int getItemCount() {
return FruitList.size();
}
}
public static void actionStart(Context context,String fruit_produce,String fruit_name){
Intent intent = new Intent(context,FruitProduce.class);
intent.putExtra("produce",fruit_produce);
intent.putExtra("name",fruit_name);
context.startActivity(intent);
}
}
在一个活动里,我用Toast测试过,按键是可以正常发挥作用的,问题应该就是出在intent打开第二个活动那里。