This question already has answers here:
How do I pass data between Activities in Android application?
                                
                                    (53个答案)
                                
                        
                                3年前关闭。
            
                    
在以下用于android studio中的android应用的程序中,当调用第二个类(Main2Activity)的getPercent()时,它总是返回999(默认值),并且,


  ttper = .....;


onClick()中主类的语句永远不会执行。有什么具体原因吗?你们能指出吗!

这是主要活动,

public class MainActivity extends AppCompatActivity {

float i1m,i2m,mm,atp,assp;
float ttper=999;
boolean b=false;
EditText i1,i2,model,assignment,attendence;
Button b1;

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

    final Context cont = this;
    final Intent intent = new Intent(cont, Main2Activity.class);

    b1=(Button)findViewById(R.id.button2);
    b1.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            //try {
            i1=(EditText)findViewById(R.id.int1);
            i2=(EditText)findViewById(R.id.int2);
            model=(EditText)findViewById(R.id.mod);
            assignment=(EditText)findViewById(R.id.assign);
            attendence=(EditText)findViewById(R.id.attend);

            i1m = Float.parseFloat(String.valueOf(i1.getText()));
            i2m = Float.parseFloat(i2.getText().toString());
            mm = Float.parseFloat(model.getText().toString());
            assp = Float.parseFloat(assignment.getText().toString());
            atp = Float.parseFloat(attendence.getText().toString());
            ttper = ((i1m / 10) + (i2m / 10) + ((mm / 100) * 15) + (assp) + ((atp >= 75.0f) ? ((atp - 75) / 5) : 0.0f));

            //setValues();
            startActivity(intent);
            //}
            //catch (Exception e) {
              //  Log.e("app crash",e.getMessage());
            //}
        }
    });
}
/*void setValues()
{
   /* i1m = Float.parseFloat(String.valueOf(i1.getText()));
    i2m = Float.parseFloat(i2.getText().toString());
    mm = Float.parseFloat(model.getText().toString());
    assp = Float.parseFloat(assignment.getText().toString());
    atp = Float.parseFloat(attendence.getText().toString());*/
}/*
float getPercent()
{
        //float ttper=50.0f;
        return ttper;
}
}


这是第二项活动

public class Main2Activity extends AppCompatActivity {

float tper=1.0f;
String str;
@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        TextView v1 = (TextView) findViewById(R.id.textView11);
        MainActivity m1 = new MainActivity();
       //m1.setValues();
        //try {
            str = String.valueOf(m1.getPercent()) + "%";
            v1.setText(str);
        //}
    //catch (Exception e) {
      //  Log.e("app crash",e.getMessage());
    //}
}

}

最佳答案

它行不通。如果创建new MainActivity(),则您的ttper将为999

您应通过以下方式pass data between Activities


将新计算出的ttper放入Intent:intent.putExtra("ttper", ttper );
然后在MainActivity2中使用getIntent().getFloatExtra("ttper", 999.0f);

07-24 09:25