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.
Related
I am trying just to pass the int user input to the next class and print it, see that it works before continuing on using it or something else.
Home.java
start and exit button
public class Home extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button one = (Button) findViewById(R.id.b1);
one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
goToSecondActivity();
}
});
Button two = (Button) findViewById(R.id.b2);
two.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
private void goToSecondActivity() {
Intent i = new Intent(this, SelectNumberOfPlayers.class);
startActivity(i);
}
}
SelectNumberOfPlayers.java
Taking only the numbers from the input and passing it to StartGame.class
public class SelectNumberOfPlayers extends AppCompatActivity {
EditText numberOfPlayers;
Button three;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.enter_number_of_players);
three = (Button) findViewById(R.id.button3);
three.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
String txt = numberOfPlayers.getText().toString();
Intent i = new Intent(getApplicationContext(), StartGame.class);
i.putExtra("players", txt);
startActivity(i);
}
});
}
}
StartGame.java
Receiving Int and printing to TextView
public class StartGame extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start_game);
Intent i = getIntent();
TextView numOfPlayersVal = (TextView) findViewById(R.id.txt2);
numOfPlayersVal.setText(i.getStringExtra("Player number"));
}
}
Where is the error happening? I've set the input keyboard to take only numbers
1. you forgot to initialize youe editext first initialize it in your SelectNumberOfPlayers Activity
numberOfPlayers = (EditText) findViewById(R.id.numberOfPlayers);
2.
the key must be same to pass and receive data with intent
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start_game);
Intent i = getIntent();
TextView numOfPlayersVal = (TextView) findViewById(R.id.txt2);
numOfPlayersVal.setText(i.getStringExtra("players"));
}
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);
}
}
[Here is a screenshot of my output][1]
I am trying to sort the elements of an array.
I have stored the elements to the array from EditText1(ed) and I want to sort them and display them in EditText2.
I am done with storing them and displaying them, i wanted to sort them using Collections.sort(array); but it shows me that there is something wrong.
This is my code so far:
public class MainActivity extends AppCompatActivity {
List<EditText> allEds = new ArrayList<EditText>();
EditText ed,ed2;
RelativeLayout container;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed= (EditText)findViewById(R.id.editText);
ed2=(EditText)findViewById(R.id.editText2);
final Button b=(Button)findViewById(R.id.button);
container = (RelativeLayout)findViewById(R.id.rl);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
allEds.add(ed);
String[] strings = new String[allEds.size()];
for(int i=0; i < allEds.size(); i++){
strings[i] = allEds.get(i).getText().toString();
Log.e("My data", strings[i]);
ed2.setText(strings[i]);
}
}
});
[1]: http://i.stack.imgur.com/dowdE.jpg
I think this will probably work (replace the code in the onClick with this):
String[] strings = ed.getText().toString().split("\\r?\\n");
Arrays.sort(strings);
String output = TextUtils.join("\\n",strings);
ed2.setText(output);
may be something like this?
public class MainActivity extends AppCompatActivity {
List<String> allEds = new ArrayList<String>();
EditText ed,ed2;
RelativeLayout container;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed= (EditText)findViewById(R.id.editText);
ed2=(EditText)findViewById(R.id.editText2);
final Button b=(Button)findViewById(R.id.button);
container = (RelativeLayout)findViewById(R.id.rl);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String str = ed.getText().toString();
String lines[] = str.split("\\r?\\n");
Arrays.sort(lines);
ed2.setText(TextUtils.join("\n", lines));
}
});
I have search a lot in different sites to solve this problem but I can't till now.
I have a simple app with 50 articles, two buttons, previous and next.
All works fine till the last article 50 where the problem is that the user can't choose the previous button 49, only can load from the beginning.
Here is my code: MainActivity.java
public class MainActivity extends AppCompatActivity {
private TextView meditCenterText;
private Button mStartButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
meditCenterText = (TextView) findViewById(R.id.editCenterText);
mStartButton = (Button) findViewById(R.id.startButton);
meditCenterText.setMovementMethod(new ScrollingMovementMethod());
mStartButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String TextView = meditCenterText.getText().toString();
startStory(TextView);
}
});
}
private void startStory(String TextView) {
Intent intent = new Intent(this, StoryActivity.class);
intent.putExtra("TextView", TextView);
startActivity(intent);
}
#Override
protected void onResume() {
super.onResume();
meditCenterText.setText("..... ");
}}
StoryActivity.java
public class StoryActivity extends AppCompatActivity {
public static final String TAG = StoryActivity.class.getSimpleName();
private Story mStory = new Story();
private ImageView mImageView;
private TextView mTextView;
private Page mCurrentPage;
private String mName;
private Button mPreviousButton;
private Button mNextButton;
private ScrollView mScrollView;
private Button mStartButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_story2);
//Intent intent = getIntent();
//mName = intent.getStringExtra(getString(R.string.key_name));
if(mName == null){
mName = "";
}
Log.d(TAG, mName);
mImageView = (ImageView)findViewById(R.id.storyImageView);
mTextView = (TextView)findViewById(R.id.storyTextView);
mPreviousButton = (Button)findViewById(R.id.previousButton);
mNextButton = (Button)findViewById(R.id.nextButton);
mTextView.setMovementMethod(new ScrollingMovementMethod());
loadPage(0);
}
private void loadPage(int choice){
mCurrentPage = mStory.getPage(choice);
Drawable drawable = getResources().getDrawable(mCurrentPage.getImageId());
mImageView.setImageDrawable(drawable);
String pageText = mCurrentPage.getText();
//add the name if placeholder included.
//pageText = String.format(pageText,mName);
mTextView.setText(pageText);
if(mCurrentPage.isSingle()){
mPreviousButton.setVisibility(View.VISIBLE);
mNextButton.setText("Start From The Beginning");
mNextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mPreviousButton.setVisibility(View.VISIBLE);
loadPage(0);
}
});
}else{
mPreviousButton.setText(mCurrentPage.getChoices().getText1());
mNextButton.setText(mCurrentPage.getChoices().getText2());
mPreviousButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int previousPage = mCurrentPage.getChoices().getPreviousPage();
loadPage(previousPage);
}
});
mNextButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
int nextPage = mCurrentPage.getChoices().getNextPage();
loadPage(nextPage);
}
});
}}
}
Thanks for help.
It's probably the logic in your Page class. Have a proper look at it, otherwise from the android side everything is alright. One last thing, you should probably use a ViewPager for your app instead of changing the text every time you load a new oage
I have a class in which a string is taken from an input. I want to use the value of the input in a second class.
public class Incontrare extends ActionBarActivity {
public static String nome1=null;
public String variabileNome;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_incontrare);
Button button1 = (Button) findViewById(R.id.button1Nome);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
alertDia(); //takes the input from variabileNome
nome1 = variabileNome;
TextView textv1 = (TextView) findViewById(R.id.textView1);
textv1.setText(nome1);
}
});
public static String getNome1() {return nome1;}
}
}
And the second class:
public class IncontrarePersona1 extends ActionBarActivity {
String nome1=Incontrare.getNome1();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_incontrare_persona1);
b1=(Button)findViewById(R.id.play);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String toPrint = "There is " + nome1;
Toast.makeText(getApplicationContext(), toPrint, Toast.LENGTH_SHORT).show();
}
});
}
I tried the second class also in this way:
public class IncontrarePersona1 extends ActionBarActivity {
String nome1=Incontrare.getNome1();
public static String no1;
b1=(Button)findViewById(R.id.play);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Incontrare no1= new Incontrare();
String n1= no1.nome1;
String toPrint = "There is " + nome1;
Toast.makeText(getApplicationContext(), toPrint, Toast.LENGTH_SHORT).show();
}
});
}
Where is the mistake? Why do I get always null?
Any help is appreciated. Thanks :)
If you are jumping from activity to another use Bundle.
Bundle example -
Set value
Intent i = new Intent();
i.setClass(this, IncontrarePersona1.class);
i.putExtra("text", "some text");
startActivity(i);
Get Value:
Bundle extras = getIntent().getExtras();
if (extras != null)
{
String data= extras.getString("text");
//data is your param
}
Otherwise use static class with static variables as Will McG said.
I think the issue is that when the second Activity is brought into focus, the data in the previous Activity is lost.
Create a separate class to hold your static variables that both Activitys can access.
Edit:
Another method I use for passing data is the SharedPreferences if I'm worried about the entire application losing focus.