我在两个活动之间发送数据时遇到问题 - 我不熟悉编程并认为它与代码的定位有关但我不确定,更可能是它的某些东西简单。
我试图将stepCount
中的号码发送到第二个活动,它存储在myText3
中并且在第一个活动中可见并且计数正常(它的原始数据但是为现在)。
如果我将值设置为myText3
,它会在第二个活动中显示该值,但在stepCount
myText3
的值
提前致谢..
第一项活动
import android.content.Intent;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.view.View.OnClickListener;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
//import android.support.design.widget.FloatingActionButton;
//import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements SensorEventListener {
TextView tvx, tvy, tvz, display, display2, display3;
float total;
float stepCount;
float x, y, z;
float xPrev,yPrev, zPrev;
float totalPrev = 0;
Button saveButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//setSupportActionBar(toolbar);
/*FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});*/
tvx = (TextView) findViewById(R.id.textView);
tvy = (TextView) findViewById(R.id.textView2);
tvz = (TextView) findViewById(R.id.textView3);
display = (TextView) findViewById(R.id.display);
display2 = (TextView) findViewById(R.id.display2);
display3 = (TextView) findViewById(R.id.display3);
saveButton = (Button) findViewById(R.id.saveButton);
SensorManager mgr = (SensorManager) getSystemService(SENSOR_SERVICE);
Sensor GSensor = mgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mgr.registerListener(this, GSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public void onSensorChanged(SensorEvent event) {
// called byt the system every 10 ms
x = event.values[0];
y = event.values[1];
z = event.values[2];
total = x + y + z;
new CountDownTimer(3000, 1000) {
public void onFinish() {
// When timer is finished
// Execute your code here
xPrev = x;
yPrev = y;
zPrev = z;
totalPrev = xPrev + yPrev + zPrev;
}
public void onTick(long millisUntilFinished) {
// millisUntilFinished The amount of time until finished.
}
}.start();
if (Math.abs(total - totalPrev) > 20) {
stepCount++;
}
tvx.setText(String.valueOf(event.values[0]));
tvy.setText(String.valueOf(event.values[1]));
tvz.setText(String.valueOf(event.values[2]));
String mytext = Float.toString(total);
display.setText(mytext);
String mytext2 = Float.toString(totalPrev);
display2.setText(mytext2);
String mytext3 = Float.toString(stepCount);
display3.setText(mytext3);
saveButton.setOnClickListener(new View.OnClickListener() {
String mytext3;
@Override
public void onClick(View v) {
Intent A2Intent = new Intent (v.getContext(), Main2Activity.class);
//EditText et = (EditText) findViewById(R.id.editText);
// pass the entered phone number to the next activity
//A2Intent.putExtra("phoneNumber", tv2.getText());
//String ph = et.getText().toString();
A2Intent.putExtra("steps", mytext3);
// start the next activity/page
startActivity(A2Intent);
}
});
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
/*@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}*/
/*saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent A2Intent = new Intent (v.getContext(), Main2Activity.class);
EditText et = (EditText) findViewById(R.id.editText);
// pass the entered phone number to the next activity
//A2Intent.putExtra("phoneNumber", tv2.getText());
String ph = et.getText().toString();
A2Intent.putExtra("phoneNumber", ph);
// start the next activity/page
startActivity(A2Intent);
}
})*/
}
第二项活动
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Main2Activity extends AppCompatActivity {
TextView textView1;
Button newButton;
String displayCount;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
textView1 = (TextView) findViewById(R.id.editText);
newButton = (Button) findViewById(R.id.button);
displayCount = getIntent().getStringExtra("steps");
textView1.setText(displayCount);
}
}
答案 0 :(得分:0)
在saveButton onClickListener中,您正在创建一个新的String mytext3,然后将空的未初始化的字符串传递给您的意图。相反,你可以做的是使用findViewById在textView中检索实际的mytext3,然后将其传入。
答案 1 :(得分:0)
将String mytext3 = Float.toString(stepCount);
放入saveButton
onClick()
这样的内容:
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent A2Intent = new Intent (v.getContext(), Main2Activity.class);
String mytext3 = Float.toString(stepCount);
A2Intent.putExtra("steps", mytext3);
startActivity(A2Intent);
}
});
答案 2 :(得分:0)
saveButton.setOnClickListener(new View.OnClickListener() {
// String mytext3;
//you are giving intent a empty string here comment above line
@Override
public void onClick(View v) {
Intent A2Intent = new Intent (v.getContext(), Main2Activity.class);
//EditText et = (EditText) findViewById(R.id.editText);
// pass the entered phone number to the next activity
//A2Intent.putExtra("phoneNumber", tv2.getText());
//String ph = et.getText().toString();
A2Intent.putExtra("steps", mytext3);
// start the next activity/page
startActivity(A2Intent);
}
});
评论完成后,您必须final String mytext3
才能在onClick中访问它。