Toast Message不工作android

时间:2014-11-06 06:34:57

标签: android android-toast

我有2个类BlockIdActivity.java和ScanWifi.java。我在BlockIdActivity.java文件中有2个按钮,我可以看到我在那里定义的吐司。但是,我无法看到我在ScanWifi.Java类中定义的按钮的吐司。

以下是BlockIdActivity.java的代码

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;


public class BlockIdActivity extends ActionBarActivity {

private ImageButton mUpButton;
private ImageButton mDownButton;
private TextView mBlock_Id_Field;
int counter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_block_id);

    mUpButton = (ImageButton)findViewById(R.id.arrow_up);
    mDownButton = (ImageButton)findViewById(R.id.arrow_down);
    mBlock_Id_Field = (TextView)findViewById(R.id.BlockIdField);


    mUpButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (mBlock_Id_Field.getText().toString().trim().equals(""))
         {
                counter = 1;
                mBlock_Id_Field.setText(String.valueOf(counter));
         }
            else {
                counter = Integer.valueOf(mBlock_Id_Field.getText().toString().trim());
                counter++;
                mBlock_Id_Field.setText(String.valueOf(counter));
            }

        }
        });

    mDownButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            int counter = Integer.valueOf(mBlock_Id_Field.getText().toString().trim());
        //  boolean emptyfield = mBlock_Id_Field.getText().toString().equals("");  

            if(counter <=1 ){
                Toast.makeText(BlockIdActivity.this,
                        R.string.negative_blockid_toast,
                        Toast.LENGTH_SHORT).show();
                counter = 1;
                mBlock_Id_Field.setText(String.valueOf(counter));
            }else {
                counter--;
            mBlock_Id_Field.setText(String.valueOf(counter));
            }

        }
    });
    }

}

和ScanWifi.java的代码

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class ScanWifi extends ActionBarActivity {

    private Button mScanWifiButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_block_id);

        mScanWifiButton = (Button)findViewById(R.id.ScanWifiButton);
        mScanWifiButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Toast.makeText(getBaseContext(),
                        R.string.ScanWifi_toast,
                        Toast.LENGTH_SHORT).show();

            }

    });

    }

}

这是strings.xml文件:

<resources>

    <string name="app_name">SnifferTrain</string>
    <string name="BlockId">BlockId</string>
    <string name="ScanWifi">ScanWifi</string>
    <string name="ScanWifi_toast">ScanWifi Button Pressed</string>
    <string name="negative_blockid_toast">Block Id Field Cannot Be Less Than 1 or Empty.Setting BlockID To 1</string>
    <string name="action_settings">Settings</string>

</resources>

有人可以解释为什么当我按下ScanWifi按钮时ScanWifi_toast没有显示。我已经尝试将toast的上下文更改为ScanWifi.this或getApplicationContext(),但它似乎不起作用。我没有得到任何编译错误。请帮忙

7 个答案:

答案 0 :(得分:1)

我认为你的字符串文件存在问题。做其他方式:

Toast.makeText(ScanWifi.this,getResource().getString(R.string.string.ScanWifi_toast),Toast.LENGTH_SHORT).show();

答案 1 :(得分:1)

您的ScanWifi活动代码未运行。因此,点击监听器没有注册到屏幕上的按钮,因此看不到烤面包。

由于您的所有活动都使用相同的内容视图布局,因此您应该将findViewById()setOnClickListener()移至主要活动。

要启动其他活动,请使用Intent,例如

startActivity(new Intent(context, ActivityName.class));

答案 2 :(得分:0)

将此用于Toast消息

Toast.makeText(getBaseContext(),
                    getResource().getString(R.string.ScanWifi_toast),
                    Toast.LENGTH_SHORT).show();

答案 3 :(得分:0)

应该是:

Toast.makeText(getBaseContext(),getResource().getString(R.string.ScanWifi_toast), Toast.LENGTH_SHORT).show();

由于R.string.ScanWif i_toast不会自动返回您放入XML的String,因此只会返回id

答案 4 :(得分:0)

您应该使用getString(R.string.ScanWifi_toast)代替直接使用R.string.ScanWifi_toast

答案 5 :(得分:0)

试试这个

Toast.makeText(context, context.getString(R.string.ScanWifi_toast), Toast.LENGTH_LONG).show();

答案 6 :(得分:0)

您可以使用此

Toast.makeText(getBaseContext(),"xyz",Toast.LENGTH_SHORT).show();

Toast.makeText(Activity.this,string,Toast.LENGTH_SHORT).show();