0

I am having a problem when I press my button to store my EditText info to a string the app crashes and I can't find any problem's, am I setting something up wrong and I just don't see it?

public class Tab5 extends Activity {

Button btnGo ;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab5);

    btnGo = (Button) findViewById(R.id.button1);


    btnGo.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) { 


            // TODO Auto-generated method stub
            EditText userid = (EditText) findViewById(R.id.editText1);
            userid.setText(2);
            Intent intent = new Intent(Tab5.this, Tab1.class);
            userid.getText().toString();
            startActivity(intent);
                        }
                    });

}
}
bumbob
  • 69
  • 7
  • Please have a look at [this question](http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this). – nhaarman May 09 '14 at 19:10
  • EditText are meant to get input from the user. So thers no point setting a value to it. First please instantiate it outside your onClick() then on the button click get the text entered by userid.getText().toString(); which you can use later. It isnt being used at the moment. – Atul O Holic May 09 '14 at 19:14
  • @AtulOHolic what am i doing wrong that isn't getting the text when the button is clicked – bumbob May 09 '14 at 19:19
  • setText() will take String, however 2 is an int so you will either have to do String.valueOf(2) or "2". But I m curious to know why you wanna do that if you yourself want to setText then you directly do it, why using an EditText? – Atul O Holic May 09 '14 at 19:29
  • @AtulOHolic i am running a tabhosted webview app with a different webview in every tab, however all of the webview url's end with ?id=* and i want what ever they type in to be the value of "*" and have 2 as the default load, what would be the easiest way to do this? – bumbob May 09 '14 at 19:33

4 Answers4

1

If you do

userid.setText(2);

Android will think of that int ad a resourceId, and will not find it in the R.java.

So just remember to pass always String to the setText method

userid.setText(String.valueOf(2));

or

userid.setText(""+2);
Enrichman
  • 11,157
  • 11
  • 67
  • 101
0

use

 userid.setText("2");

instead of

 userid.setText(2);
Santosh Kathait
  • 1,444
  • 1
  • 11
  • 22
  • ok so in my app i have 5 different webviews in different tab's all of the url's end with ?id= and i want the id to equal what i type in, on click of the button i want it to refresh all of the webviews but now when i click the button (that fixed it by the way thanks) it just opens a new webview and gets rid of my tabhost? – bumbob May 09 '14 at 19:08
  • Please see this for reference - http://stackoverflow.com/questions/13542982/android-webview-stay-in-app – Atul O Holic May 09 '14 at 19:16
  • 1
    If the answer help you vote for them it might help other too and anyway I am not getting what exactly you want...kindly put some code – Santosh Kathait May 09 '14 at 19:18
0

You are setting a int to an EditText. Convert the int to String. Try this:

Use +, the string concatenation operator:

ed = (EditText) findViewById (R.id.box);
int x = 10;
ed.setText(""+x);

or use String.valueOf(int):

ed.setText(String.valueOf(x));

or use Integer.toString(int):

ed.setText(Integer.toString(x))
Community
  • 1
  • 1
0

You can have ?id= assigned to a variable as

public final static String URL = "outputapps.com/build/infoview.php?id=";

and then can concatenate the EditText value but as you said you need 2 to be the default we will have to check EditText first if it contains any text or not. DO this is Tab5

Intent intent = new Intent(Tab5.this, Tab1.class);
String edit = userid.getText().toString();
String myURL = null;
if(edit != null && edit.length > 0){
    myURL = edit;
}else{
    myURL = "2";
}
intent.putExtra("EXTRA_URL", myURL);
startActivity(intent);

then for loading the URL do this in your Tab1,

String myURL = getIntent().getStringExtra("EXTRA_URL");
webView.loadUrl(YourActivity.URL + myURL);

I know the above can be written in fewer lines but I hope this is clear. Please let me know if you need further explanation.

Atul O Holic
  • 6,692
  • 4
  • 39
  • 74
  • the url is in another activity how would i use that in this setting private WebView webView; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.tab1); webView = (WebView) findViewById(R.id.webView1); webView.getSettings().setJavaScriptEnabled(true); Intent intent = getIntent(); webView.loadUrl("http://outputapps.com/build/infoview.php?id="); webView.setWebViewClient(new MyAppWebViewClient()); } }' – bumbob May 09 '14 at 20:03
  • you can pass the url as an Intent extra and incase it is fixed i.e. always going to be same then you can create it as a static String and access directly as Classname.url wherever you want. Keep public access in this case. See I have edited the URL above is in the Activty you want. – Atul O Holic May 09 '14 at 20:05
  • i keep messing up or something, thank you so much for all this help, i have no idea why this isnt working here are some screen shots of the code http://gyazo.com/dcf4e7296500a101f787c31306799019 that is where the webview is that needs to pull the variable and this is where the edittext is http://gyazo.com/710aefbd3d6948bdc8392bc777cde55a – bumbob May 09 '14 at 20:23
  • Please don't kill me for choosing wrong variable name. final is a keyword and hence the issue, change it to something else like final_URL or something. :) – Atul O Holic May 10 '14 at 03:57
  • I have edited my answer to further explain how we can pass your String from Tab5 to Tab1 via an intent. – Atul O Holic May 10 '14 at 04:12
  • if you could please further help me i ran into another problem http://stackoverflow.com/questions/23638780/error-reloading-webviews-in-a-tabhost-with-a-button – bumbob May 13 '14 at 18:30