getApplicationContext()不适用于异步事件

时间:2010-03-04 08:19:27

标签: android

我想使用异步事件来加密输入到texbox中的文本,例如。按下按钮。它编译时没有错误,但按下按钮时没有任何反应。从我在论坛上收集的内容来看,我的背景是错误的。有人能帮我吗?以下是代码:

mSendButton = (Button)findViewById(R.id.button_send);
mSendButton.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        TextView view = (TextView)findViewById(R.id.edit_text_out);
        String message = view.getText().toString();
        if (message == "bla") {
            Toast.makeText(getApplicationContext(), "Bla was entered", Toast.LENGTH_LONG).show();
        }
        else {
            Toast.makeText(getApplicationContext(), "Bye ", Toast.LENGTH_SHORT).show();
        }
    }
});

2 个答案:

答案 0 :(得分:2)

试试这个:

mSendButton = (Button)findViewById(R.id.button_send);
mSendButton.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        TextView view = (TextView)findViewById(R.id.edit_text_out);
        String message = view.getText().toString();
        if (message == "bla") {
            Toast.makeText(getActivity(), "Bla was entered", Toast.LENGTH_LONG).show();
        }
        else {
            Toast.makeText(getActivity(), "Bye ", Toast.LENGTH_SHORT).show();
        }
    }
});

或:

mSendButton = (Button)findViewById(R.id.button_send);
mSendButton.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        TextView view = (TextView)findViewById(R.id.edit_text_out);
        String message = view.getText().toString();
        if (message == "bla") {
            Toast.makeText(getContext(), "Bla was entered", Toast.LENGTH_LONG).show();
        }
        else {
            Toast.makeText(getContext(), "Bye ", Toast.LENGTH_SHORT).show();
        }
    }
});

答案 1 :(得分:0)

您确定没有堆栈跟踪吗?

您的代码似乎是正确的,至少如果它在活动中。

建议:

  • 尝试使用getBaseContext()而不是getApplicationContext()
  • 寻找堆栈跟踪
  • 尝试使用Toast.makeText(MyActivityClass.this,“Bye”,Toast.LENGTH_SHORT).show();