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

问题描述 投票:0回答:2

重新编辑:对于那些感兴趣的人,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">

    <TextView
        android:id="@+id/transaction_header"
        style="?textGroupheader"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginLeft="0dp"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="0dp"
        android:layout_marginRight="0dp"
        android:layout_marginBottom="20dp"
        android:text="@string/group_header"
        android:textStyle="bold" />

    <!-- Spinner displays accounts available using SpinnerActivity -->
    <include
        layout="@layout/fragment_spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <!-- Radio checks on or off. Still need to link selected as fragments for button on click behaviour -->
    <include
        layout="@layout/fragment_radio"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <!-- Button will take selected radio and open related activity, for now we just go to MainActivity -->
    <include
        layout="@layout/fragment_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

按钮控制代码

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ButtonActivity">

    <Button
        android:id="@+id/button_next_trans"
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:background="@drawable/action_button"
        android:paddingStart="24dp"
        android:paddingLeft="24dp"
        android:paddingTop="8dp"
        android:paddingEnd="24dp"
        android:paddingRight="24dp"
        android:paddingBottom="8dp"
        android:text="@string/next"
        android:textColor="@color/colorWhite"
        android:onClick="goToPayments"/>

</FrameLayout>

按钮活动

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;


public class ButtonActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_button);
    }

    public void goToPayments(View view) {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }

}

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

java android android-fragments android-activity android-button
2个回答
0
投票

你需要声明按钮和View for fragment这样

 Button button;
 public View myView;

然后在onCreate中初始化为这样

 myView = inflater.inflate(R.layout.fragment_button,container,false);
 button = (Button) myView.findViewById(R.id.button_next_trans);

然后创建这样的方法,

  public void payments(){
     button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           Intent i = new Intent(getActivity(),MainActivity.class)
           startActivity(i);
           }
        }
   }

并在onCreate()方法结束时返回视图。

 return myView;

并致电付款();在onCreate


0
投票
public class ButtonActivity extends AppCompatActivity implements View.OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_button);

    Button btnPay = (Button)findViewById(R.id.button_next_trans);
    btnPay.setOnClickListener(this);

}

public void goToPayments(View view) {
    Intent intent = new Intent(this, MainActivity.class);
    startActivity(intent);
}

@Override
public void onClick(View v) {
 {
    switch (v.getId()) {
        case R.id.button_next_trans:
            goToPayments();
            break;      
    }
  }
}}

从xml中删除以下行

android:onClick="goToPayments"
© www.soinside.com 2019 - 2024. All rights reserved.