[英]Android: processing input in Timer to set in Textview
This native Braille app I am making has 6 different buttons that I would like to have every unique combination bring me different letters. 我正在制作的这款本机盲文应用程序具有6个不同的按钮,我希望每种独特的组合都能带给我不同的字母。
For example: I would like hitting button 1 to simple bring me the letter "A". 例如:我想按下按钮1来简单地给我带来字母“ A”。 Then hitting button 1 and button 2 continuously brings me the letter "C".
然后连续按一下按钮1和按钮2会给我带来字母“ C”。 I want every different button combination of these 6 buttons to bring me a seperate letter.
我希望这6个按钮的每个不同按钮组合能带给我一封单独的信。
In processInput()
I initiated map
but when I tried clicking the buttons "The app automatically stopped" 在
processInput()
我启动了map
但是当我尝试单击按钮“应用程序自动停止”时
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.keyboard);
Window window = this.getWindow();
window.addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
window.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
window.addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);
// Init GUI
txtMessage = (EditText) findViewById(R.id.txtMesssage);
Button buttonOne = (Button) findViewById(R.id.block1);
Button buttonTwo = (Button) findViewById(R.id.block2);
Button buttonThree = (Button) findViewById(R.id.block3);
Button buttonFour = (Button) findViewById(R.id.block4);
Button buttonFive = (Button) findViewById(R.id.block5);
Button buttonSix = (Button) findViewById(R.id.block6);
// Attached Click Listener
btnSend.setOnClickListener(this);
buttonOne.setOnClickListener(this);
buttonTwo.setOnClickListener(this);
buttonThree.setOnClickListener(this);
buttonFour.setOnClickListener(this);
buttonFive.setOnClickListener(this);
buttonSix.setOnClickListener(this);
}
@Override
public void onClick(View v) {
final Map<String, String> map = new HashMap<String, String>();
Timer processTimer = new Timer();
processTimer.schedule(new TimerTask() {
public void run() {
processInput();
}
private void processInput() {
// TODO Auto-generated method stub
map.put(getString(R.id.block1), "A");
map.put(getString(R.id.block1) + getString(R.id.block2), "C");
map.get(R.id.block1);
map.get(R.id.block1+R.id.block2);
}
}, 500); // Delay before processing.
processTimer.cancel();
processTimer.schedule(new TimerTask() {
public void run() {
processInput();
}
private void processInput() {
// TODO Auto-generated method stub
}
}, 500);
}
XML Layout: XML布局:
I can't seem to be getting it, I am getting errors in Timer
. 我似乎无法得到它,我在
Timer
遇到错误。
Logcat error: Logcat错误:
01-12 21:43:09.764: E/AndroidRuntime(20677): FATAL EXCEPTION: main
01-12 21:43:09.764: E/AndroidRuntime(20677): Process: info.androidhive.speechtotext, PID: 20677
01-12 21:43:09.764: E/AndroidRuntime(20677): java.lang.IllegalStateException: Timer was canceled
01-12 21:43:09.764: E/AndroidRuntime(20677): at java.util.Timer.scheduleImpl(Timer.java:558)
01-12 21:43:09.764: E/AndroidRuntime(20677): at java.util.Timer.schedule(Timer.java:456)
01-12 21:43:09.764: E/AndroidRuntime(20677): at info.androidhive.speechtotext.Braillekeyboard.onClick(Braillekeyboard.java:307)
01-12 21:43:09.764: E/AndroidRuntime(20677): at android.view.View.performClick(View.java:5225)
01-12 21:43:09.764: E/AndroidRuntime(20677): at android.view.View$PerformClick.run(View.java:21195)
01-12 21:43:09.764: E/AndroidRuntime(20677): at android.os.Handler.handleCallback(Handler.java:739)
01-12 21:43:09.764: E/AndroidRuntime(20677): at android.os.Handler.dispatchMessage(Handler.java:95)
01-12 21:43:09.764: E/AndroidRuntime(20677): at android.os.Looper.loop(Looper.java:148)
01-12 21:43:09.764: E/AndroidRuntime(20677): at android.app.ActivityThread.main(ActivityThread.java:5451)
01-12 21:43:09.764: E/AndroidRuntime(20677): at java.lang.reflect.Method.invoke(Native Method)
01-12 21:43:09.764: E/AndroidRuntime(20677): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
01-12 21:43:09.764: E/AndroidRuntime(20677): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
The original code: 原始代码:
Timer processTimer = new Timer().schedule(new TimerTask() {
public void run() { processInput(); }
}, 500); // Delay before processing.
processTimer.cancel();
processtimer.schedule(new TimerTask() {
public void run() { processInput(); }
}, 500);
You are trying to schedule()
on a cancelled
Timer. 您正在尝试在已
cancelled
计时器上schedule()
。 As per documentation , a cancelled Timer cannot be used again. 根据文档 ,已取消的Timer不能再次使用。 You need a new instance.
您需要一个新实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.