Passing a String to other activity did not work - java

So I have a textEdit field in MainActivity2. And I will pass the String of it to MainActivity8. You go to MainActivity8 if you click a Button.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2activity);
((Button) findViewById(R.id.weiter)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity2.this, MainActivity8.class);
EditText et1 = (EditText) findViewById(R.id.editText2);
String Link1 = et1.getText().toString();
EditText et = (EditText) findViewById(editText1);
String Kat1 = et.getText().toString();
if (Link1.matches("") || Kat1.matches("")){
et.setHintTextColor(RED);
et1.setHintTextColor(RED);
}
else {
startActivity(i);
}
Intent intent = new Intent(MainActivity2.this, MainActivity8.class);
intent.putExtra("Kate1", Kat1);
}
});
And MainActivity8 looks like this:
public class MainActivity8 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main8);
Intent intent = getIntent();
String Kategorie1 = intent.getExtras().getString("Kategorie1");
TextView tv = (TextView) findViewById(R.id. textView2);
tv.setText(Kategorie1);
}
}
So everytime when I click the Button my App is crashing. And i don't know whats wrong. I get this from the Android Monitor:
12-30 11:45:29.046 29591-29591/com.example.luca.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.luca.myapplication, PID: 29591
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.luca.myapplication/com.example.luca.myapplication.MainActivity8}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
Can you give me a tip whats wrong?

at first you are send your data using "Kate1" keyword
intent.putExtra("Kate1", Kat1);
and fetching data using key "Kategorie1" which is different
change
intent.getExtras().getString("Kategorie1");
to
intent.getExtras().getString("Kate1");

#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2activity);
((Button) findViewById(R.id.weiter)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText et1 = (EditText) findViewById(R.id.editText2);
String Link1 = et1.getText().toString();
EditText et = (EditText) findViewById(editText1);
String Kat1 = et.getText().toString();
if (Link1.matches("") || Kat1.matches("")){
et.setHintTextColor(RED);
et1.setHintTextColor(RED);
}
else {
Intent intent = new Intent(MainActivity2.this, MainActivity8.class);
intent.putExtra("Kate1", Kat1);
startActivity(intent );
}
}
});
and MainActivity8.class
public class MainActivity8 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main8);
String Kategorie1 =getIntent().getStringExtra("Kate1");
TextView tv = (TextView) findViewById(R.id. textView2);
tv.setText(Kategorie1);
}
}
You start intent i but not put extra value in intent i you put value Intent "intent".

your key is "Kate1" and on another activity you used "Kategorie1".
put same key brother.

check null before getString
Bundle bundle= getIntent().getExtras();
if (bundle!= null) {
link = bundle.getString("Kate1");
}

getIntent().getStringExtra("your putString value"));
This will work.

Actually the intent with which you are starting the activity ,in that intent's bundle you are not adding the string to be sent.
from your code,you are creating an intent i like this
Intent i = new Intent(MainActivity2.this, MainActivity8.class);
and starting the activity using that intent "i" , but you are not putting the string in that intent object.
so just your proper code should be like shown below :
((Button) findViewById(R.id.weiter)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity2.this, MainActivity8.class);
EditText et1 = (EditText) findViewById(R.id.editText2);
String Link1 = et1.getText().toString();
EditText et = (EditText) findViewById(editText1);
String Kat1 = et.getText().toString();
if (Link1.matches("") || Kat1.matches("")){
et.setHintTextColor(RED);
et1.setHintTextColor(RED);
}
else {
i.putExtra("Kate1",Kat1);
startActivity(i);
}
//remove this intent,as not required
}
});
and in the MainActivity8 , try to get the string with the same key Kate1
intent.getExtras().getString("Kate1");

Related

Why is the bundle always null in spite of key being present?

