【发布时间】:2021-12-05 05:22:17
【问题描述】:
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;
}
}
点击 R1 我需要移动到 Specialist.class 页面,点击 R2 我需要移动到 Doctor.class 页面。如何在java文件中实现代码?我在单击 RadioButton 时实现了上述代码,它会关闭应用程序。请问谁能解决我的问题? Intent 中是否有任何错误。 enter image description here
【问题讨论】:
-
你能分享错误信息吗?还有(按钮)rootView.findViewById(R.id.R1);您应该将 (Button) 替换为 RadioButton
-
我附上了显示错误的图片。我已经用 RadioButton 替换了 Button。
-
你的 AndroidManifest.xml 中有代码,允许你的活动吗?前任。
-
是的,在 AndroidManifest.xml 中存在活动。
标签: android