将数据从一个活动传递到另一个活动时出现Android错误

时间:2015-09-25 14:53:44

标签: android

我正在创建一个计算总分数的应用程序。百分比并在名为ResultActvity的下一个活动中显示结果。我正在使用相同的ResultActivity来显示第1年级学生的结果。第二年的学生成绩,一切正常,当它显示第一年的学生的结果,但是当它显示第二年的学生的结果时,它只显示总分,但没有显示%。我是android开发的新手,请帮我解决这个问题。谢谢你:)。

FIRSTACTIVITY



    package com.jntuhcalculator.anu.jntuhcalculator;

        import android.app.Activity;
        import android.content.Intent;
        import android.graphics.Typeface;
        import android.os.Bundle;
        import android.util.Log;
        import android.view.View;
        import android.widget.Button;
        import android.widget.EditText;
        import android.widget.TextView;
        import android.widget.Toast;

        /**
         * Created by anu on 18/9/15.
         */
        public class FirstMarks extends Activity {

            TextView tv_Subjects1, tv_Internal1, tv_External1;
            EditText et_Int_Eng, et_Int_M1, et_Int_MM, et_Int_Phy, et_Int_Chem, et_Int_Cp, et_Int_ElcsLab, et_Int_EpLab, et_Int_ItLab, et_Int_Draw;
            EditText et_Ext_Eng, et_Ext_M1, et_Ext_MM, et_Ext_Phy, et_Ext_Chem, et_Ext_Cp, et_Ext_ElcsLab, et_Ext_EpLab, et_Ext_ItLab, et_Ext_Draw;
            Button btn_Cal1, btn_Ok1;
            int IEng,IM1,IMM,IEPhy,IEChem,ICp,IEDraw,IELCS_Lab,IEP_LAB,IIT_LAB;
            int EEng,EM1,EMM,EEPhy,EEChem,ECp,EEDraw,EELCS_Lab,EEP_LAB,EIT_LAB;
            int ITotal1,ETotal1,Total1;
            float Percentage1;
            String f1;

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.marks_1);


                //INITIALISING VIEWS.
                //INITIALISING TEXT VIEWS.
                tv_Subjects1 = (TextView) findViewById(R.id.tv_Subject1);
                tv_Internal1 = (TextView) findViewById(R.id.tv_Internal1);
                tv_External1 = (TextView) findViewById(R.id.tv_External1);
                //INITIALISING EDIT TEXT.
                //INTERNAL
                et_Int_Eng = (EditText) findViewById(R.id.et_Int_Eng);
                et_Int_M1 = (EditText) findViewById(R.id.et_Int_M1);
                et_Int_MM = (EditText) findViewById(R.id.et_Int_MM);
                et_Int_Phy = (EditText) findViewById(R.id.et_Int_Phy);
                et_Int_Chem = (EditText) findViewById(R.id.et_Int_Chem);
                et_Int_Cp = (EditText) findViewById(R.id.et_Int_Cp);
                et_Int_ElcsLab = (EditText) findViewById(R.id.et_Int_ElcsLab);
                et_Int_EpLab = (EditText) findViewById(R.id.et_Int_EpLab);
                et_Int_ItLab = (EditText) findViewById(R.id.et_Int_ItLab);
                et_Int_Draw = (EditText) findViewById(R.id.et_Int_Draw);
                //EXTERNAL
                et_Ext_Eng = (EditText) findViewById(R.id.et_Ext_Eng);
                et_Ext_M1 = (EditText) findViewById(R.id.et_Ext_M1);
                et_Ext_MM = (EditText) findViewById(R.id.et_Ext_MM);
                et_Ext_Phy = (EditText) findViewById(R.id.et_Ext_Phy);
                et_Ext_Chem = (EditText) findViewById(R.id.et_Ext_Chem);
                et_Ext_Cp = (EditText) findViewById(R.id.et_Ext_Cp);
                et_Ext_ElcsLab = (EditText) findViewById(R.id.et_Ext_ElcsLab);
                et_Ext_EpLab = (EditText) findViewById(R.id.et_Ext_EpLab);
                et_Ext_ItLab = (EditText) findViewById(R.id.et_Ext_ItLab);
                et_Ext_Draw = (EditText) findViewById(R.id.et_Ext_Draw);
                //INITIALISING BUTTON
                btn_Cal1 = (Button) findViewById(R.id.btn_Cal1);
                btn_Ok1 = (Button) findViewById(R.id.btn_Ok1);




                //WHEN SAVE BUTTON IS CLICKED.
                btn_Ok1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                      //GETTING VALUES FROM EDIT TEXT.
                        //INTERNALS.
                        IEng = getIntValue(et_Int_Eng);
                        IM1 = getIntValue(et_Int_M1);
                        IMM = getIntValue(et_Int_MM);
                        IEPhy = getIntValue(et_Int_Phy);
                        IEChem = getIntValue(et_Int_Chem);
                        ICp = getIntValue(et_Int_Cp);
                        IEDraw = getIntValue(et_Int_Draw);
                        IELCS_Lab = getIntValue(et_Int_ElcsLab);
                        IEP_LAB = getIntValue(et_Int_EpLab);
                        //EXTERNALS.
                        EEng = getIntValue(et_Ext_Eng);
                        EM1 = getIntValue(et_Ext_M1);
                        EMM = getIntValue(et_Ext_MM);
                        EEPhy = getIntValue(et_Ext_Phy);
                        EEChem = getIntValue(et_Ext_Chem);
                        ECp = getIntValue(et_Ext_Cp);
                        EEDraw = getIntValue(et_Ext_Draw);
                        EELCS_Lab = getIntValue(et_Ext_ElcsLab);
                        EEP_LAB = getIntValue(et_Ext_EpLab);
                        EIT_LAB = getIntValue(et_Ext_ItLab);

                     //CALCUATIONS.
                        //INTERNAL TOTAL.
                        ITotal1 = (IEng+IM1+IMM+IEPhy+IEChem+ICp+IEDraw+IELCS_Lab+IEP_LAB+IIT_LAB);
                        //EXTERNAL TOTAL.
                        ETotal1 = (EEng+EM1+EMM+EEPhy+EEChem+ECp+EEDraw+EELCS_Lab+EEP_LAB+EIT_LAB);
                        //TOTAL.
                        Total1 = (ITotal1+ETotal1);
                        //PERCENTAGE.
                        Percentage1 = (float)(Total1/10);
                        f1 = Float.toString(Percentage1);

                        //CREATING TOAST.
                        Toast.makeText(getBaseContext(),"Press Result",Toast.LENGTH_SHORT).show();

                    }
                });

                btn_Cal1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent i = new Intent(FirstMarks.this,FirstResult.class);
                        Bundle b = new Bundle();
                        b.putInt("res1",Total1);
                        b.putString("per1",f1);
                        i.putExtras(b);
                        startActivity(i);
                        finish();
                    }
                });




            }



            //SUB-FUNCTION FOR GETTING INTEGER FROM EDITTEXT.
            private int getIntValue(EditText et) throws NumberFormatException {
                int i =0;
                String s;
                try {
                    s = et.getText().toString();
                    if(s.equals("")){
                        Toast.makeText(getApplicationContext(),"Invalid Marks",Toast.LENGTH_SHORT).show();
                    }
                    i = Integer.parseInt(s);
                    return i;

                } catch (NumberFormatException e) {
                    Log.e("ERROR:", "NOT A NUMBER");
                }
                  return 0;

            }


          }
    
