0
fragment2_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RadioGroup
        android:id="@+id/G1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/R1"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:text="By Specialist" />

        <RadioButton
            android:id="@+id/R2"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:text="By Doctor"/>
    </RadioGroup>


</LinearLayout>

Fragment2.java
package com.example.tabbedapp;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

public class Fragment2 extends Fragment {
    public Fragment2(){

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment2_layout, container, false);

        Intent intent = new Intent(getActivity(), Specialist.class);
        RadioButton button = (RadioButton) rootView.findViewById(R.id.R1);
        button.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                startActivity(intent);
            }
        });
        return rootView;
    }

}

By clicking R1 I need to move to Specialist.class page, clicking R2 I need to move to Doctor.class page. How do I implement the code in java file? I have implemented the above code while clicking the RadioButton, it closes the app. Please Can anyone resolve my problem? Is there any mistake in Intent. enter image description here

Suji
  • 11
  • 3
  • Can you share the error message? Also (Button) rootView.findViewById(R.id.R1); You should replace (Button) with RadioButton – gokmenbayram Oct 18 '21 at 05:42
  • 1
    https://stackoverflow.com/a/6781061/9030938 check this – Naveen Oct 18 '21 at 05:51
  • I have attached the image that shows error. And I have replaced the Button with RadioButton. – Suji Oct 18 '21 at 06:08
  • do u have code in your AndroidManifest.xml, that allows your activities? ex. – SWR Oct 18 '21 at 07:01
  • Yes, in AndroidManifest.xml the activity exists. – Suji Oct 18 '21 at 07:20
  • your classes Doctor and Specialist extends from Fragment or Activity? – SWR Oct 18 '21 at 07:22
  • Probably u have wrong casting, try to change this line: Button button = (Button) rootView.findViewById(R.id.R1); to RadioButton button = (RadioButton) rootView.findViewById(R.id.R1); – SWR Oct 18 '21 at 07:30
  • Yes, I got the output. I have changed the Specialist fragment to Specialist Activity. – Suji Oct 18 '21 at 08:27

2 Answers2

0

You can use like this

button.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View view) {
            Intent intent = new Intent(this,  
            Specialist.class);
        }

Or

final RadioGroup group= (RadioGroup) findViewById(R.id.radioGroupId);
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
    int id = group.getCheckedRadioButtonId();
    switch (id) {
        case R.id.r1:
            // Your code
            break;
        case R.id.r2:
            // Your code
            break;
        case R.id.r3:
            // Your code
            break;
        case R.id.r4:
            // Your code
            break;
        default:
            // Your code
            break;
    }
}});
0

I hope, that your classes like Speacialist and Doctor are Activities, not Fragments, so probably you forgot to register your activities in the manifest.

   <activity
        android:name=".Specialist"/>
   <activity
        android:name=".Doctor"/>
SWR
  • 73
  • 10