java - 它不会将数据从 Activity 发送到 Fragment
问题描述
我有很多布局、活动和片段。在一个名为的活动中有选项卡,其中Hastane1
有四个选项卡。他们没有任何问题,但其中一个有。我想将数据从一个活动发送到片段。他们是分开的。片段在Hastane1
活动,但Haberlesme1
活动在另一个地方。它应该将我在调用的活动中输入的数字发送到名为Haberlesme1
的片段中的 textView,该片段Hastane1Tab3Frag
是Hastane1
. Android Studio 本身没有任何错误,但是当我在手机上运行该应用程序时,它会在我按下名为“Gönder”(@id/button8
)的按钮时停止。
Haberlesme1(活动):
package com.example.projev021.İllerPackage.KocaeliPackage.GebzePackage.GebzeFragments.Hastane1Fragments;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.example.projev021.HaberlesmePackage.Haberlesme1;
import com.example.projev021.R;
public class Hastane1Tab3Frag extends Fragment {
Button goster;
TextView sonucText;
int sayi=0;
Haberlesme1.Ogrenci ogr;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
View v= inflater.inflate(R.layout.hastane1_tab3,container,false);
goster=(Button)v.findViewById(R.id.button13);
sonucText = (TextView)v.findViewById(R.id.textView3);
goster.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sonucText.setText(sayi);
}
});
return v;
}
public void sendData(int birinciSayi) {
this.sayi=birinciSayi;
}
public void sendOgrenci(Haberlesme1.Ogrenci ogrenci) {
this.ogr=ogrenci;
}
}
haberlesme1.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">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:fontFamily="@font/lineto_circular_black"
android:text="@string/kackisivar"
android:textSize="50sp"/>
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:ems="10"
android:inputType="number"
android:text="" />
<Button
android:id="@+id/button8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="50dp"
android:layout_marginEnd="50dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="5dp"
android:background="@drawable/buttonshape"
android:fontFamily="@font/lineto_circular_black"
android:text="@string/gonder"
android:textColor="#FFC107"
android:textSize="30sp"
android:onClick="calistir"/>
</LinearLayout>
Hastane1Tab3Frag(片段):
package com.example.projev021.İllerPackage.KocaeliPackage.GebzePackage.GebzeFragments.Hastane1Fragments;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.example.projev021.HaberlesmePackage.Haberlesme1;
import com.example.projev021.R;
public class Hastane1Tab3Frag extends Fragment {
Button goster;
TextView sonucText;
int sayi=0;
Haberlesme1.Ogrenci ogr;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
View v= inflater.inflate(R.layout.hastane1_tab3,container,false);
goster=(Button)v.findViewById(R.id.button13);
sonucText = (TextView)v.findViewById(R.id.textView3);
goster.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sonucText.setText(sayi);
}
});
return v;
}
public void sendData(int birinciSayi) {
this.sayi=birinciSayi;
}
public void sendOgrenci(Haberlesme1.Ogrenci ogrenci) {
this.ogr=ogrenci;
}
}
hastane1tab3.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container">
<LinearLayout
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginStart="10dp"
android:fontFamily="@font/lineto_circular_black"
android:text="@string/kisisayisi_"
android:textSize="50sp" />
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:fontFamily="@font/lineto_circular_black"
android:text=""
android:textSize="100sp"
android:textColor="#000000" />
<Button
android:id="@+id/button13"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="50dp"
android:layout_marginEnd="50dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="5dp"
android:background="@drawable/buttonshape"
android:fontFamily="@font/lineto_circular_black"
android:text="@string/goster"
android:textColor="#FFC107"
android:textSize="30sp"/>
</LinearLayout>
</FrameLayout>
解决方案
推荐阅读
- android - 检查 Firebase 事件是否成功执行
- android - 如何在 Android 上为一周中的特定日子安排闹钟?
- delphi - 调用汇编语言函数导致“浮点堆栈检查”异常
- android - 设置提醒后一分钟内弹出每周通知
- html - 垂直对齐图像旁边的文本
- javascript - 为什么我无法在 nodejs 中保存和检查会话?
- arrays - 如何在没有for in scala的情况下迭代数组?
- css - 在不使用 JavaScript 的情况下消除渲染阻塞 CSS - GoogleAMP
- bootstrap-4 - 如何在引导程序 4 中将文本和按钮放在同一行
- sql - 新手问题:代码行以列名 null 结尾。如何在此行末尾添加注释?