我正在尝试保存每秒生成的EEG耳机(int值)中的数据。
我试图在数组中保存每秒生成的不同值,以便可以在下一个活动中查看数组的值。
我当前的代码运行正常,但在网络活动中它只提供
[l@42d01e90
我的错误是什么?
尝试保存数据的代码:
注意:switch语句仅涉及要处理的数据类型。
我要保存的数据是msg.arg1
case TGDevice.MSG_MEDITATION:
//save level from each second in an array?
for(int i=0; i<medResults.length; i++){
msg.arg1= medResults[i];
}
我试图将值保存到的数组:
int[] medResults= new int[100];
传递数据的意图正在运行,但数据本身并未正确显示。
收到数据的活动:
public class MeditationResults extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.meditationresults);
// List view to hold the test results
TextView tv= (TextView) findViewById(R.id.medresults);
// getting data from the previous activity via intents
int[] results = getIntent().getIntArrayExtra("results");
tv.setText(results.toString());
}
}
包含switch语句的完整活动(供参考):
/**
* Class holding the activity that has the 10 random sums for the user to answer
* @author Ross
*
*/
public class Meditation extends Activity implements View.OnClickListener {
// declare vars
TextView text;
EditText answer;
Button submit;
TextView meditation;
int random1;
int random2;
String[] question = new String[10];
int correctAnswer[] = new int[10];
int[] results = new int[10];
int[] medResults= new int[100];
int score = 0;
int questionNumber = 1;
MediaPlayer correctNoise;
MediaPlayer incorrectNoise;
//Below are all imports from neurosky, bluetooth etc
BluetoothAdapter bluetoothAdapter;
TGDevice device;
TGEegPower eegPower;
final boolean rawEnabled = true; // ??
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.meditation);
// initialising variables
initialiseVars();
// set up random
setUpRandom();
// Set text view equal to question in array
text.setText(question[questionNumber - 1]);
// set on click listener for the submit button
submit.setOnClickListener(this);
// updateQuestion
updateQuestion();
//CODE RELATING TO NEUROSKY
// Check if Blue tooth is available on the Android device
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter == null) {
// Alert user that Bluetooth is not available
Toast.makeText(this, "Bluetooth not available", Toast.LENGTH_LONG)
.show();
// finish();
return;
} else {
// create the TGDevice
// Android app is connecting to hardware device using the standard
// constructor
device = new TGDevice(bluetoothAdapter, handler);
}
//meditation.append("NeuroSky: " + TGDevice.version + " " + TGDevice.build_title);
//meditation.append("\n");
//Code to connect device...
if (device.getState() != TGDevice.STATE_CONNECTING
&& device.getState() != TGDevice.STATE_CONNECTED) {
// Connect the device to the Neurosky Headset (starting connection
// process)
device.connect(rawEnabled); // note: rawEnabled allows raw sample
// data to be sent to device
device.EKGstartDetection();
}
}
/**
* Method that initialises variables
*/
public void initialiseVars() {
correctNoise = MediaPlayer.create(Meditation.this, R.raw.correctnoise);
incorrectNoise = MediaPlayer.create(Meditation.this, R.raw.incorrectnoise);
text = (TextView) findViewById(R.id.tvTopRandomTest);
answer = (EditText) findViewById(R.id.etEnterAnswerRandomTest);
submit = (Button) findViewById(R.id.btnSubmitRandomTest);
meditation = (TextView) findViewById(R.id.tvSeeMeditation);
}
/**
* Method that creates the random sum for user to answer
*/
public void setUpRandom() {
// setting up new random
Random random = new Random();
// Generating random number between 1 and 12
random1 = random.nextInt(12) + 1;
// Generating another random number between 1 and 12
random2 = random.nextInt(12) + 1;
// Creating random question String
question[questionNumber - 1] = random1 + " x " + random2 + " = ";
// Creating correct answer to question
correctAnswer[questionNumber - 1] = random1 * random2;
}
/**
* Method that updates question after each click
*/
public void updateQuestion() {
// updating question after each click
setUpRandom();
text.setText(question[questionNumber - 1]);
answer.setText("");
}
public void onClick(View v) {
// sets text view equal to what is entered in editText
final String entry = answer.getText().toString();
// convert from string value to int
int a = Integer.parseInt(entry); //
// setting the user answer equal to the correct part of results array
results[questionNumber - 1] = a;
// If user answer is equal to correct answer then increase score
if (a == correctAnswer[questionNumber - 1]) {
score++;
correctNoise.start();
}else{
incorrectNoise.start();
}
// if question number is under 10
if (questionNumber < 10) {
// updates question number
questionNumber++;
// called after an answer is given
updateQuestion();
} else {
//CHANGE THIS IF IT DOESNT WORK
// // Passing values to the results activity
Intent intent = new Intent(this, MeditationResults.class);
intent.putExtra("results", medResults);
// intent.putExtra("Questions", question);
// intent.putExtra("CorrectAnswer", correctAnswer);
// intent.putExtra("score", score);
// Start Activity
this.startActivity(intent);
}
}
//METHODS ETC RELATING TO NEUROSKY
/**
* Handles messages from TGDevice
*
*/
final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
// msg.what determines the type of each message
switch (msg.what) {
case TGDevice.MSG_EEG_POWER:
//WILL NEED TO CHANGE TO PUTPUT TO SCREEN.
// Log.d("LSD", "highAlpha: " + eegPower.highAlpha);
// Log.d("LSD", "lowAlpha: " + eegPower.lowAlpha);
// Log.d("LSD", "highBeta: " + eegPower.highBeta);
// Log.d("LSD", "lowBeta: " + eegPower.lowBeta);
// Log.d("LSD", "lowGamma: " + eegPower.lowGamma);
// Log.d("LSD", "midGamma: " + eegPower.midGamma);
// Log.d("LSD", "delta: " + eegPower.delta);
// Log.d("LSD", "theta: " + eegPower.theta);
break;
case TGDevice.MSG_STATE_CHANGE:
// actual value of the message is determined by msg.arg1
switch (msg.arg1) {
case TGDevice.STATE_IDLE:
break;
case TGDevice.STATE_CONNECTING:
meditation.append("Connecting...\n");
break;
case TGDevice.STATE_CONNECTED:
meditation.append("Connected.\n");
device.start();
break;
case TGDevice.STATE_NOT_FOUND:
meditation.append("Could not connect any of the paired BT devices. Turn them on and try again.\n");
break;
case TGDevice.STATE_ERR_NO_DEVICE:
meditation.append("No Bluetooth devices paired. Pair your device and try again.\n");
break;
case TGDevice.STATE_ERR_BT_OFF:
meditation.append("Bluetooth is off. Turn on Bluetooth and try again.");
break;
case TGDevice.STATE_DISCONNECTED:
meditation.append("Disconnected.\n");
} /* end switch on msg.arg1 */
break;
case TGDevice.MSG_POOR_SIGNAL:
//meditation.append("PoorSignal: " + msg.arg1 + "\n");
break;
case TGDevice.MSG_HEART_RATE:
meditation.append("Heart rate: " + msg.arg1 + "\n");
break;
case TGDevice.MSG_RAW_DATA:
/* Handle raw EEG/EKG data here */
//tv.append("RAW EEG: " + msg.arg1); Note: commented out as trying to get the exact figs (gamma etc)
break;
case TGDevice.MSG_ATTENTION:
//meditation.append("Attention: " + msg.arg1 + "\n");
break;
case TGDevice.MSG_MEDITATION:
//save level from each second in an array?
for(int i=0; i<medResults.length; i++){
msg.arg1= medResults[i];
}
//display each level on screen as it changes each second
meditation.setText("Meditation: " + msg.arg1 + "\n");
if(msg.arg1<=35){
//if mediation level is low it turns red
meditation.setBackgroundColor(Color.RED);
}else if((msg.arg1<=70)&&(msg.arg1>=35)){
meditation.setBackgroundColor(Color.YELLOW);
}else{
meditation.setBackgroundColor(Color.GREEN);
}
/
break;
case TGDevice.MSG_BLINK:
meditation.append("Blink: " + msg.arg1 + "\n");
break;
default:
break;
} /* end switch on msg.what */
//sv.fullScroll(View.FOCUS_DOWN); May need to use this again if the scroll view is needed!
} /* end handleMessage() */
}; /* end Handler */
/**
* This method is called when the user clicks on the "Connect" button.
*
* @param view
*/
public void doStuff(View view) {
// if the device is not currently connecting and is not already
// connected
if (device.getState() != TGDevice.STATE_CONNECTING
&& device.getState() != TGDevice.STATE_CONNECTED) {
// Connect the device to the Neurosky Headset (starting connection
// process)
device.connect(rawEnabled); // note: rawEnabled allows raw sample
// data to be sent to device
device.EKGstartDetection();
}
} /* end doStuff() */
/**
* Method that closes app if the back
* button is pressed
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
device.close();
this.finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
答案 0 :(得分:0)
您似乎犯的第一个错误就是您显示结果的方式。
tv.setText(results.toString());
默认情况下,toString()方法返回描述对象的字符串,如官方文档中所述:
Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object.
这就是你得到[l@42d01e90
的原因。为了显示数组的值,只需使用循环逐个显示每个值:
for(int i=0; i<results.length; i++) {
//Displays the data in LogCat
Log.e("LOG", results[i]);
}
但是我认为向用户显示一些很多值并不是一个好主意。您应该计算平均值并将其显示在TextView中。
int sum = 0;
for(int i=0; i<results.length; i++) {
sum += results[i];
}
int average = sum/results.length;
tv.setText(average);
希望它有所帮助!