我知道,此问题已在此站点和其他站点中存在,但我仍然尝试查找解决方案,因为到目前为止,每个解决方案都失败了。
我有一个活动,其中包含2个片段。在第一个Fragment中,我有5个EditText,此输入我需要传递到第二个Fragment,但是...我在此Package中也有一个Java类,它必须使用此输入的值,结果我必须显示在第二个片段中。 这是一个普通的计算器。
这是我的第一个片段:
package com.example.afotel;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class gyorsSzam extends Fragment {
EditText gradeFive, gradeFour, gradeThree, gradeTwo, gradeOne;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_gyors_szam, container, false);
Button calculate = (Button) view.findViewById(R.id.gGyors);
gradeFive = (EditText) view.findViewById(R.id.numberOfGardeFive);
gradeFour = (EditText) view.findViewById(R.id.numberOfGardeFour);
gradeThree = (EditText) view.findViewById(R.id.numberOfGardeThree);
gradeTwo = (EditText) view.findViewById(R.id.numberOfGardeTwo);
gradeOne = (EditText) view.findViewById(R.id.numberOfGardeOne);
calculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentTransaction fr = getFragmentManager().beginTransaction();
fr.replace(R.id.gyorsSzamlalas, new jegyekFelLe());
fr.commit();
String gradeFiveSt = gradeFive.getText().toString();
String gradeFourSt = gradeFour.getText().toString();
String gradeThreeSt = gradeThree.getText().toString();
String gradeTwoSt = gradeTwo.getText().toString();
String gradeOneeSt = gradeOne.getText().toString();
Bundle bundle = new Bundle();
bundle.putString("Five", gradeFiveSt );
bundle.putString("Four", gradeFourSt );
bundle.putString("Three", gradeThreeSt );
bundle.putString("Two", gradeTwoSt );
bundle.putString("Ones", gradeOneeSt );
}
});
return view;
}
}
这是我的第二个片段:
package com.example.afotel;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
public class jegyekFelLe extends Fragment {
public jegyekFelLe() {
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater,@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_jegyek_fel_le, container, false);
Bundle bundle = getArguments();
String gradeFive = bundle.getString("Five");
String gradeFour = bundle.getString("Four");
String gradeThree = bundle.getString("Three");
String gradeTwo = bundle.getString("Two");
String gradeOne = bundle.getString("One");
TextView upFive = (TextView) rootView.findViewById(R.id.upFive);
upFive.setText(gradeFive);
TextView felNegyes = (TextView) rootView.findViewById(R.id.);
upFour.setText(gradeFour);
TextView upThree= (TextView) rootView.findViewById(R.id.upThree);
upThree.setText(gradeThree);
TextView upTwo = (TextView) rootView.findViewById(R.id.upTwo);
upTwo.setText(gradeTwo);
TextView downOne = (TextView) rootView.findViewById(R.id.dowbOne);
leEgyes.setText(gradeOne);
return rootView;
}
}
如果代码包含TextView upFive ...(等)部分,如果单击“计算”按钮,程序将崩溃。我不知道这是什么问题,因为我做了这部视频所说的一切:https://www.youtube.com/watch?v=_4i5Jk5RnFA
我知道,此示例中未使用Java类,这是因为我的代码存在此问题。首先,我想知道如何在两个Fragment之间传递数据,然后编写以计算这5个EditText所需的结果。 我是Android Studio的入门者,因此我需要简单,难以理解的解决方案。
希望您能理解我的问题,对不起我的英语。
答案 0 :(得分:1)
设置包并在提交事务之前向片段添加参数。您可能需要将代码更改为以下结构:
Bundle bundle= new Bundle();
//add items to the bundle then begin transacting
Fragment jegyekFelLe= new jegyekFelLe();
jegyekFelLe.setArguments(bundle);
FragmentTransaction fr = getFragmentManager().beginTransaction();
fr.replace(R.id.gyorsSzamlalas,jegyekFelLe );
fr.commit();
答案 1 :(得分:0)
您没有将捆绑软件作为片段中的参数传递
*将数据从片段A发送到片段B
jom: C:\Users\Klaas\OneDrive\Universiteit\Year 3\Computer Graphics\Assignment
1\build-OpenGL_transformations-Desktop_Qt_5_12_1_MSVC2017_64bit-Debug\Makefile.Debug [debug\user_input.obj] Error 2
jom: C:\Users\Klaas\OneDrive\Universiteit\Year 3\Computer Graphics\Assignment 1\build-OpenGL_transformations-Desktop_Qt_5_12_1_MSVC2017_64bit-Debug\Makefile.Debug [debug\model.obj] Error 2
jom: C:\Users\Klaas\OneDrive\Universiteit\Year 3\Computer Graphics\Assignment 1\build-OpenGL_transformations-Desktop_Qt_5_12_1_MSVC2017_64bit-Debug\Makefile.Debug [debug\main.obj] Error 2
jom: C:\Users\Klaas\OneDrive\Universiteit\Year 3\Computer Graphics\Assignment 1\build-OpenGL_transformations-Desktop_Qt_5_12_1_MSVC2017_64bit-Debug\Makefile.Debug [debug\mainview.obj] Error 2
jom: C:\Users\Klaas\OneDrive\Universiteit\Year 3\Computer Graphics\Assignment 1\build-OpenGL_transformations-Desktop_Qt_5_12_1_MSVC2017_64bit-Debug\Makefile.Debug [debug\mainwindow.obj] Error 2
jom: C:\Users\Klaas\OneDrive\Universiteit\Year 3\Computer Graphics\Assignment 1\build-OpenGL_transformations-Desktop_Qt_5_12_1_MSVC2017_64bit-Debug\Makefile [debug] Error 2
13:15:36: The process "D:\ProgramFiles\Qt\Tools\QtCreator\bin\jom.exe" exited with code 2.
Error while building/deploying project OpenGL_transformations (kit: Desktop Qt 5.12.1 MSVC2017 64bit)
The kit Desktop Qt 5.12.1 MSVC2017 64bit has configuration issues which might be the root cause for this problem.
When executing step "Make"
*从片段a中获取数据
Bundle bundle= new Bundle();
bundle.putString("Your_key",data)
//add items to the bundle then begin transacting
Fragment fragment= new jegyekFelLe();
fragment.setArguments(bundle);
FragmentTransaction fr = getFragmentManager().beginTransaction();
fr.replace(R.id.gyorsSzamlalas,fragment );
fr.commit();
答案 2 :(得分:0)
1。)您需要使用“活动”的支持片段管理器,而不是片段的子片段管理器
2。),您需要先将arguments
捆绑软件交给捆绑软件,然后再将其添加到事务处理的碎片管理器中。
calculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String gradeFiveSt = gradeFive.getText().toString();
String gradeFourSt = gradeFour.getText().toString();
String gradeThreeSt = gradeThree.getText().toString();
String gradeTwoSt = gradeTwo.getText().toString();
String gradeOneeSt = gradeOne.getText().toString();
FragmentTransaction fr = (((FragmentActivity)getActivity()).getSupportFragmentManager()).beginTransaction();
Fragment fragment = new jegyekFelLe();
Bundle bundle = new Bundle();
bundle.putString("Five", gradeFiveSt );
bundle.putString("Four", gradeFourSt );
bundle.putString("Three", gradeThreeSt );
bundle.putString("Two", gradeTwoSt );
bundle.putString("Ones", gradeOneeSt );
fragment.setArguments(bundle);
fr.replace(R.id.gyorsSzamlalas, fragment);
fr.commit();
}
答案 3 :(得分:-1)
您可以使用模型类执行此操作,示例如下
打包com.example.afotel;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class gyorsSzam extends Fragment {
EditText gradeFive, gradeFour, gradeThree, gradeTwo, gradeOne;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_gyors_szam, container, false);
Button calculate = (Button) view.findViewById(R.id.gGyors);
gradeFive = (EditText) view.findViewById(R.id.numberOfGardeFive);
gradeFour = (EditText) view.findViewById(R.id.numberOfGardeFour);
gradeThree = (EditText) view.findViewById(R.id.numberOfGardeThree);
gradeTwo = (EditText) view.findViewById(R.id.numberOfGardeTwo);
gradeOne = (EditText) view.findViewById(R.id.numberOfGardeOne);
calculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String gradeFiveSt = gradeFive.getText().toString();
String gradeFourSt = gradeFour.getText().toString();
String gradeThreeSt = gradeThree.getText().toString();
String gradeTwoSt = gradeTwo.getText().toString();
String gradeOneeSt = gradeOne.getText().toString();
ModelClass modelClass = new ModelClass();
modelClass.setFive(gradeFiveSt );
modelClass.setFour(gradeFourSt );
modelClass.setThree( gradeThreeSt );
modelClass.setTwo(gradeTwoSt );
modelClass.setOnes(gradeOneeSt );
FragmentTransaction fr = getFragmentManager().beginTransaction();
fr.replace(R.id.gyorsSzamlalas, new jegyekFelLe(modelClass));
fr.commit();
}
});
return view;
}
}
package com.example.afotel;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
public class jegyekFelLe extends Fragment {
ModelClass modelClass;
public jegyekFelLe(ModelClass modelClass) {
this.modelClass = modelClass;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater,@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_jegyek_fel_le, container, false);
//Bundle bundle = getArguments();
//String gradeFive = bundle.getString("Five");
//String gradeFour = bundle.getString("Four");
//String gradeThree = bundle.getString("Three");
//String gradeTwo = bundle.getString("Two");
//String gradeOne = bundle.getString("One");
TextView upFive = (TextView) rootView.findViewById(R.id.upFive);
upFive.setText(modelClass.getFive());
TextView felNegyes = (TextView) rootView.findViewById(R.id.);
upFour.setText(modelClass.getFour());
TextView upThree= (TextView) rootView.findViewById(R.id.upThree);
upThree.setText(modelClass.getThree());
TextView upTwo = (TextView) rootView.findViewById(R.id.upTwo);
upTwo.setText(modelClass.getTwo());
TextView downOne = (TextView) rootView.findViewById(R.id.dowbOne);
leEgyes.setText(modelClass.getOnes());
return rootView;
}
}
public class ModelClass {
private String Ones;
private String Two;
private String Three;
private String Four;
private String Five;
public String getOnes() {
return Ones;
}
public void setOnes(String ones) {
Ones = ones;
}
public String getTwo() {
return Two;
}
public void setTwo(String two) {
Two = two;
}
public String getThree() {
return Three;
}
public void setThree(String three) {
Three = three;
}
public String getFour() {
return Four;
}
public void setFour(String four) {
Four = four;
}
public String getFive() {
return Five;
}
public void setFive(String five) {
Five = five;
}
}