首页 > 解决方案 > 它不会将数据从 Activity 发送到 Fragment

问题描述

我有很多布局、活动和片段。在一个名为的活动中有选项卡,其中Hastane1有四个选项卡。他们没有任何问题,但其中一个有。我想将数据从一个活动发送到片段。他们是分开的。片段在Hastane1活动,但Haberlesme1活动在另一个地方。它应该将我在调用的活动中输入的数字发送到名为Haberlesme1的片段中的 textView,该片段Hastane1Tab3FragHastane1. 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>

以及截图: 活动 分段

标签: javaandroidandroid-studio

解决方案


推荐阅读