简体   繁体   English

我想将数据从活动转移到片段,然后在片段中显示数据。 我的代码不起作用

[英]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.

相关问题 将数据从片段传递到另一个片段。 片段未附加到上下文 - Passing data from fragment to another fragment. Fragment not attached to a context Java代码在片段活动中不起作用 - java code doesn't work in fragment activity OnActivityResult在片段内部不起作用。 我该如何解决我的代码? - OnActivityResult does not work inside fragment. How can I fix this on my code? 将 RecylerView 项目数据发送到另一个片段。 我正在从 Firebase 实时数据库获取数据 - Send RecylerView Items Data To Another Fragment. I Am Getting Data From Firebase RealTime Database 将数据从“活动”传递到“片段”无效 - Passing data from Activity to Fragment does not work 为什么我不能从我的活动中获取数据到我的片段? - Why can't I get data from my activity to my fragment? 如何在不承载该片段的活动中刷新从 Listview 接收数据的片段中的 Textview - How to refresh a Textview in a fragment which receives data from a Listview in an activity which doesn't host that fragment 为什么我的片段不能显示来自 recyclerview 的数据? - Why ain't my fragment cannot display data from recyclerview? 我在片段活动中的按钮不起作用 - my button inside fragment activity doesn't work Android:Google Maps Fragment。 在新活动中显示标记信息 - Android: Google Maps Fragment. Display marker information in a new activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM