So... My problem is that when I try to send Intent putExtra , get it in the new class and then put it into a TextView it returns null...
Here is my code:
final String phonenum = text.getText().toString();
Intent i = new Intent(sms_verification.this, sms_verification_two.class);
i.putExtra("num", num);
i.putExtra("phonenum" , phonenum);
startActivity(i);
And then the "sms_verification_two"
Button next;
EditText code;
TextView phone;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sms_verification2);
next = (Button)findViewById(R.id.button);
code = (EditText)findViewById(R.id.editText2);
phone = (TextView)findViewById(R.id.textView);
String phonenum = getIntent().getStringExtra("phonenum");
String num = getIntent().getStringExtra("num");
phone.setText(phonenum);
Try using this approach on your sms_verification_two activity
String phonenum = "";
String num = "";
Bundle extras = getIntent().getExtras();
if (extras != null) {
phonenum = extras.getString("phonenum");
num = extras.getString("num");
}
phone.setText(phonenum + num);
Make sure the data you put in the intent isn't null. The code itself looks fine to me.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button goToNextActivity=(Button) findViewById(R.id.bSendID);
final EditText etNum=(EditText) findViewById(R.id.etNumID);
final EditText etPhoneum=(EditText) findViewById(R.id.etPhoneumID);
goToNextActivity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String textFromEtNum=etNum.getText().toString();
String textFromEtPhoneum=etPhoneum.getText().toString();
Intent i = new Intent(getApplicationContext(), secondActivity.class);
i.putExtra("num", textFromEtNum);
i.putExtra("phonenum" , textFromEtPhoneum);
startActivity(i);
}
});
}
}
second activity:
public class secondActivity extends AppCompatActivity {
Button next;
EditText code;
TextView phone;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
next = (Button)findViewById(R.id.button);
code = (EditText)findViewById(R.id.editText2);
phone = (TextView)findViewById(R.id.textView);
String phonenum = getIntent().getStringExtra("phonenum");
String num = getIntent().getStringExtra("num");
phone.setText(phonenum + " " + num);
}
}
First Activity
**if your text variable TextView or EditText then write**
String phonenum = text.getText().toString();
Intent i = new Intent(sms_verification.this, sms_verification_two.class);
i.putExtra("nums", num);
i.putExtra("phonenums" , phonenum);
startActivity(i);
Second Activity
public class sms_verification_two extends AppCompatActivity {
Button next;
EditText code;
TextView phone;
String phonenumss = "",numss = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
next = (Button)findViewById(R.id.button);
code = (EditText)findViewById(R.id.editText2);
phone = (TextView)findViewById(R.id.textView);
if(getIntent().getStringExtra("phonenums") != null){
phonenumss = getIntent().getStringExtra("phonenums");
}
if(getIntent().getStringExtra("nums") != null){
numss = getIntent().getStringExtra("nums");
}
phone.setText(" " +phonenumss + " " + numss);
}
}
Related
I want sent the content of the variable indicatorpositionstart of main2Activity to mainActivity. I used the solution "extra", but does not work.
What's the problem?
public class Main2Activity extends AppCompatActivity {
private Button imageValidate;
int indicatorPositionStart = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
imageValidate = findViewById(R.id.buttonValidate);
imageValidate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent2 = new Intent(Main2Activity.this, MainActivity.class);
intent2.putExtra("indicatorPositionStartt", indicatorPositionStart );
startActivity(intent2);
}
});
}
public class MainActivity extends AppCompatActivity{
int id = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.textView3);
if (id != 0) {
int id = getIntent().getExtras().getInt("indicatorPositionStartt", 0);
String indicatorPositionStart = String.valueOf(id);
textView.setText(indicatorPositionStart);
}
}
}
Thank you in advance.
The problem is that you are checking the value before you get them from the extras, see this lines on the second activity:
// you are using it here, before getting the value
if (id != 0) {
int id = getIntent().getExtras().getInt("indicatorPositionStartt", 0);
String indicatorPositionStart = String.valueOf(id);
textView.setText(indicatorPositionStart);
}
}
Another problem on the MainActivity is that you are not filling in the id field.
I fixed it, just replace this one with your second activity:
public class MainActivity extends AppCompatActivity{
int id = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.textView3);
// get the value first
id = getIntent().getExtras().getInt("indicatorPositionStartt", 0);
// then use it
if (id != 0) {
String indicatorPositionStart = String.valueOf(id);
textView.setText(indicatorPositionStart);
}
}
}
In your MainActivity, remove this check if (id !=0 ) because activity started you set the value of id =0 so if (id !=0 ) this if is never true.
For Sending Data:
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("arg", YourData);
startActivity(intent);
For Receiving Data:
String data = getIntent().getExtras().getString("arg");
Update
remove if( id != 0) and use if(getIntent().hasExtra("indicatorPositionStartt"))
like this:
if (getIntent().hasExtra("indicatorPositionStartt")) {
int id = getIntent().getExtras().getInt("indicatorPositionStartt", 0);
String indicatorPositionStart = String.valueOf(id);
textView.setText(indicatorPositionStart);
}
The SharedPreferences doesn't save my username while I don't get any error. I have checked my method with some tutorials and I can't find what's wrong. I have tried both commit() and 'apply()' despite that can't make here a difference.
Someone who finds the error?
my code:
public class MainActivity extends AppCompatActivity implements AsyncResponse, View.OnClickListener {
EditText etUsername, etPassword;
ImageButton btnLogin;
String username;
CheckBox ckbx_login;
public static final String PREFS_NAME = "MyPrefsFile";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
etUsername = (EditText) findViewById(R.id.etUsername);
etPassword = (EditText) findViewById(R.id.etPassword);
btnLogin = (ImageButton) findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(this);
username = etUsername.getText().toString();
}
#Override
public void processFinish(String result) {
if(result.equals("success")) {
SharedPreferences.Editor editor = getApplicationContext().getSharedPreferences(PREFS_NAME,0).edit();
editor.putString("loginname",username);
editor.commit();
Intent in = new Intent(this, SubActivity.class);
String naam2= etUsername.getText().toString();
in.putExtra("naam2",naam2);
startActivity(in);
}
else{
Toast.makeText(this,"Inloggen mislukt", Toast.LENGTH_LONG).show();
}
}
#Override
public void onClick(View view) {
HashMap postData = new HashMap();
postData.put("mobile", "android");
postData.put("txtUsername", etUsername.getText().toString());
postData.put("txtPassword", etPassword.getText().toString());
PostResponseAsyncTask task = new PostResponseAsyncTask(this, postData);
task.execute("http://exemple.com");
}
And the second class:
public static final String PREFS_NAME = "MyPrefsFile";
String name;
TextView msg_welcome;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_sub);
msg_welcome = (TextView) findViewById(R.id.txtVwelcome);
SharedPreferences prefs = this.getSharedPreferences(PREFS_NAME,0);
String restored_loginname = prefs.getString("loginname",null);
if(restored_loginname != null) {
name = prefs.getString("name", "No name defined");
}
Toast.makeText(getApplicationContext(),name,Toast.LENGTH_LONG).show();
}
public void scan(View View){
zXingScannerView = new ZXingScannerView(getApplicationContext());
setContentView(zXingScannerView);
zXingScannerView.setResultHandler(this);
zXingScannerView.startCamera();
}
#Override
protected void onPause() {
super.onPause();
zXingScannerView.stopCamera();
}
#Override
public void handleResult(Result result) {
// Toast.makeText(getApplicationContext(),result.getText(),Toast.LENGTH_LONG).show();
Intent i = new Intent(getApplicationContext(), Avt_form2.class);
String qrcode = result.getText();
i.putExtra("qrcode",qrcode);
startActivity(i);
}
Btw I know that Postdata is outdated. I am looking to replace it.
Issue : currently your taking the value of username in onCreate when you don't have the user input so
username = etUsername.getText().toString();
should be executed after user input is available like in onclick
or
#Override
public void processFinish(String result) {
if(result.equals("success")) {
SharedPreferences.Editor editor = getApplicationContext().getSharedPreferences(PREFS_NAME,0).edit();
username = etUsername.getText().toString();
//^^^^^^^^^
editor.putString("loginname",username);
editor.commit();
Note : name was not previously used to store any value , i guess wanted to use name2
I have been trying to get a string to go from one java class to another for a project of mine. The code I have been experimenting with is not working. When I press the button, I know it opens the other Java class because it creates the other layout, but it doesn't show the string. Please help me.
First Java Class:
public class MainActivity extends AppCompatActivity {
private Button button;
Context context;
private EditText editText;
String number = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
editText = (EditText) findViewById(R.id.editText);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (editText.getText().toString() != null) {
String value = "value";
Intent secondscreenIntent = new Intent(context, SecondScreenJ.class);
secondscreenIntent.putExtra("Number", editText.getText().toString());
startActivity(secondscreenIntent);
}
}
});
}
}
Second Java Class:
public class SecondScreenJ extends Activity {
String number = null;
private TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondscreen);
textView = (TextView) findViewById(R.id.textView);
Bundle extras = getIntent().getExtras();
if (extras != null){
number = extras.getString("number");
}
textView.setText(number);
}
}
you are putting "Number" as key but, in your second activity you are trying to retrieve "number"
so change number to Number and it will work.
Its case Sensitive.
Don't make your keys hard-coded in that way.
Just declare public static variable in MainActivity and use it from SecondScreenJ
public class MainActivity extends AppCompatActivity {
private Button button;
Context context;
private EditText editText;
public static String NUMBER_KEY = "Number";
String number = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
editText = (EditText) findViewById(R.id.editText);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (editText.getText().toString() != null) {
String value = "value";
Intent secondscreenIntent = new Intent(context, SecondScreenJ.class);
secondscreenIntent.putExtra(NUMBER_KEY , editText.getText().toString());
startActivity(secondscreenIntent);
}
}
});
}
}
Second Java Class:
public class SecondScreenJ extends Activity {
String number = null;
private TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondscreen);
textView = (TextView) findViewById(R.id.textView);
Bundle extras = getIntent().getExtras();
if (extras != null){
number = extras.getString(MainActivity.NUMBER_KEY);
}
textView.setText(number);
}
}
Beware when using the key for putting and getting the extras. They're case sensitive. Replace "number" with "Number" in your second activity.
I tried looking on other questions but nothing seems to work. Trying to pass the value stored in sUsername from activity Username.java to MainActivity.java (username).
This is Username.java:
public class Username extends ActionBarActivity implements View.OnClickListener {
EditText eUsername;
Button login;
String sUsername;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_username);
eUsername = (EditText) findViewById (R.id.username);
sUsername = eUsername.getText().toString();
login = (Button)findViewById(R.id.login);
login.setOnClickListener(this);
}
private void loginClick() {
Intent intent = new Intent(this, MainActivity.class );
intent.putExtra("containsUsername", sUsername); //first argument is the name of the string being passed
startActivity(intent);
}
public void onClick (View v) {
switch (v.getId()) {
case R.id.login:
loginClick();
break;
}
}
and this is MainActivity.java
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
String username = bundle.getString("containsUsername");
WebView listOfSongs = (WebView) findViewById (R.id.webview);
String url = "http://ws.audioscrobbler.com/2.0/?method=user.gettoptracks&user="+username+"&api_key=68f82cd7b37e7b23800c2025066531c9&format=json";
listOfSongs.loadUrl(url);
}
your loginCheck Method should be like this:
private void loginClick() {
sUsername = eUsername.getText().toString();
Intent intent = new Intent(this, MainActivity.class );
intent.putExtra("containsUsername", sUsername); //first argument is the name of the string being
startActivity(intent);
}
As in on create your edittext might be empty.
Please check your edittext is empty or not. Chnage your method longClick.
private void loginClick(){
if(!sUsername.isEmpty()) {
Intent intent = new Intent(this, MainActivity.class );
intent.putExtra("containsUsername", sUsername);
startActivity(intent);
} else {
Toast.makeText(Username.this, "Please enter username.", Toast.LENGTH_LONG).show();
}
}
I am working on a tic tac toe application for android. In the Two player section, I've created an activity which will ask the two players to enter their names. For that I've used two EditTexts. The problem is That my app force closes while starting the next activity. Here is my code:
//Activity 1:
EditText player1field,player2field;
Button startbutton;
Intent startbuttonintent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_options);
setupActionBar();
player1field = (EditText) findViewById(R.id.player1field);
player2field = (EditText) findViewById(R.id.player2field);
startbuttonintent = new Intent(this, Activity2.class);
startbutton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
String player1name = player1field.getText().toString();
String player2name = player2field.getText().toString();
startbuttonintent.putExtra("PLAYER1NAME",player1name);
startbuttonintent.putExtra("PLAYER2NAME",player2name);
startActivity(startbuttonintent);
}
});
}
this is activity 2
//Activity2
Intent startbuttonintent = getIntent();
TextView p1name,p2name;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_3m);
String player1name = startbuttonintent.getStringExtra("PLAYER1NAME");
String player2name = startbuttonintent.getStringExtra("PLAYER2NAME");
p1name = (TextView) findViewById(R.id.p1name);
p2name = (TextView) findViewById(R.id.p2name);
p1name.setText(player1name);
p2name.setText(player2name);
}
This code is not giving me any errors but my app force closes when I run it.
Please help me.
Thanks in advance.
Try this:
Activity 1:
EditText player1field,player2field;
Button startbutton;
Intent startbuttonintent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_options);
setupActionBar();
player1field = (EditText) findViewById(R.id.player1field);
player2field = (EditText) findViewById(R.id.player2field);
startbutton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
String player1name = player1field.getText().toString();
String player2name = player2field.getText().toString();
startbuttonintent = new Intent(this, Activity2.class);
startbuttonintent.putExtra("PLAYER1NAME",player1name);
startbuttonintent.putExtra("PLAYER2NAME",player2name);
startActivity(startbuttonintent);
}
});
}
You have call this method before onclick which is causing error.
And fetch the intent text like this:
//Activity2
TextView p1name,p2name;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_3m);
Bundle extras = getIntent().getExtras();
String player1name = extras.getString("PLAYER1NAME");
String player2name = extras.getString("PLAYER2NAME");
p1name = (TextView) findViewById(R.id.p1name);
p2name = (TextView) findViewById(R.id.p2name);
p1name.setText(player1name);
p2name.setText(player2name);
}
String intArray[] = {"one","two"};
Intent in = new Intent(this, B.class);
in.putExtra("my_array", intArray);
startActivity(in);
For Reading :
Bundle extras = getIntent().getExtras();
String[] arrayInB = extras.getStringArray("my_array");
Take yout getIntent method inside onCreate method
//Activity2
TextView p1name,p2name;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_3m);
Intent startbuttonintent = getIntent();
String player1name = startbuttonintent.getStringExtra("PLAYER1NAME");
String player2name = startbuttonintent.getStringExtra("PLAYER2NAME");
p1name = (TextView) findViewById(R.id.p1name);
p2name = (TextView) findViewById(R.id.p2name);
p1name.setText(player1name);
p2name.setText(player2name);
}
Move Intent startbuttonintent = getIntent(); to inside your onCreate() method
getIntent() is a method in Activity, so you can't call it in a static class level initialization. You must call it only after your Activity object is ready, like when in the onCreate() method
#Chinmay Dabke Please get the id of the button "startbutton"
by using
startbutton = (Button)findviewbyId(R.id.startbutton);
Try This....in Activity1
Bundle bundle = new Bundle();
bundle .putString("PLAYER1NAME",player1name);
bundle .putString("PLAYER2NAME",player2name);
intent1.putExtras(bundle);
in Activity2
Bundle b = getArguments();
String player1name = b.getString("PLAYER1NAME");
String player2name = b.getString("PLAYER2NAME");