活动类是MainActivity.java,片段类是frganswer.java。我在活动中使用edittext取两个数字,然后需要在片段中显示其总和。没有错误,但是当我尝试在AVD上运行它时,它“没有响应”。请指出我的错误。
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>
最佳答案
由于您的片段是用XML声明的,因此当您在send()
中创建新实例时,它无处可去。而是在您的send()
方法中执行以下操作:
public void send(double c)
{
frganswer ob= (frganswer) getFragmentManager().findFragmentById(R.id.frg_1);;
ob.displaySum(c);
}
然后在您的
frganswer
添加方法中public void displaySum(double c) {
String ans=String.valueOf(c);
((TextView) view.findViewById(R.id.textView1)).setText(ans);
}
还要注意,根据Java约定,类名应以大写字母
Frganswer
开头另外:@ Marche101也是正确的。您应该仅在单击按钮后提取
aa and bb
的值,并检查在EditText
中输入的值是否为空,合法等。不需要对片段的args
中的onCreate()
进行任何处理与您的情况无关。