将片段分配给片段时出错

时间:2014-01-01 13:11:21

标签: android-intent android-fragments

公共类BusinessFragment扩展Fragment {     按钮航空;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.businessfragment, container, false);
}

public void addListenerOnButton1(){
    final Context context = this;

        aviation = (Button) findViewById (R.id.button1);
        aviation.setOnClickListener (new OnClickListener(){

            public void onClick (View v){

                Intent intent = new Intent (context, Aviation.class);
                startActivity (intent);



                }

                    });

                        }

错误突出显示单词“this”和“findviewbyid”,因此我无法运行该程序。 我已经搜索了很多关于片段和意图的信息,但无法得到关于如何做到这一点的明确答案

1 个答案:

答案 0 :(得分:0)

公共类BusinessFragment扩展Fragment {         按钮航空;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.businessfragment, container, false);
    }

    public void addListenerOnButton1(){

        aviation = (Button) getView().findViewById(R.id.button1);
         aviation.setOnClickListener (new OnClickListener(){


                public void onClick (View v){

                    Intent intent = new Intent(getActivity(), Aviation.class);
                    startActivity(intent);



                    }

                        });

                            }

}

我可以运行应用程序,但按钮不会打开新活动Aviation。