-3

I have 2 classes BlockIdActivity.java and ScanWifi.java. I have 2 buttons in BlockIdActivity.java file and i am able to see my toast defined there. However i cant see the toast for the button i defined in ScanWifi.Java class.

Following is the code for 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));
            }

        }
    });
    }

}

and code for 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();

            }

    });

    }

}

This is the strings.xml file :

<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>

Can someone please explain why ScanWifi_toast does not show when i press ScanWifi Button. I have tried changing the context of the toast to ScanWifi.this or getApplicationContext() but it doesn't seem to work. I don't get any compilation errors. Please help

frogatto
  • 28,539
  • 11
  • 83
  • 129
srai
  • 1,023
  • 2
  • 14
  • 27
  • 1
    change `R.string.ScanWifi_toast` to `getResource().getString(R.string.ScanWifi_toast)` – Shayan Pourvatan Nov 06 '14 at 06:35
  • always try to define name in lowercase instead uppercase in String.xml more detail check : http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling – Haresh Chhelana Nov 06 '14 at 06:41
  • 1
    Despite all the answers so far, there's an overload of `Toast.makeText()` that accepts a string resource id and it works all right. The problem is somewhere else. Consider adding logging or a debugger breakpoint to your code to first confirm it is run, and if not, figure out why it is not run. – laalto Nov 06 '14 at 06:42
  • @laalto : Yes, i have tried the suggestions but i still do not see the toast. I will try what you have suggested. – srai Nov 06 '14 at 06:50
  • Looking at the two files, I see you say it works when you do `Toast.makeText(BlockIdActivity.this` but fails when you do `Toast.makeText(getBaseContext()`. Is this a hint? Do you get an error message in your LogCat? – Ken Y-N Nov 06 '14 at 06:57
  • If I had to guess, I'd say your `ScanWifi` activity has not been run at all. Note that your activities are both using the same layout but you're only assigning a click listener to the scan button in the other activity. – laalto Nov 06 '14 at 07:09
  • @KenY-N : I get an error saying Style contains key with bad entry: 0x01010479. I am not sure what this means. I have checked my ScanWifi_toast string and it seems to look ok? – srai Nov 06 '14 at 07:10
  • @laalto : Can you please elaborate on that? Do think it is problem with this mScanWifiButton.setOnClickListener(new View.OnClickListener()? If so then what is the solution? – srai Nov 06 '14 at 07:17
  • How are you lauching the `ScanWifi` activity in the first place? – laalto Nov 06 '14 at 07:42
  • @laalto : I am just executing the whole code as an android application? Is this what you mean? Do i have to call ScanWifi class in BlockIdActivity.java by a constructor or something?Sorry if this is silly. I am very new to java and android – srai Nov 06 '14 at 07:53

7 Answers7

1

I think the problem with your string file. Do like other way:

Toast.makeText(ScanWifi.this,getResource().getString(R.string.string.ScanWifi_toast),Toast.LENGTH_SHORT).show();
Pratik Dasa
  • 7,439
  • 4
  • 30
  • 44
1

Your ScanWifi activity code is not run. Hence the click listener is not registered to a button on screen and therefore the toast is not seen.

Since both your activities are using the same content view layout, you should probably move the findViewById() and setOnClickListener() to the main activity.

To launch other activities, use an Intent, e.g.

startActivity(new Intent(context, ActivityName.class));
laalto
  • 150,114
  • 66
  • 286
  • 303
  • If i move findViewById() and setOnClickListener() to the main activity then most of the code for ScanWifi.java class is moved to the mainactivity file? In that case i can just create another button along with the 2 which are already there and implement the same logic with it. The reason i put findViewById() and setOnClickListener() in a seperate ScanWifi.java class was to keep all the widgets separate, since scanwifi will become more complex in future. I just need it to work from within ScanWifi.java for now? Can you please show me how to start the ScanWifi activity? – srai Nov 06 '14 at 08:54
  • If you want another activity with another layout, you can do it, just use another layout name in `setContentView()`. Use the `startActivity()` with `ScanWifi.class` to start it. – laalto Nov 06 '14 at 09:00
  • I actually want to use the same layout for ScanWifi.class. Is it possible to use the same layout but start different activities for blockidactivity and scanwifi activity like i had attempted initially? – srai Nov 06 '14 at 09:23
0

Use this for Toast message

Toast.makeText(getBaseContext(),
                    getResource().getString(R.string.ScanWifi_toast),
                    Toast.LENGTH_SHORT).show();
frogatto
  • 28,539
  • 11
  • 83
  • 129
Naveen Tamrakar
  • 3,349
  • 1
  • 19
  • 28
0

It should be :

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

Because R.string.ScanWifi_toast dont automatically return the String you put in your XML, it only return the id.

Blaze Tama
  • 10,828
  • 13
  • 69
  • 129
0

you should use getString(R.string.ScanWifi_toast) in place of directly using R.string.ScanWifi_toast

frogatto
  • 28,539
  • 11
  • 83
  • 129
raj
  • 2,088
  • 14
  • 23
0

Try this

Toast.makeText(context, context.getString(R.string.ScanWifi_toast), Toast.LENGTH_LONG).show();
frogatto
  • 28,539
  • 11
  • 83
  • 129
manDroid
  • 1,125
  • 3
  • 13
  • 25
0

You can use this

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

or

Toast.makeText(Activity.this,string,Toast.LENGTH_SHORT).show();
Bo A
  • 3,144
  • 2
  • 33
  • 49
Amitsharma
  • 1,577
  • 1
  • 17
  • 29
  • You can take Ur String Values in side toast directly ... or Assign in String that value after that use in Toast... – Amitsharma Nov 06 '14 at 07:06