我正在尝试编写一些代码来跟踪Android设备上的一些生物识别 每次按第四个按钮,程序都应输出:
size1 size2 size3 size4, pressure1 pressure2 pressure3 pressure4, touchduration1 touchduration2 touchduration3 touchduration4, betweentime1 betweentime2 betweentime3;
出于某种原因,虽然它没有插入计算机并且我自己尝试运行它,但是第一次按下第四个按钮时程序崩溃了。
我已经调试了一段时间,但我无法弄清楚它为什么不起作用。
我希望一双新鲜的眼睛能够帮助我。
package com.example.joe.myapplication;
import android.content.Context;
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import java.io.File;
import java.io.FileOutputStream;
import java.util.*;
import android.app.Activity;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.*;
import android.widget.EditText;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
public class MainActivity extends ActionBarActivity {
/*long pressTime1 = 0;
long releaseTime1 = 0;
long pressTime2 = 0;
long releaseTime2 = 0;
long pressTime3 = 0;
long releaseTime3 = 0;
long pressTime = 0;
long releaseTime = 0;*/
long pressTime = 0;
long releaseTime = 0;
long pressDuration = 0;
long betweenDuration = 0;
int count = 0;
int count2 = 0;
float [] array = new float [10];
double[] sizeArray = new double[4];
double[] pressureArray = new double[4];
long[] pressTimeArray = new long[4];
long[] betweenTimeArray = new long[4];
int i = 0; //button number
int session_number = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Sets the spinner
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.Sessions, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
final String Text = spinner.getSelectedItem().toString();
// get EditText by id
EditText inputTxt = (EditText) findViewById(R.id.editText);
// Store EditText in Variable
final String str = inputTxt.getText().toString();
//Sets the buttons
Button Button1 = (Button)findViewById(R.id.button);
Button Button2 = (Button)findViewById(R.id.button2);
Button Button3 = (Button)findViewById(R.id.button3);
Button Button4 = (Button)findViewById(R.id.button4);
Button1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN ) {
getSizeandPressure(event);
pressTime = System.nanoTime();
}
if(event.getAction() == MotionEvent.ACTION_UP) {
releaseTime = System.nanoTime();
pressDuration = releaseTime - pressTime;
pressTimeArray[i] = pressDuration;
i++;
Log.d("Press duration", "time: " + pressDuration);
}
return false;
}
});
Button2.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN ) {
getSizeandPressure(event);
pressTime = System.nanoTime();
betweenDuration = pressTime - releaseTime;
betweenTimeArray[i] = betweenDuration;
Log.d("Time between B1 and B2", "time: " + betweenDuration);
}
if(event.getAction() == MotionEvent.ACTION_UP) {
releaseTime = System.nanoTime();
pressDuration = releaseTime - pressTime;
pressTimeArray[i] = pressDuration;
i++;
Log.d("Press duration", "time: " + pressDuration);
}
return false;
}
});
Button3.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN ) {
getSizeandPressure(event);
pressTime = System.nanoTime();
betweenDuration = pressTime - releaseTime;
betweenTimeArray[i] = betweenDuration;
Log.d("Time between B2 and B3", "time: " + betweenDuration);
}
if(event.getAction() == MotionEvent.ACTION_UP) {
releaseTime = System.nanoTime();
pressDuration = releaseTime - pressTime;
pressTimeArray[i] = pressDuration;
i++;
Log.d("Press duration", "time: " + pressDuration);
}
return false;
}
});
Button4.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN ) {
getSizeandPressure(event);
pressTime = System.nanoTime();
betweenDuration = pressTime - releaseTime;
betweenTimeArray[i] = betweenDuration;
Log.d("Time between B3 and B4", "time: " + betweenDuration);
}
if(event.getAction() == MotionEvent.ACTION_UP) {
releaseTime = System.nanoTime();
pressDuration = releaseTime - pressTime;
pressTimeArray[i] = pressDuration;
i++;
session_number++;
Log.d("Press duration", "time: " + pressDuration);
// Writes the data to a text document stored in documents on the phone.
String filename = str + "_" + Text; //username
String string = sizeArray[0] + " " + sizeArray[1] + " " + sizeArray[2] + " " + sizeArray[3] + ", " +
pressureArray[0] + " " + pressureArray[1] + " " + pressureArray[2] + " " + pressureArray[3] + ", " +
pressTimeArray[0] + " " +pressTimeArray[1] + " " + pressTimeArray[2] + " " + pressTimeArray[3] + ", " +
betweenTimeArray[0] + " " + betweenTimeArray[1] + " " + betweenTimeArray[2] + ";" + "\n";
if(isExternalStorageWritable())
{
File file = getAlbumStorageDir(filename);
if (file.exists ()) file.delete ();
try {
FileOutputStream outputStream = new FileOutputStream(file);
outputStream = openFileOutput(filename, Context.MODE_PRIVATE); //The output Stead!
outputStream.write(string.getBytes());
if(10 == session_number)
{
outputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
i = 0;
}
return false;
}
});
}
@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);
}
public void getSizeandPressure(MotionEvent e)
{
float size = e.getSize();
float pressure = e.getPressure();
sizeArray[i] = size;
pressureArray[i] = pressure;
Log.d("Button_", "Size: " + size + " Pressure: " + pressure);
}
/* Checks if external storage is available for read and write */
public boolean isExternalStorageWritable(){
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}
public File getAlbumStorageDir(String albumName) {
// Get the directory for the user's public pictures directory.
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOCUMENTS), albumName);
if (!file.mkdirs()) {
Log.e("LOG_TAG", "Directory not created");
}
return file;
}
}
答案 0 :(得分:0)
您是否在AndroidManifest.xml中声明了您的权限? 像
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
或者您可以通过错误日志来显示更多信息。