为了账号安全,请及时绑定邮箱和手机立即绑定

如何修复此按钮,它无法转到活动页面?

如何修复此按钮,它无法转到活动页面?

潇潇雨雨 2022-08-03 15:39:20
public class select_fragment extends Fragment {    @Nullable    @Override    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        return inflater.inflate(R.layout.fragment_select, null);    }    private void button_parking(){        Intent myIntent = new Intent(f, parking.class);        startActivity(myIntent);    }}
查看完整描述

2 回答

?
手掌心

TA贡献1942条经验 获得超3个赞

试试这个...


public class select_fragment extends Fragment {


@Nullable

@Override

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup 

container, @Nullable Bundle savedInstanceState) {


    Button your_button = (Button) getActivity.findViewById(R.id.your_id_button)


    your_button.setOnClickListener(new OnClickListener() {

    public void onClick(View v)

    {

       button_parking();

    } 

    });

    return inflater.inflate(R.layout.fragment_select, null);


    }


    private void button_parking(){

    Intent myIntent = new Intent(getActivity(), parking.class);

    startActivity(myIntent);

}

}


查看完整回答
反对 回复 2022-08-03
?
慕容708150

TA贡献1831条经验 获得超4个赞

您尚未将视图绑定到片段,因此单击按钮无法正常工作。您需要使用 绑定视图。通常,您需要通过重写如下内容来执行绑定:findViewById()onViewCreated()


public class select_fragment extends Fragment {


    @Nullable

    @Override

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

        return inflater.inflate(R.layout.fragment_select, null);

    }


    @Override

    public void onViewCreated(View view, Bundle savedInstanceState) {

        super.onViewCreated(view, savedInstanceState);


         // bind the view here.

         Button button = findViewById(R.id.your_button);

         button.setOnClickListener(new View.OnClickListener() {

             public void onClick(View v) {

                 //call button method here

                 button_parking();

             }

         });

    }


    private void button_parking() {

        Intent myIntent = new Intent(f, parking.class);

        startActivity(myIntent);

    }

}


查看完整回答
反对 回复 2022-08-03
  • 2 回答
  • 0 关注
  • 77 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信