intent getString error - android

I have created two classes inside other class . inside these two classes i have used the Intent class.
Intent intent = new Intent(getApplicationContext(), DurationsActivity.class );
intent.putExtra("to",mydate);
in parent class i used this code to retrieve the intent value .
String to = getIntent().getExtras().getString("to");
String from = getIntent().getExtras().getString("from");
my logcat retriev this
java.lang.NullPointerException

mydate is like a key to identify the value in intent.
Declare mydate :-
String mydate,mydate1;
Intent intent = new Intent(ActivityName.this, DurationsActivity.class ); //Be Specific
intent.putExtra("to",mydate);
intent.putExtra("from",mydate1);
Than in DurationActivity.class
You can use your usual code in onCreate to get the contents from previous activity:-
String to = getIntent().getExtras().getString("to");
String from = getIntent().getExtras().getString("from");

Try, hope it will work
Intent intent = new Intent(YourCurrentActivity.this, DurationsActivity.class );
intent.putExtra("to",""+mydate);
String to = getIntent().getExtras().getString("to");
String from = getIntent().getExtras().getString("from");

Related

How to use the string from editText as display textview in another activity?

I want to use the string which is typed in edittext as textview in a new activity.
sub6 = (EditText)findViewById(R.id.editText16);
txt6 = (TextView)findViewById(R.id.textView25);
subject = (String) sub6.getText().toString();
txt6.setText(getResources().getString(R.id.editText16));
I have used this code. But is of no use. Can you please help me?
You can send the string using Bundle
Intent i=new Intent(A.this, B.class);
Bundle b = new Bundle();
b.putString("name", sub6.getText().toString());
i.putExtras(b);
startActivity(i);
and in the receiving activity:
Bundle extras = getIntent().getExtras();
String text = extras.getString("name");
Here's how I do it:
EditText sub6 = (EditText) findViewById(R.id.edittext16);
TextView txt6 = (TextView) findViewById(R.id.textview25);
String subject = sub6.getText().toString();
txt6.setText(subject);
You do a lot of unnecessary casts. Like your third line casts a String to a String. Which is useless. Also, declare the object types (like String, EditText, etc.) before declaring them.
In MainActivity,send the string using intent
sub6 = (EditText)findViewById(R.id.editText16);
txt6 = (TextView)findViewById(R.id.textView25);
subject = sub6.getText().toString();
Intent in=new Intent(FirstActivity.this,SecondActivity.class);
in.putExtra("key",subject);
startActivity(in);
In the second Activity recieve intent using getIntent() andthe string using key getStringExtra()
Intent in=getIntent();
String s=in.getStringExtra("key");
txt6.setText(s);

Send data from 1 activity to other activity via intent in android.. i want send SongName to next activity

Intent in = new Intent(MainActivity.this, Player.class);
String SongName = SpinSongSelector.getSelectedItem().toString ();
startActivity (in);
i wan to send SongName to next Activity
Try this in your first activity,
Intent intent = new Intent(MainActivity.this, Player.class);
String SongName = SpinSongSelector.getSelectedItem().toString ();
intent.putExtra("message", SongName);
startActivity(intent);
and then in second activity get your string like below
Bundle bundle = getIntent().getExtras();
String message = bundle.getString("message");
then set this string to text-view (This is not necessary but just for your reference, if you want to check whether you got correct string or not)
TextView txtView = (TextView) findViewById(R.id.your_resource_textview);
txtView.setText(message);
use this line before startActivty :
intent.putExtra("SongName",SongName);
then retrieve it from the target activty.
just use
first activity to send :
Intent in = new Intent(MainActivity.this, Player.class);
String SongName = SpinSongSelector.getSelectedItem().toString ();
in.putExtra("data",SongName);
startActivity (in);
to receive in another activity:
String Sname = getIntent().getExtras().getString("data");
to see the details of intents refer here : http://developer.android.com/guide/components/intents-filters.html

passing values to another activity

I am trying to pass value "0000002" in string format to next activity like below:
Intent pass = new Intent(this, SecondActivity.class);
pass.putExtras("EmpID", "0000002");
In second activity
Bundle info = getIntent().getExtras();
System.out.println("Test " + info.getString("EmpID")); // this line printing "null" value instead of "0000002".
I am able to pass and fetch the other strings successfully. I am not able to fetch the EmpID.
Please help me.
Here is a Sample
From 1st Activity
Bundle localBundle = new Bundle();
localBundle.putString("Loan Amount", editText1.getText().toString());
localBundle.putString("Loan Tenture", editText2.getText().toString());
localBundle.putString("Interest Rate", editText3.getText().toString());
Intent localIntent = new Intent(this, Activity2.class);
localIntent.putExtras(localBundle);
startActivity(localIntent);
and in Activity2
String string1 = getIntent().getStringExtra("Loan Amount");
String string2 = getIntent().getStringExtra("Loan Tenture");
String string3 = getIntent().getStringExtra("Interest Rate");
For your case, you can use like
Bundle localBundle = new Bundle();
localBundle.putString("EmpID", "0000002");
Intent pass = new Intent(this, SecondActivity.class);
pass.putExtras(localBundle);
startActivity(pass);
and in SecondActivity you can get the EmpId like
String empId = getIntent().getStringExtra("EmpID");
----------------- Another Way -----------------
Intent pass = new Intent(this, SecondActivity.class);
pass.putExtra("EmpID", "0000002");
startActivity(pass);
In second activity you can get the EmpId like
Bundle bundle = getIntent().getExtras();
String empId = bundle.getString("EmpID");
Write pass.putExtra("EmpID", "0000002"); not putExtras
use this
Intent pass = new Intent(this, SecondActivity.class);
pass.putExtra("EmpID", "0000002");
startActivity(pass);
In second activity
Bundle info = getIntent().getExtras();
System.out.println("Test " + info.getString("EmpID"));
//Activity A
Intent pass = new Intent(this, SecondActivity.class);
pass.putExtra("EmpID", "0000002");
//Activity B
Intent intent = getIntent();
String EmpID = intent.getStringExtra("EmpID");
System.out.println("Test " + EmpID);
Try Using this:
When passing value:
Intent pass = new Intent(this, SecondActivity.class);
pass.putExtra("EmpID", "0000002");
To get the value:
System.out.println("Test " + getIntent().getStringExtra("EmpID"));