**SECONDACTIVITY**


    package com.jntuhcalculator.anu.jntuhcalculator;

        import android.app.Activity;
        import android.content.Intent;
        import android.graphics.Typeface;
        import android.os.Bundle;
        import android.util.Log;
        import android.view.View;
        import android.widget.Button;
        import android.widget.EditText;
        import android.widget.TextView;
        import android.widget.Toast;

        /**
         * Created by anu on 19/9/15.
         */
        public class SecondMarks_1 extends Activity {

            TextView tv_PS,tv_MFCS,tv_DS,tv_DLD,tv_EDC,tv_BEE,tv_EELab,tv_DSLab;
            TextView tv_Subjects21, tv_Internal21, tv_External21;
            EditText et_Int_PS, et_Int_MFCS, et_Int_DS, et_Int_DLD, et_Int_EDC, et_Int_BEE, et_Int_EELab, et_Int_DSLab;
            EditText et_Ext_PS, et_Ext_MFCS, et_Ext_DS, et_Ext_DLD, et_Ext_EDC, et_Ext_BEE, et_Ext_EELab, et_Ext_DSLab;
            Button btn_Cal21, btn_Ok21;
            int IPS,IMFCS,IDS,IDLD,IEDC,IBEE,IEE_Lab,IDS_Lab;
            int EPS,EMFCS,EDS,EDLD,EEDC,EBEE,EEE_Lab,EDS_Lab;
            int ITotal21,ETotal21,Total21;
            float Percentage21;
            String f21;

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.secondmarks_1);


                //INITIALISING VIEWS.
                //INITIALISING TEXT VIEWS.
                tv_Subjects21 = (TextView) findViewById(R.id.tv_Subject21);
                tv_Internal21 = (TextView) findViewById(R.id.tv_Internal21);
                tv_External21 = (TextView) findViewById(R.id.tv_External21);
                //SUBJECT TEXT VIEWS.
                tv_PS = (TextView) findViewById((R.id.tv_PS));
                tv_MFCS = (TextView) findViewById((R.id.tv_MFCS));
                tv_DS = (TextView) findViewById((R.id.tv_DS));
                tv_DLD = (TextView) findViewById((R.id.tv_DLD));
                tv_EDC = (TextView) findViewById((R.id.tv_EDC));
                tv_BEE = (TextView) findViewById((R.id.tv_BEE));
                tv_EELab = (TextView) findViewById((R.id.tv_EE_Lab));
                tv_DSLab = (TextView) findViewById((R.id.tv_DS_Lab));
                //INITIALISING EDIT TEXT.
                //INTERNAL
                et_Int_PS = (EditText) findViewById(R.id.et_Int_PS);
                et_Int_MFCS = (EditText) findViewById(R.id.et_Int_MFCS);
                et_Int_DS = (EditText) findViewById(R.id.et_Int_DS);
                et_Int_DLD = (EditText) findViewById(R.id.et_Int_DLD);
                et_Int_EDC = (EditText) findViewById(R.id.et_Int_EDC);
                et_Int_BEE = (EditText) findViewById(R.id.et_Int_BEE);
                et_Int_EELab = (EditText) findViewById(R.id.et_Int_EELab);
                et_Int_DSLab = (EditText) findViewById(R.id.et_Int_DSLab);
                //EXTERNAL
                et_Ext_PS = (EditText) findViewById(R.id.et_Ext_PS);
                et_Ext_MFCS = (EditText) findViewById(R.id.et_Ext_MFCS);
                et_Ext_DS = (EditText) findViewById(R.id.et_Ext_DS);
                et_Ext_DLD = (EditText) findViewById(R.id.et_Ext_DLD);
                et_Ext_EDC = (EditText) findViewById(R.id.et_Ext_EDC);
                et_Ext_BEE = (EditText) findViewById(R.id.et_Ext_BEE);
                et_Ext_EELab = (EditText) findViewById(R.id.et_Ext_EELab);
                et_Ext_DSLab = (EditText) findViewById(R.id.et_Ext_DSLab);
                //INITIALISING BUTTON
                btn_Cal21 = (Button) findViewById(R.id.btn_Cal21);
                btn_Ok21 = (Button) findViewById(R.id.btn_Ok21);
        //DISPLAYING SUBJECT NAMES WHEN TEXT VIEW IS CLICKED.
                tv_PS.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        disp(tv_PS, "PS:Probabilty & Statistics");
                    }
                });
                tv_MFCS.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        disp(tv_MFCS, "MFCS:Mathematical Foundation Of Computer Science");

                    }
                });
                tv_DS.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {disp(tv_DS, "DS:Data Structures");
                    }
                });
                tv_DLD.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        disp(tv_DLD, "DLD:Digital Logic Design");
                    }
                });
                tv_EDC.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        disp(tv_EDC, "EDC:Electronic Devices & Circuits");
                    }
                });
                tv_BEE.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) { disp(tv_BEE, "BEE:Basic Electrical Engineering");
                    }
                });
                tv_EELab.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) { disp(tv_EELab, "Electrical & Electronics Lab");
                    }
                });
                tv_DSLab.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {disp(tv_DSLab, "Data Structures Lab");
                    }
                });


                //WHEN SAVE BUTTON IS CLICKED.
                btn_Ok21.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        //GETTING VALUES FROM EDIT TEXT.
                        //INTERNALS.
                        IPS = getIntValue(et_Int_PS);
                        IMFCS = getIntValue(et_Int_MFCS);
                        IDS = getIntValue(et_Int_DS);
                        IDLD = getIntValue(et_Int_DLD);
                        IEDC = getIntValue(et_Int_EDC);
                        IBEE = getIntValue(et_Int_BEE);
                        IEE_Lab = getIntValue(et_Int_EELab);
                        IDS_Lab = getIntValue(et_Int_DSLab);
                        //EXTERNALS.
                        EPS = getIntValue(et_Ext_PS);
                        EMFCS = getIntValue(et_Ext_MFCS);
                        EDS = getIntValue(et_Ext_DS);
                        EDLD = getIntValue(et_Ext_DLD);
                        EEDC = getIntValue(et_Ext_EDC);
                        EBEE = getIntValue(et_Ext_BEE);
                        EEE_Lab = getIntValue(et_Ext_EELab);
                        EDS_Lab = getIntValue(et_Ext_DSLab);

                        //CALCULATIONS.
                        //INTERNAL TOTAL.
                        ITotal21 = (IPS+IMFCS+IDS+IDLD+IEDC+IBEE+IEE_Lab+IDS_Lab);
                        //EXTERNAL TOTAL.
                        ETotal21 = (EPS+EMFCS+EDS+EDLD+EEDC+EBEE+EEE_Lab+EDS_Lab);
                        //TOTAL.
                        Total21 = (ITotal21+ETotal21);
                        //PERCENTAGE.
                        Percentage21 = ((Total21/750)*100);
                        f21 = Float.toString(Percentage21);

                        //CREATING TOAST.
                        Toast.makeText(getBaseContext(),"Press Result",Toast.LENGTH_SHORT).show();
                    }
                });

                  btn_Cal21.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent i = new Intent(SecondMarks_1.this,FirstResult.class);
                        Bundle b = new Bundle();
                        b.putInt("res1",Total21);
                        b.putString("per1",f21);
                        i.putExtras(b);
                        startActivity(i);
                        finish();
                    }
                });




            }



            //SUB-FUNCTION FOR GETTING INTEGER FROM EDITTEXT.
            private int getIntValue(EditText et) throws NumberFormatException {
                int i =0;
                String s;
                try {
                    s = et.getText().toString();
                    if(s.equals("")){
                        Toast.makeText(getApplicationContext(),"Invalid Marks",Toast.LENGTH_SHORT).show();
                    }
                    i = Integer.parseInt(s);
                    return i;

                } catch (NumberFormatException e) {
                    Log.e("ERROR:", "NOT A NUMBER");
                }
                return 0;

            }

            private void disp(TextView tv,String s){
                Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
            }

        }

    
