包含标记中的android片段按钮不起作用

时间:2017-05-11 11:20:43

标签: android xml button include fragment

我遇到了一个问题,我使用时按钮不起作用

这是我的fragment_category_sub xml文件:

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

      ...

    <include
        layout="@layout/content_unavailable"
        />

    ....

这是我的include xml文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/content_unavailable"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp">

<Button
    android:id="@+id/try_again"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/error_default_tryAgain"/>

</LinearLayout>

这是我的片段,我尝试实现setOnClickListener:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    rootView = inflater.inflate(R.layout.fragment_category_sub, container, false);
    ButterKnife.bind(this, rootView);

    contentUnavailable = rootView.findViewById(R.id.content_unavailable);

    Button buttonAgain = (Button) 
    contentUnavailable.findViewById(R.id.try_again);

    buttonAgain.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (utils.isConnectingToInternet()) {

               categorySubPresenter.getAllListItems(uid, category_id);
            }
        }
    });

    return rootView;
}

有没有人知道为什么这个按钮在片段中被按下时不起作用以及如何解决它?因为Activity中的相同代码没有问题。提前谢谢。

1 个答案:

答案 0 :(得分:0)

我认为这不是因为{ "identificacao": "Manejatinga. ", "historico": "A técni.", "descricao": "A.sasasa ", "objetivos": "Existem. ", "sustentabilidade": "Co.", "vantagens_desvantagens": "VANNS: resi", "custos": "INVESTUSTOS: a", "direitos": "Tecnologia livre. ", "instituicao": "Tecnologia ", "assistencia_manutencao_te": " ", "experiencia": "Existem cerca de ", "entraves_adocao": "ENTRAção. ", "condicoes_requeridas": "Aio.", "fornecedores": "Sódocumeda.", "usuarios": "Produtentar. ", "programa": "Em áre.", "avaliacao_impacto": " reduzidos. ", "transferencia_tecnologia": "públicos diversos.", "outros": "Até 1000 cs", "visualizacao_tecnologia": "Cong", "palavras_chave": [ "teste", "aaaaaaa", ], 标签。我被警告过,我无法在片段中设置监听器。因此,如果您想在某个按钮上设置侦听器,则必须在活动中执行此操作。

创建活动时会调用片段回调include,因此从此刻开始,您可以调用片段onActivityCreated()

这是一个例子

getActivity() method