我有很多動態textViews,當工具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)上下文);
我的代碼完全工作的活動,但在片段我有錯誤
如何,我必須解決我的錯誤?
謝謝你。問題解決:) –
不客氣^^ –