如何修复此代码以在Android应用程序中的活动之间传递数据?

时间:2017-10-12 13:31:55

标签: java android

我在一项活动(OHNSCalculatorSkillsCourses)中设置了一个计算器,它可以进行简单的计算并给出结果。

我希望结果显示在另一个活动中(OHNSCalculator)。

我已使用下面的两个活动代码进行了更新。

OHNSCalculatorSkillsCourses:

public class OHNSCalculatorSkillsCourses extends Activity{

EditText editText3SC;
TextView textViewResultSC;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ohns_calculator_skillscourses);

    editText3SC = (EditText) findViewById(R.id.editText3SC);
    textViewResultSC = (TextView) findViewById(R.id.textViewResultSC);

    final SharedPreferences prefs = PreferenceManager
            .getDefaultSharedPreferences(this);

    editText3SC.setText(prefs.getString("autoSave4", ""));

    String result= prefs.getString("autoSave4","");
    if(!result.equals("") && !result.isEmpty()){
        textViewResultSC.setText(addNumbers());
        prefs.getString("autoSave4","");
    }
    editText3SC.addTextChangedListener(new TextWatcher() {

        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
            // TODO Auto-generated method stub
        }

        public void onTextChanged(CharSequence s, int start, int before,
                                  int count) {
            textViewResultSC.setText(addNumbers());
        }


        public void afterTextChanged(Editable s)
        {
            prefs.edit().putString("autoSave4", s.toString()).commit();
        }


    });

}

public void showGreetings(View view)
{
    String button_text;
    button_text = ((Button) view).getText().toString();
    if (button_text.equals("Back"))
    {
        Intent intent = new Intent(this,OHNSCalculator.class);
        startActivity(intent);

        Intent getScore = new Intent(OHNSCalculatorSkillsCourses.this, OHNSCalculator.class);
        getScore.putExtra("SkillsScore", textViewResultSC.getText().toString());
        startActivity(getScore);

    }}

private String addNumbers() {
    float number1;
    if (!editText3SC.getText().toString().isEmpty() && editText3SC.getText().length() > 0) {
        number1 = Float.parseFloat(editText3SC.getText().toString());
    } else {
        number1 = 0;
    }
    float sum = (float) (number1 * 0.5);
    if (sum > 1) sum = 1;
    return sum + "";
}}

OHNSCalculator:

public class OHNSCalculator extends Activity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
    setContentView(R.layout.ohns_calculator);
    Intent mgetScore = getIntent();
    float textViewResultSC = Float.parseFloat(mgetScore.getStringExtra("SkillScore"));

}

public void showGreetings(View view)
{
    String button_text;
    button_text = ((Button) view).getText().toString();
    if (button_text.equals("Surgical Experience (Max 6)"))
    {
    Intent intent = new Intent(this,OHNSCalculatorSurgicalExperience.class);
        startActivity(intent);
    }
    else if (button_text.equals("Skills Courses (Max 1)"))
    {
    Intent intent = new Intent(this,OHNSCalculatorSkillsCourses.class);
        startActivity(intent);
    }
    else if (button_text.equals("Qualifications (Max 5)"))
    {
        Intent intent = new Intent(this,OHNSCalculatorQualifications.class);
        startActivity(intent);
    }
    else if (button_text.equals("Presentations (Max 3)"))
    {
        Intent intent = new Intent(this,OHNSCalculatorPresentations.class);
        startActivity(intent);
    }
    else if (button_text.equals("Publications (Max 5)"))
    {
        Intent intent = new Intent(this,OHNSCalculatorPublications.class);
        startActivity(intent);
    }

}}

我仍然收到此错误

logcat的

enter image description here

2 个答案:

答案 0 :(得分:7)

首先使用equals而不是!=

来比较字符串

第二:不发送TextView引用而是发送值

getScore.putExtra("SkillsScore",textViewResultSC.getText().toString());

第三步:在拥有数据后执行在intent对象和startActvity中放置值的行

第四:使用解析将字符串转换为浮点数,无论何时需要

答案 1 :(得分:4)

在获得 Intent onCreate() 的值后,您必须传递 Activity

您必须更改

Intent getScore = new Intent(SkillsCourses.this, Calculatorresult.class);
getScore.putExtra("SkillsScore",  textViewResultSC.getText().toString());
startActivity(getScore);

您可以使用以下代码获取结果   OnCreate() Calculatorresult.class

Intent mgetScore = getIntent();
String textViewResultSC = mgetScore.getStringExtra("SkillsScore");

注意:如果您想要textViewResultSC中的 float 结果,那么您可以使用以下代码:

Intent mgetScore = getIntent();
float textViewResultSC = Float.parseFloat(mgetScore.getStringExtra("SkillsScore"));

修改

你的班级必须

public class Calculatorresult extends Activity {

  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {


 super.onCreate(savedInstanceState);
    setContentView(R.layout.calculatorresult);
    Intent mgetScore = getIntent();
    float textViewResultSC = Float.parseFloat(mgetScore.getStringExtra("SkillsScore"));

}

编辑2

您在key

中传递了错误的 float textViewResultSC = Float.parseFloat(mgetScore.getStringExtra("SkillScore"));

因为您已使用 valuekey 从意图中传递了 SkillsScore ,所以您获得了 {{ 1}} 使用 valuekey 导致 SkillScore

您需要更改此

NullPointerException

float textViewResultSC = Float.parseFloat(mgetScore.getStringExtra("SkillScore"));