在片段中正确使用 onClick 方法来打开新活动是什么?

重新编辑:对于那些感兴趣的人,onClick 的方法在下面得到了解决。事实证明,您不能为控件创建单独的类,因此按钮控件需要位于当前活动类中。


问题:在我的片段中,我包含了几个控件,其中一个是应该打开新活动的按钮。单击按钮时,我收到以下崩溃...


这是单击当前活动片段中的按钮时出现的 LogCat 错误


E/AndroidRuntime: FATAL EXCEPTION: main

    Process: com.example.paymentdemo, PID: 14352

    java.lang.IllegalStateException: Could not find method onClick(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button_next_trans'

        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:424)

        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:381)

        at android.view.View.performClick(View.java:6597)

        at android.view.View.performClickInternal(View.java:6574)

        at android.view.View.access$3100(View.java:778)

        at android.view.View$PerformClick.run(View.java:25885)

        at android.os.Handler.handleCallback(Handler.java:873)

        at android.os.Handler.dispatchMessage(Handler.java:99)

        at android.os.Looper.loop(Looper.java:193)

        at android.app.ActivityThread.main(ActivityThread.java:6669)

        at java.lang.reflect.Method.invoke(Native Method)

        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)

        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

这是托管控件的片段


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    android:padding="16dp">



我没有经验,所以我的实现可能有点生疏。如果您对如何正确实现按钮 onClick 方法有任何建议,我将不胜感激。


蛊毒传说
浏览 136回答 2
2回答

万千封印

您需要为框架声明按钮和视图像这样&nbsp;Button button;&nbsp;public View myView;然后在 onCreate 像这样初始化&nbsp;myView = inflater.inflate(R.layout.fragment_button,container,false);&nbsp;button = (Button) myView.findViewById(R.id.button_next_trans);然后创建这样的方法,&nbsp; public void payments(){&nbsp; &nbsp; &nbsp;button.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Intent i = new Intent(getActivity(),MainActivity.class)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;startActivity(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;}并在 onCreate() 方法结束时返回视图。&nbsp;return myView;并调用支付();在 onCreate

慕后森

public class ButtonActivity extends AppCompatActivity implements View.OnClickListener {@Overrideprotected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; setContentView(R.layout.fragment_button);&nbsp; &nbsp; Button btnPay = (Button)findViewById(R.id.button_next_trans);&nbsp; &nbsp; btnPay.setOnClickListener(this);}public void goToPayments(View view) {&nbsp; &nbsp; Intent intent = new Intent(this, MainActivity.class);&nbsp; &nbsp; startActivity(intent);}@Overridepublic void onClick(View v) {&nbsp;{&nbsp; &nbsp; switch (v.getId()) {&nbsp; &nbsp; &nbsp; &nbsp; case R.id.button_next_trans:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; goToPayments();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }&nbsp; }}}从 xml 中删除以下行android:onClick="goToPayments"
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java