**RESULTACTIVITY**
<pre><code>

    package com.jntuhcalculator.anu.jntuhcalculator;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;

    /**
     * Created by anu on 18/9/15.
     */
    public class FirstResult extends Activity {

        EditText et_Total1,et_Percentage1;
        Button btn_Exit1;
        int sres1;
        String sper1;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.result_first);

            //INITIALISING VIEWS.
            et_Total1 = (EditText) findViewById(R.id.et_Total1);
            et_Percentage1 = (EditText) findViewById(R.id.et_Percentage1);
            btn_Exit1 = (Button) findViewById(R.id.btn_Exit1);

            //GETTING DATA FROM PREVIOUS ACTIVITY.
            Bundle b = getIntent().getExtras();
             sres1 = b.getInt("res1", 0);
             sper1 = b.getString("per1");

           //Converting Float Into String.
            //f = Float.toString(sper1);

            //SETTING RESULTS.
            et_Total1.setText(sres1+"");
            et_Percentage1.setText(sper1+"%");

            //WHEN EXIT IS CLICKED.
            btn_Exit1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(getBaseContext(), YearDetails.class);
                    i.putExtra("Total",sres1);
                    startActivity(i);
                    finish();
                    System.exit(0);
               }
            });


        }
    }

1 个答案:

答案 0 :(得分:0)

