片段中的按钮导致活动不起作用(更新)

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

我有一个带有2个按钮的片段,旨在引导新活动。我已经获得了一些以前的反馈并创建了新代码。但是,仍然没有任何效果,甚至没有触发打印语句

package com.example.workoutapp;
import android.content.Intent;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class CreateFragment extends Fragment {
public Button button;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable 
Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_create, container, false);
    Button Workoutbutton=(Button)v.findViewById(R.id.Workoutbutton);
    Button Timersbutton=(Button)v.findViewById(R.id.Timerbutton);
    Workoutbutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            System.out.println("Workouts Clicked");
            Intent intent=new Intent(getActivity(),WorkoutsCreater.class);
            startActivity(intent);
        }
    });
    Timersbutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            System.out.println("Timers Pressed");
            Intent intent=new Intent(getActivity(),TimersCreater.class);
            startActivity(intent);
        }
    });

    return inflater.inflate(R.layout.fragment_create, container, false);


}



}
java button android-intent onclick fragment
1个回答
0
投票
package com.example.workoutapp;

import android.content.Intent;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class CreateFragment extends Fragment implements View.OnClickListener {
    public Button workoutButton;
    public Button timersButton;
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_create, container, false);
        workoutButton=view.findViewById(R.id.Workoutbutton);
        workoutButton.setOnClickListener(this);

        timersButton=view.findViewById(R.id.Timerbutton);
        timersButton.setOnClickListener(this);
        return view;

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.Workoutbutton:
                System.out.println("Workout Button Pressed");
                break;
            case R.id.Timerbutton:
                System.out.println("Timers Button Pressed");
                break;
        }
    }

}

© www.soinside.com 2019 - 2024. All rights reserved.