onClickListener在片段中不起作用

时间:2013-06-20 18:07:36

标签: android android-fragments onclicklistener

public class MainFragment extends Fragment {
    public static final String TAG = MainFragment.class.getSimpleName();
    private static final String ABOUT_SCHEME = "settings";
    private static final String ABOUT_AUTHORITY = "main";
    public static final Uri ABOUT_URI = new Uri.Builder().scheme(ABOUT_SCHEME).authority(ABOUT_AUTHORITY).build();

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

        View v = inflater.inflate(R.layout.mainbutton, container, false);

        return v;
    }

}

根据以下链接: How to handle button clicks using the XML onClick within Fragments

public class StartFragment extends Fragment implements OnClickListener{


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

        View v = inflater.inflate(R.layout.fragment_start, container, false);

        Button b = (Button) v.findViewById(R.id.StartButton);
        b.setOnClickListener(this);
        return v;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.StartButton:

                /* ... */

                break;
        }
    }
}

来源也不起作用。

3 个答案:

答案 0 :(得分:4)

@ J.Romero是对的。试试这段代码,我改变了onClick方法并添加了一些调试日志。

public class StartFragment extends Fragment implements OnClickListener {

    private static final LOG_TAG = "com.example.activity"

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

        View v = inflater.inflate(R.layout.fragment_start, container, false);

        if (v != null) {
            Button mButton = (Button) v.findViewById(R.id.StartButton);
            Log.d(TAG, "View is not null");

            if (mButton != null) {
                b.setOnClickListener(this);
                Log.d(TAG, "mButton is not null");
            }
        }

        return v;
    }

    @Override
    public void onClick(View v) {
        if (v == mButton) {
            //do something
        }
    }
}

答案 1 :(得分:2)

您无法访问onCreateView方法中的UI元素 - EVER

使用onActivityCreated方法,即告诉片段活动已完全创建并准备好进行交互

 @Override
        public void onActivityCreated(Bundle savedInstanceState) 
        {
            // TODO Auto-generated method stub
            super.onActivityCreated(savedInstanceState);

            Button b = (Button) v.findViewById(R.id.StartButton);
            b.setOnClickListener(new OnClickListener() {

               @Override
                public void onClick(View v) {
                                    }
                            });
        }

答案 2 :(得分:-1)

尝试这种方式:

Button b = (Button) v.findViewById(R.id.StartButton);
b.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        /*Your code*/   
                        }
                });