How to pass hidden id using Intent in Android?

I have Edittext Activity & I have set Edittext Id in different getter setter class. Now I want to pass Edittext id using intent. I am trying to create a object of getter class but when I am calling getEdittext id it is passing 0 or new value. Can anybody tell me how it is possible.
my code here:
case R.id.setDate:
Intent passAddTask_Id = new Intent(getApplicationContext(), Task_Details.class);
db = new TodoTask_Database(getApplicationContext());
tasknote = taskNote.getText().toString();
db.addTaskNote(tasknote, null, completed);
AddTask addTask = new AddTask(); //Getter setter class object
int addtask_Id = addTask.getTaskID(); //getting 0 value
passAddTask_Id.putExtra("AddTask_Id", addtask_Id);
startActivity(passAddTask_Id);
break;
Getting Intent:
Intent passAddTask_ID = getIntent();
int AddtaskID = passAddTask_ID.getIntExtra("AddTask_Id", 0);
To get the value, you'll have to use putExtra() like so:
case R.id.setDate:
Intent passAddTask_Id = new Intent(getApplicationContext(), Task_Details.class);
passAddTask_Id.putExtra("AddTask_Id", 11);
db = new TodoTask_Database(getApplicationContext());
tasknote = taskNote.getText().toString();
db.addTaskNote(tasknote, null, completed);
AddTask addTask = new AddTask(); //Getter setter class object
int addtask_Id = addTask.getTaskID(); //getting 0 value
passAddTask_Id.putExtra("AddTask_Id", addtask_Id);
startActivity(passAddTask_Id);
break;
From the documentation here -
Returns
the value of an item that previously added with putExtra() or the default value if none >was found.
After that, when you use:
Intent passAddTask_ID = getIntent();
int AddtaskID = passAddTask_ID.getIntExtra("AddTask_Id", 0);
You'll get the result of 11, because you have put it with the putExtra() method.
You are passing the values through intent correctly. I think you are getting problems while getting the passed values. Try this and this works for me.
You have to use a bundle for getting the values.
Intent intent = new Intent();
Bundle b = intent.getExtras();
int addTaskID = b.getInt("AddTask_Id, 0);
Finally I got the Answer here... I need to create query in database, return long in insert method and then call that particular row id.
like this:
public long addTaskNote(String taskNote, String TaskList_Id, String taskCompleted) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_TASKNOTE, taskNote); // Task Note
values.put(KEY_TASK_TASKLISTID, TaskList_Id); // TaskList ID
values.put(KEY_TASKCOMPLETED, taskCompleted); // Task Completed
// Inserting Row
long id = db.insert(TABLE_TODOTASK, null, values);
db.close(); // Closing database connection
return id; //It will give you the return value of id.
}
Changes in my code:
case R.id.setDate:
Intent passAddTask_Id = new Intent(getApplicationContext(), Task_Details.class);
db = new TodoTask_Database(getApplicationContext());
tasknote = taskNote.getText().toString();
long Addtask_id = db.addTaskNote(tasknote, null, completed);
System.out.println(Addtask_id);
passAddTask_Id.putExtra("AddTask_Id", Addtask_id);
startActivity(passAddTask_Id);
break;
Here you can get id at run time saved value.

Am unable to get the Data from bundle in my application

In my app, i need to send a Two dimensional Array and two more integer values form on Activity to Another with the help of Intent method.
This is done as ..
Intent i = new Intent(getApplicationContext(), ViewActivity.class);
Bundle postbundle = new Bundle();
String[][] X={{"abc"},{"def"}};
postbundle.putSerializable("data", X);
i.putExtra("A", postbundle);
i.putExtra("albumid", position);
i.putExtra("Bigcard",bigcard);
here am using .putSerializable method to place an array into bundle.
So to access these data in the receiver Activity am using
Bundle bundle = getIntent().getBundleExtra("A");
String[][] ABC=(String[][]) bundle.getSerializable("data");
Log.e("Array is",""+ABC);
but I got java.lang.NullPointerException error message..
Whith out use of " Static " declaration how can i get these values from bundle here (in the receiver Activity..)
Let me out pls from this ..
step-1:Write a seperate bean class and save into another file
public class MyBean implements Serializable{
String[][] data = null;
public void set2DArray(String[][] data){
this.data = data;
}
public String[][] get2DArray(){
return data;
}
}
step-2:In the calling activity
Intent intent = new Intent(this, Second.class);
String data[][] = new String[][] {{"1","kumar"},{"2","sona"},{"3","kora"},{"1","pavan"},{"2","kumar"},{"3","kora333"}};
MyBean bean = new MyBean();
bean.set2DArray(data);
Bundle b = new Bundle();
b.putSerializable("mybean", bean);
intent.putExtra("obj", b);
startActivity(intent);
step-3:In the caller activity
Bundle b = getIntent().getBundleExtra("obj");
MyBean dData = (MyBean) b.getSerializable("mybean");
String[][] str =dData.get2DArray();
Not a real answer, but a try:
what happens if You try:
Intent intent = getIntent();
int a = intent.getIntExtra("albumid"); //if Your value is an int, otherwise use String
//getStringExtra or whatever Your value is

Categories

Resources