Activity class is MainActivity.java, Fragment class is frganswer.java. I am taking two numbers in the activity using edittext and then I need to display its sum in the fragment. There are no errors, but when I try to run it on the AVD, it goes 'not responding'. Please point out my mistakes.
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
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
<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
<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. I assume your app is crashing as you are trying to parse an Integer when it is null or empty. 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. Instead in your send()
method do something like:
public void send(double c)
{
frganswer ob= (frganswer) getFragmentManager().findFragmentById(R.id.frg_1);;
ob.displaySum(c);
}
And in your frganswer
add method
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
Addition: @Marche101 is right too. 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.