您无法在不同的活动之间发送值,我会给您一个示例,尝试一下。

语法就像这样

用于发送数据

 String data = getIntent().getExtras().getString("keyName");
 Intent intent = new Intent(ActivityOne.this, ActivityTwo.class);
 intent.putExtra("keyName","value");

在ActivityTwo上接收

String value= getIntent().getStringExtra("keyName");
Intent intent = getIntent();
Bundle extras = intent.getExtras();

附加提示: -

所以你需要在FirstActivity中编辑

 btn_Cal21.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent i = new Intent(SecondMarks_1.this,FirstResult.class);
                        Bundle b = new Bundle();
                        b.putInt("res1",Total21);
                        b.putString("per1",f21);
                        i.putExtras(b);
                        startActivity(i);
                        finish();
                    }
                });

以下活动相同。

仍然没有得到?实际上在你的FirstActivity中已经放了

 Intent i = new Intent(FirstMarks.this,FirstResult.class);
                        Bundle b = new Bundle();
                        b.putInt("res1",Total1);
                        b.putString("per1",f1);

在你的第二次活动中。

Intent i = new Intent(SecondMarks_1.this,FirstResult.class);
                    Bundle b = new Bundle();
                    b.putInt("res1",Total21);
                    b.putString("per1",f21);
                    i.putExtras(b);
                    startActivity(i);

你发现你在两个地方都使用过 per1字符串,改变一下,这可能导致应用程序无法获取值(可能会提供其他字符串)。

在这部分中,您需要为要接收的第二个活动结果添加代码。

             Bundle b = getIntent().getExtras();
             sres1 = b.getInt("res1", 0);
             sper1 = b.getString("per1");

希望有所帮助。