The code below is intended to pass the data, using bundle, collected on signing up in Signup.java to ViewProfile.java which displays the data. On checking for a key in the bundle, it returns true, however, the bundle is null when checked in ViewProfile.java. Help will b appreciated.
Signup.java
public class Signup extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
final EditText n=(EditText)findViewById(R.id.editText3);
final EditText u=(EditText)findViewById(R.id.editText4);
final EditText p=(EditText)findViewById(R.id.editText5);
final EditText c=(EditText)findViewById(R.id.editText6);
Button s=(Button)findViewById(R.id.button4);
final Userdatabase udb=new Userdatabase(this);
s.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
String name=n.getText().toString();
String email=u.getText().toString();
String password=p.getText().toString();
String phone=c.getText().toString();
boolean b=udb.insertuser(name,email,password);
if(b==true) {
Intent i = new Intent(Signup.this, MainActivity.class);
Bundle bundle=new Bundle();
bundle.putString("NAME",name);
bundle.putString("ID",email);
bundle.putString("PHONE",phone);
i.putExtras(bundle);
startActivity(i);
}
else
Toast.makeText(getApplicationContext(),"Please try again",Toast.LENGTH_SHORT).show();
}
});
}
}
ViewProfile.java
public class ViewProfile extends AppCompatActivity {
String name,username,contact,profession;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_profile);
Intent intent=getIntent();
Bundle b=intent.getExtras();
if(b!=null) {
name = b.getString("NAME");
username = b.getString("ID");
contact = b.getString("PHONE");
TextView tv1=(TextView)findViewById(R.id.textView17);
TextView tv2=(TextView)findViewById(R.id.textView18);
TextView tv3=(TextView)findViewById(R.id.textView19);
tv1.setText(name);
tv2.setText(username);
tv3.setText(contact);
}
ImageView img=(ImageView)findViewById(R.id.imageView4);
img.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent i=new Intent(ViewProfile.this,Profile.class);
startActivity(i);
}
});
}
}
You are calling the wrong activity in the SignUp activity:
if(b==true) {
Intent i = new Intent(Signup.this, MainActivity.class);//problem here
You have set the intent as MainActivity.class and sending data to MainActivity and not to ViewProfile activity.
Change to this in SignUp activity:
if(b==true) {
Intent i = new Intent(Signup.this, ViewProfile.class);

Passing EditText integer to TextView via Intent app crashes after clicking (button) three

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"));
}

Passing value from one activity to another doesn't work

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();
}
}

pass two strings from one activity to another

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");

How can I pass data from one view to another in android?

I have two views named: first_view and second_view.
The first view consists of a button and an editable text view. The second view consists of a single text view.
In my first view, I want to put a number in the datable text view. As I click the button, it should display the number in the second view.
How can I code Java classes for the two views?
I am assuming that you want to setText within the same Activity, if not so then tell me, Ill change my answer.
Here is what you have to do.
public class MyActivity extends Activity {
Button btn;
EditText et;
TextView tv;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button)findViewById(R.id.yourbtnID);
et = (EditText) findViewById(R.id.yourEtID);
tv = (TextView) findViewById(R.id.yourtvID);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String myText = et.getText().toString();
tv.setText(myText);
}
});
}
}
If you want to pass text between two Activities then use Intent.
In your current Activity do this.
Intent i = new Intent(YourCurrentActivity.this, YourNextActivity.class);
String str = yourEditText.getText().toString();
i.putExtra("edittextvalue" , str);
startActivity(i);
Then in next Activity do this..
Bundle extras = getIntent().getExtras();
String myEtText;
if (extras != null) {
myEtText = extras.getString("edittextvalue");
yourTextView.setText(myEtText);
}
if Two Views in the same Activity , you can do that
Button btn;
EditText txtInput;
TextView txtShow;
//btn=firstView.findViewWithTag(tag)
btn=firstView.findViewById(R.id.**);
txtInput=firstView.findViewById(R.id.**);
txtShow=secondView.findViewById(R.id.**);
btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
String input=txtInput.getText().toString();
txtShow.setText(input);
}
});
if you have Two Activity :
Button btn;
EditText txtInput;
String VALUE_KEY="show";
private void test()
{
btn=(Button)findViewById(R.id.**);
txtInput=(Button)findViewById(R.id.**);
btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
String input=txtInput.getText().toString();
Intent intent=new Intent(this, AnotherActivity.Class);
intent.putExtra(VALUE_KEY, input);
}
});
}
On the AnotherActivity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent=this.getIntent();
String value=intent.getStringExtra(VALUE_KEY);
TextView view=(TextView)findViewById(R.id.txt);
view.setText(value);
}
Try this,
put your value in String: String et_str = EditText.getText().toString();
When you call the other intent,
Intent i = new Intent(first_view .class, second_view.class);
i.putExtra("REF",et_str);
StartActivity(i);
In The Second View, get this value using getExtra()

Categories