Android CardView OnClick启动新活动无法正常工作

时间:2016-05-17 21:18:17

标签: android android-activity

我目前正在开发一款紧急应用程序,可在用户点击卡片时拨打服务短代码或号码。

到目前为止,我已经能够实现拨号功能,但我想从其中一张卡开始一项新活动而不是拨号。

我尝试使用意图从OnClick方法启动新活动,但我的应用程序崩溃,而其他卡工作得非常好

以下是IDE和CardviewDataAdapter.java代码生成的错误消息。 The error message screen shot CardViewDataAdapter.java代码:

public class CardViewDataAdapter extends RecyclerView.Adapter<CardViewDataAdapter.ViewHolder> {

    private List<ItemObject> itemList;


    public CardViewDataAdapter(Context context, List<ItemObject> itemList) {
        this.itemList = itemList;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public CardViewDataAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                             int viewType) {
        // create a new view
        View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.cardview_row, null);


        //Todo: set the view's size, margins, paddings and layout parameters
        // create ViewHolder
        ViewHolder viewHolder = new ViewHolder(itemLayoutView);
        return viewHolder;


    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {

        // - get data from your itemsData at this position
        // - replace the contents of the view with that itemsData
        viewHolder.image_view.setImageResource(itemList.get(position).getPhoto());
        viewHolder.image_name.setText(itemList.get(position).getName());

    }


    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return this.itemList.size();
    }

    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    // inner class to hold a reference to each item of RecyclerView
    public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        // each data item is a string and an image in this case
        public ImageView image_view;
        public TextView image_name;
        public Context context;


        public ViewHolder(View itemLayoutView) {


            super(itemLayoutView);
            this.image_view = (ImageView) itemLayoutView.findViewById(R.id.image_view);
            this.image_name = (TextView) itemLayoutView.findViewById(R.id.image_name);

            // Attach a click listener to the entire row view
            itemLayoutView.setOnClickListener(this);
            itemLayoutView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    Toast.makeText(v.getContext(), "OnLongClick Version :" + getAdapterPosition(),
                            Toast.LENGTH_SHORT).show();
                    return true;
                }
            });




        }


        // Handles the row being being clicked
        @Override

        public void onClick(View view) {


            if (getAdapterPosition() == 0) {
                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:123"));
                view.getContext().startActivity(callIntent);
            } else if (getAdapterPosition() == 1) {
                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:1234"));
                view.getContext().startActivity(callIntent);
            } else if (getAdapterPosition() == 2) {
                //intent =  new Intent(mContext, TutorialActivity.class);
                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:12345"));
                view.getContext().startActivity(callIntent);
            } else if (getAdapterPosition() == 3) {
                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:123456"));
                view.getContext().startActivity(callIntent);
            }else if (getAdapterPosition() == 4){
                view.getContext().startActivity(new Intent(view.getContext(),safetyActivity.class));
            }
            else {
                view.getContext().startActivity(new Intent(view.getContext(),safetyActivity.class));
            }

        }


    }


}

1 个答案:

答案 0 :(得分:-1)

您收到此异常是因为您尝试启动未在Manifest中声明的活动。