无法强制转换为android.view.View $ OnClickListener

时间:2017-03-31 14:07:25

标签: android android-studio android-fragments onclicklistener

我有一个片段用于显示包含许多动态textView的数据,当实现setOnClickListener用于动态视图时会出现此错误:

com.Package.activity.DashboardActivity cannot be cast to android.view.View$OnClickListener

活动:DasgboardActivity:在frameLayout中加载片段

  getSupportFragmentManager().beginTransaction().replace(R.id.container,new TestFragment()).commit();

片段

public class TestFragment extends Fragment implements View.OnClickListener{

  CreateView createView;
List<Model> listData=new ArrayList();
  @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
  createView = new CreateView();
  listData=db.getAllData();
  createView.makeView(getActivity,listData);
}


    @Override
    public void onClick(View v) {}

}

CreateView类:用于创建动态视图:

  public View makeView(Context context, List<Model> words_List) {
        LinearLayout linearLayout = new LinearLayout(context);

        linearLayout.setOrientation(LinearLayout.HORIZONTAL);

            for (Model w : words_List) {

                TextView textView = new TextView(context);

                textView.setText(w.getW_text()+"");
                textView.setId(w.getW_id());
                textView.setTag(w.getW_id());


                    textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT));


       textView.setOnClickListener((View.OnClickListener) context);


                linearLayout.addView(textView);
            }


        return linearLayout;
    }

在这一行我得到了错误:    textView.setOnClickListener((View.OnClickListener)context);

我的代码完全处理活动,但在片段中我有错误

我该如何解决我的错误?

2 个答案:

答案 0 :(得分:0)

它是实现View.OnClickListener的片段,而不是活动。 这样做:

public class TestFragment extends Fragment{

    CreateView createView;
    List<Model> listData=new ArrayList();
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
         createView = new CreateView();
         listData=db.getAllData();
         createView.makeView(getActivity,listData);
    }


    View.OnClickListener textViewClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //your listener code here
        }
    };
}

当您需要将OnClickListener设置为textView时,请使用

textView.setOnClickListener(textViewClickListener);

答案 1 :(得分:0)

您可以在CreateView类中创建OnClickListener接口的实例,如下所示:

   public class CreateView {

        View.OnClickListener onClickListener = null;

        public CreateView(View.OnClickListener  listener){
            this.onClickListener = listener ; 
        }


        public View createLabelForMainWordsInOneLine(Context context, List<Model> words_List) {
            LinearLayout linearLayout = new LinearLayout(context);
            linearLayout.setOrientation(LinearLayout.HORIZONTAL);

            for (Model w : words_List) {

                TextView textView = new TextView(context);

                textView.setText(w.getW_text() + "");
                textView.setId(w.getW_id());
                textView.setTag(w.getW_id());


                textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT));


                textView.setOnClickListener(onClickListener);


                linearLayout.addView(textView);
            }


            return linearLayout;
        }
    }

然后,您可以像这样实现CreateView:

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
  createView = new CreateView(this);
  listData=db.getAllData();
  createView.makeView(getActivity,listData);
}