![](/img/trans.png)
[英]Passing data from fragment to another fragment. Fragment not attached to a context
[英]I want to transfer data from activity to fragment and then display data in the fragment. My code doesn't work
活動類是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>
移動這條線
aa=editText1.getText().toString();
這條線
bb=editText2.getText().toString();
進入您的add()方法。 我假定您嘗試解析一個Integer為null或為空時,您的應用程序崩潰了。 您應該有一條錯誤日志,准確顯示它崩潰的那一行,以在將來幫助您解決此類問題。
由於您的片段是用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
輸入的值是否為空,合法等。無需對fragment的onCreate()
args
進行任何操作,因為它與您的內容無關案件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.