[英]I want to transfer data from activity to fragment and then display data in the fragment. My code doesn't work
Activity class is MainActivity.java, Fragment class is frganswer.java. 活动类是MainActivity.java,片段类是frganswer.java。 I am taking two numbers in the activity using edittext and then I need to display its sum in the fragment. 我在活动中使用edittext取两个数字,然后需要在片段中显示其总和。 There are no errors, but when I try to run it on the AVD, it goes 'not responding'. 没有错误,但是当我尝试在AVD上运行它时,它“没有响应”。 Please point out my mistakes. 请指出我的错误。
MainActivty.java MainActivty.java
public class MainActivity extends FragmentActivity {
String aa,bb;
double a,b,c;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText editText1=(EditText) findViewById(R.id.no1);
aa=editText1.getText().toString();
EditText editText2=(EditText) findViewById(R.id.no2);
bb=editText2.getText().toString();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void add(View view)
{
a=Integer.parseInt(aa);
b=Integer.parseInt(bb);
c=a+b;
send(c);
}
public void send(double c)
{
Bundle bundle=new Bundle();
bundle.putDouble("", c);
frganswer ob=new frganswer();
ob.setArguments(bundle);
}
}
frganswer.java frganswer.java
public class frganswer extends Fragment {
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.activity_fragment_answer,
container, false);
double c=getArguments().getDouble("");
String ans=String.valueOf(c);
TextView t = (TextView) view.findViewById(R.id.textView1);
t.setText(ans);
return view;
}
}
activity_main.xml activity_main.xml中
<EditText
android:id="@+id/no1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/app_name"
android:layout_marginTop="14dp"
android:ems="10"
android:hint="@string/no1"
android:inputType="numberDecimal" />
<EditText
android:id="@+id/no2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/no1"
android:layout_marginTop="18dp"
android:ems="10"
android:hint="@string/no2"
android:inputType="numberDecimal" />
<fragment
android:id="@+id/frg_1"
android:name="com.example.calculator.frganswer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/add"
android:layout_below="@+id/add"
android:layout_marginTop="61dp"
tools:layout="@layout/activity_fragment_answer" />
<Button
android:id="@+id/add"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/no2"
android:layout_below="@+id/no2"
android:layout_marginTop="20dp"
android:text="@string/add"
android:onClick="add"
</RelativeLayout>
activity_fragment_answer.xml activity_fragment_answer.xml
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:scrollHorizontally="true"
android:selectAllOnFocus="true"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Move this line 移动这条线
aa=editText1.getText().toString();
and this line 这条线
bb=editText2.getText().toString();
in to your add() method. 进入您的add()方法。 I assume your app is crashing as you are trying to parse an Integer when it is null or empty. 我假定您尝试解析一个Integer为null或为空时,您的应用程序崩溃了。 You should have a error log showing you exactly the line that it crashed at to help you with this kind of problem in the future. 您应该有一条错误日志,准确显示它崩溃的那一行,以在将来帮助您解决此类问题。
Since your fragment is declared in XML, when you create new instance in send()
it goes nowhere. 由于您的片段是用XML声明的,因此当您在send()
创建新实例时,它就无处可寻。 Instead in your send()
method do something like: 而是在send()
方法中执行以下操作:
public void send(double c)
{
frganswer ob= (frganswer) getFragmentManager().findFragmentById(R.id.frg_1);;
ob.displaySum(c);
}
And in your frganswer
add method 然后在您的frganswer
添加方法中
public void displaySum(double c) {
String ans=String.valueOf(c);
((TextView) view.findViewById(R.id.textView1)).setText(ans);
}
Also note that according to Java conventions class names should start from capital letteer Frganswer
还要注意,根据Java约定,类名应以大写字母Frganswer
Addition: @Marche101 is right too. 另外: @ Marche101也是正确的。 You should extract the values of aa and bb
only after button was clicked and check that values entered in EditText
s are not empty, are legal, etc. No need to do anything with args
in fragment's onCreate()
as it is not relevant in your case. 仅应在单击按钮后提取aa and bb
的值,并检查在EditText
输入的值是否为空,合法等。无需对fragment的onCreate()
args
进行任何操作,因为它与您的内容无关案件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.