There are many many questions about starting new Intent from another Intent but i'm unable to find solutions of mine. In my main Activity i started an Activity this way
Intent intent = new Intent(MainActivity.this, DownloadManagerSettings.class);
intent.putExtra("DM_SETTINGS_ACTIVITY", "MainActivity");
startActivity(intent);
and trying to get extra String value this way
public class DownloadManagerSettings extends Activity {
private Button dmOk;
private Bundle extra;
private String className;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dm_settings);
extra = getIntent().getExtras();
dmOk = (Button) findViewById(R.id.dm_settings_ok);
final Bundle strExtra = extra;
dmOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
className = strExtra.getString("DM_SETTINGS_ACTIVITY");
try {
Intent intent = new Intent(DownloadManagerSettings.this, Class.forName(className));
startActivity(intent);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
});
}
But in my LogCat shows following error
08-28 14:19:33.777: E/AndroidRuntime(17837): java.lang.NullPointerException08-28 14:19:33.777: E/AndroidRuntime(17837): at com.downloadmanager.settings.DownloadManagerSettings$1.onClick(DownloadManagerSettings.java:101)
Here 101 Line is
className = strExtra.getString("DM_SETTINGS_ACTIVITY");
UPDATE
new error
08-28 14:55:11.237: E/AndroidRuntime(24623): Caused by: java.lang.NullPointerException 08-28 14:55:11.237: E/AndroidRuntime(24623): at com.downloadmanager.settings.DownloadManagerSettings.onCreate(DownloadManagerSettings.java:66)
66 Line is
className = strExtra.getString("DM_SETTINGS_ACTIVITY");
NEW UPDATE1
Now
Intent intent = new Intent(MainActivity.this, DownloadManagerSettings.class);
intent.putExtra(getApplicationContext().getPackageName()+".DM_SETTINGS_ACTIVITY", "MainActivity");
startActivity(intent);
Another Activity
className = getIntent().getStringExtra(getApplicationContext().getPackageName()+".DM_SETTINGS_ACTIVITY");
final String newClass = className;
Log.d("DM_AAAAAAAA", className);
dmOk.setOnClickListener(new View.OnClickListener() {
Find in LogCat
08-28 15:09:14.670: E/AndroidRuntime(27291): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.addon.downloadmanager/com.downloadmanager.settings.DownloadManagerSettings}: java.lang.NullPointerException: println needs a message
Thanks in advance
try this:
public class DownloadManagerSettings extends Activity {
private Button dmOk;
private Bundle extra;
private String className;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dm_settings);
extra = getIntent().getExtras();
dmOk = (Button) findViewById(R.id.dm_settings_ok);
final Bundle strExtra = extra;
className = strExtra.getString("DM_SETTINGS_ACTIVITY");
dmOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method
try {
Intent intent = new Intent(DownloadManagerSettings.this, Class.forName(className));
startActivity(intent);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
});
}
Get the String data in onCreate first and then use it in onClick.
Update :
seems you'll have to add your package name as the prefix, see this. After that, use getStringExtra/getCharSequenceExtra to get the string data.(note we don't use Bundle any more). This should work.
update 1:
The following code work on my device.
Intent newIntent = new Intent(getApplicationContext().getPackageName());
newIntent.setClass(this, SecondActivity.class);
newIntent.putExtra(getApplicationContext().getPackageName()+".abc", "test");
startActivity(newIntent);
and in the other activity:
String data = getIntent().getStringExtra(getIntent().getAction()+".abc");
From the javadoc of Intent.putExtra(String,String) :
The name must include a package prefix, for example the app com.android.contacts would use names like "com.android.contacts.ShowAll".
So try this :
intent.putExtra(getPackageManager().getPackageName()+".DM_SETTINGS_ACTIVITY", "MainActivity");
and later:
className = strExtra.getString(getPackageManager().getPackageName()+".DM_SETTINGS_ACTIVITY");
Use getStringExtra() instead of getExtra()
or you can also do in from other side during intenet creation
Intent intent = new Intent(MainActivity.this, DownloadManagerSettings.class);
Bundle extras = new Bundle();
extras.putString("DM_SETTINGS_ACTIVITY", "MainActivity");
startActivity(intent);
Try this way,hope this will help you to solve your problem.
Intent intent = new Intent(MainActivity.this, DownloadManagerSettings.class);
intent.putExtra("DM_SETTINGS_ACTIVITY", "MainActivity");
startActivity(intent);
className = className = getIntent().getStringExtra("DM_SETTINGS_ACTIVITY");
OR
Intent intent = new Intent(MainActivity.this, DownloadManagerSettings.class);
Bundle bundle = new Bundle();
bundle.putString("DM_SETTINGS_ACTIVITY", "MainActivity");
startActivity(intent);
className = className = getIntent().getBundleExtra().getString("DM_SETTINGS_ACTIVITY");
The problem in your code is here,
intent.putExtra(getApplicationContext().getPackageName()+".DM_SETTINGS_ACTIVITY", "MainActivity");
Just replace this line with
intent.putExtra("MainActivity",getApplicationContext().getPackageName()+".DM_SETTINGS_ACTIVITY");
Check this code :
1. **MainActivity.java**
Intent intent = new Intent(MainActivity.this,MainActivity2.class);
intent.putExtra("MY TEXT", getApplicationContext().getPackageName()+".TestActivity");
startActivity(intent);
2. **MainActivity2.java**
Intent i = getIntent();
final String className = i.getStringExtra("MY TEXT");
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try {
Intent intent = new Intent(MainActivity2.this,Class.forName(className));
startActivity(intent);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
});
This is worked on my device.
Related
Im trying to pass data from my TestSearch class to GoogleSearch class. I assigned the values of the tEdit test to a string variable and I want to pass that to GoogleSearch class. But my app get crash when I run it.
Button disp=(Button)findViewById(R.id.btn_Search);
disp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
EditText inputTxt = (EditText) findViewById(R.id.editText1);
String str = inputTxt.getText().toString().toLowerCase().trim();
ArrayList<HashMap<String, String>> userList = controller.searchBook(str);
if (userList.size() != 0) {
//Do something
}
else
{
Intent intent = new Intent("com.example.captchalib.GoogleSearch");
intent.putExtra("message", str);
startActivity(intent);
}
}
});
In my GoogleSearch Class I used Below Code to catch the Intent
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_menu);
Intent intent = getIntent();
String message = intent.getStringExtra("message");
((TextView)findViewById(R.id.Receive)).setText(message);
}
why this is happening and how to solve it ?
Logcat
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.example.captchalib.GoogleSearch (has extras) }
android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1545)
android.app.Instrumentation.execStartActivity(Instrumentation.java:1416)
Change this line:
Intent intent = new Intent("com.example.captchalib.GoogleSearch");
To:
Intent intent = new Intent(TestSearch.this, GoogleSearch.class);
in My Main Activity I am pasing data using the data using intent using the putextra()
method
private void editHandler()
{
//send the details to another form
if (itemID > 0)
{
Bundle values = new Bundle();
singleItem = (TODOListItem) adapter.getItem(itemID);
Intent intent = new Intent(this,AddorEdit.class);
values.putString("text",singleItem.getText());
values.putString("date", singleItem.getDate());
values.putString("time", singleItem.getDate());
values.putInt("id", singleItem.getItemID());
values.putInt("alarm", singleItem.getAlarm());
intent.putExtra("bundle", values);
startActivity(intent);
}
in the next Activity I am receiving that intent
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addor_edit);
todoNote = (EditText)findViewById(R.id.txt_todoNote);
todoDate = (EditText) findViewById(R.id.txt_dateTODO);
todoTime = (EditText) findViewById(R.id.txt_timeTODO);
todoalarm =(ToggleButton) findViewById(R.id.toggle_alarm);
alarmEnable = (ImageView) findViewById(R.id.img_alarmEnable);
canceltodo = (Button) findViewById(R.id.btn_cancelTODO);
maketodo = (Button) findViewById(R.id.btn_makeTODO);
//Receiving intent
Bundle bundle = getIntent().getBundleExtra("bundle");
getValuesForEdit(bundle);
todoalarm.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
if(todoalarm.getText().equals("ON"))
{
alarmEnable.setImageResource(R.drawable.dark_alarm);
alarm = 1;
}
else
{
alarmEnable.setImageResource(0);
alarm = 0;
}
}
});
maketodo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
addnewTODO();
}
});
}
private void getValuesForEdit(Bundle bundle)
{
// if the Edit Button is pressed get all values from listView
ID = bundle.getInt("id");
todoNote.setText(bundle.getString("text"));
todoDate.setText(bundle.getString("date"));
todoTime.setText(bundle.getString("time"));
alarm = bundle.getInt("alarm");
if (alarm == 1)
{
todoalarm.setText("ON");
alarmEnable.setImageResource(R.drawable.dark_alarm);
}
}
and The application crashes
am I doing wrong with intents?? which is the right way to pass data between
activities?? suggestions and advises are needed...
thanks!!
Intent intent = new Intent(this,AddorEdit.class);
intent.putExtra("text",singleItem.getText());
intent.putExtra("date", singleItem.getDate());
intent.putExtra("time", singleItem.getDate());
intent.putExtra("id", singleItem.getItemID());
intent.putExtra("alarm", singleItem.getAlarm());
startActivity(intent);
//While get this data:
ID = getIntent().getIntExtra("id");
todoNote.setText(getIntent().getStringExtra("text"));
todoDate.setText(getIntent().getStringExtra("date"));
todoTime.setText(getIntent().getStringExtra("time"));
alarm = getIntent().getIntExtra;
Try this
Bundle values = new Bundle();
singleItem = (TODOListItem) adapter.getItem(itemID);
Intent intent = new Intent(this,AddorEdit.class);
values.putString("text",singleItem.getText());
values.putString("date", singleItem.getDate());
values.putString("time", singleItem.getDate());
values.putInt("id", singleItem.getItemID());
values.putInt("alarm", singleItem.getAlarm());
intent.putExtras(values);
startActivity(intent);
and
Bundle bndl = this.getIntent().getExtras()
I have activity A where it has a ADD button when I click on the button it displays the list of users with check boxes in Second Activity and when I select the users and click the conform button it should display in all the users Activity A.I am using adapter to display the users.
from this button I am getting the list of checked values
private void checkButtonClick()
{
conform = (Button)findViewById(R.id.Confirm);
conform.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
StringBuffer responseText = new StringBuffer();
responseText.append("The following were selected...\n");
ArrayList<Namelist> stateList = contactadapteruser.listexample;
Bundle b=null;
Namelist state;
for(int i=0;i<stateList.size();i++)
{
state = stateList.get(i);
b = new Bundle();
b.putString("selected",state.getName());
if(state.isSelected())
{
responseText.append("\n" + state.getName());
}
}
Intent i= new Intent(Contactselectuser.this,Contactrepresentativedetails.class);
i.putExtras(b);
startActivity(i);
}
});
}
from this button I am getting the I am retrieving the values
send.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i2 = getIntent();
Bundle b1 = i2.getExtras();
String name1 = b1.getString("selected");
tweet.setText(name1+",\t");
}
});
My problem I am unable to get the users which are checked.
Can any one help me .
use start activity for result to start your second activity.
Intent intent = new Intent(this, SecondActivity.class);
startActivityForResult(intent, 1);
from second activity before closing you can send
Intent return_intent = new Intent();
return_intent.putExtra("your_user_list",user_list);
setResult(RESULT_CODE,return_intent);
finish();
Now in your FirstActivity override onActivityResult() method
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_CODE){
// GET YOUR USER LIST HERE AND USE IT FOR YOUR PURPOSE.
user_list=data.getExtra("your_user_list");
}
}
}
You can use broadcast sender and receiver also for your case. But above method is good for your task.
ArrayList<Namelist> stateList = contactadapteruser.listexample;
// convert your ArrayList to a String[]
String[] arr = new String[stateList.size()];
for (int i = 0; i < arr.length; i++)
{
arr[i] = stateList.get(i).getName();
}
Then using this question as a source, put the String array in your intent:
Bundle b = new Bundle();
b.putStringArray("selected", arr);
Intent i = new Intent(Contactselectuser.this,Contactrepresentativedetails.class);
i.putExtras(b);
startActivity(i);
Then to read the String array from your other activity:
Bundle b = getIntent().getExtras();
String[] array = b.getStringArray("selected");
I have a borderless button on in one of my layouts. When it is clicked, I want the intent to transfer both the button's tag and the text it contains. I am having trouble doing this.
This is what I have for when the button is clicked:
public void openGoalWeek (View view) {
Intent intent = new Intent(this, ViewGoal.class);
Button button = (Button) findViewById(R.id.week_goal);
Bundle bundle = new Bundle();
bundle.putString(EXTRA_MESSAGE, button.getText().toString());
bundle.putString(EXTRA_TAG, button.getTag().toString());
intent.putExtras(bundle);
startActivity(intent);
}
This is what I have in the ViewGoal class:
public class ViewGoal extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_goal);
// make sure running on honeycomb or higher for actionbar API
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
}
// get the message from the intent
Intent intent = getIntent();
String message = intent.getBundleExtra(MainActivity.EXTRA_MESSAGE).toString();
String tag = intent.getBundleExtra(MainActivity.EXTRA_TAG).toString();
String text = "null";
if (tag == "year_tag") {
DatabaseHandler db = new DatabaseHandler(this);
Goal goal = db.getYearGoal(message);
text = goal._title + "\n" + goal._description;
}
if (tag == "month_tag") {
DatabaseHandler db = new DatabaseHandler(this);
MonthGoal goal = db.getMonthGoal(message);
text = goal._title + "\n" + goal._description;
}
if (tag == "week_tag") {
DatabaseHandler db = new DatabaseHandler(this);
WeekGoal goal = db.getWeekGoal(message);
text = goal._title + "\n" + goal._description;
}
// create the text view
TextView textView = new TextView(this);
textView.setTextSize(10);
textView.setText(text);
// display the content
setContentView(textView);
}
}
It throws an error at me and I am not sure why. Any help is appreciated!
why don't you just do this, just put your EXTRA instead of using bundle:
...
Intent intent = new Intent(this, ViewGoal.class);
intent.putExtra( EXTRA_MESSAGE, button.getText().toString());
intent.putExtra( EXTRA_TAG, button.getTag().toString());
....
Later on in the other activity:
....
// get the message from the intent
Intent intent = getIntent();
String id = intent.getStringExtra(EXTRA_MESSAGE);
String name = intent.getStringExtra(EXTRA_TAG);
....
In onClick button, instead of this used classname.this
Some of my errors are removed by this. Try if it helps you.
public void openGoalWeek (View view) {
Intent intent = new Intent(className.this, ViewGoal.class);
Button button = (Button) findViewById(R.id.week_goal);
Bundle bundle = new Bundle();
bundle.putString(EXTRA_MESSAGE, button.getText().toString());
bundle.putString(EXTRA_TAG, button.getTag().toString());
intent.putExtras(bundle);
startActivity(intent);
I am a beginner of Android. I want to pass the result from FirstActivity to the SecondActivity as below. How to remove results in intent extra? Or any way to pass the result to SecondActivity and show on the TextView?
(I have make a mistake and replace, my main question is how to delete the result, because i want to set another new result in it.)
FirstActivity.java
public class FirstActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//code...
try {
myDbHelper.createDatabase();
}
catch (IOException ioe) {
Log.d("Error","Error while createing Database");
ioe.printStackTrace();
throw new Error("Unable to create database");
}
try {
myDbHelper.openDataBase();
}
catch(SQLException sqle){
Log.d("Error","Error while Opening Database");
sqle.printStackTrace();
throw sqle;
}
send.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
showResult();
}
});
}
private void showResult() {
//...code
//checking for slection
results = queryData(table, type);
Intent intent = new Intent(this,SecondActivity.class);
intent.putExtra("results", results);
startActivity(intent);
}
public String queryData(String table, String type){
//...
//do somthing to get result
return result;
}
}
SecondActivity.java
public class SecondActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result_item);
TextView tv;
tv = (TextView)findViewById(R.id.tv);
Bundle extras=getIntent().getExtras();
String value1=extras.getString("results");
tv.setText("Result\n" + value1);
}
}
You can do in 2 ways:
1. You can use startActivityForResult(), onActivityResult() from the Activity A, to pass the data on to Activity B, then after some computation, passing the result back to Activity A.
2. Now if you want to just send some data from Activity A to Activity B, and then display it in TextView, then use putExtra() and getExtras()...
Sending from Activity A to B:
Intent i = new Intent(Activity_A.this, Activity_B.class);
i.putExtra("name",Name);
startActivity(i);
Receiving the value on Activity B:
Intent i = getIntent();
String n = i.getExtras().getString("name");
change startActivityForResult(intent, 0) to startActivity(intent);
like this
private void showResult() {
results = queryData(table, type);
Intent intent = new Intent(firstActivity.this,SecondActivity.class);
intent.putExtra("results", results);
startActivity(intent);
}
Make new intent and pass
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
intent.putExtra("results", results);
startActivity(intent);
You were done your way of method that pass the data to next Activity is right. But, you made a mistake with starting the activity. Starting an Activity in your code should be like below -
private void showResult() {
//...code
//checking for slection
results = queryData(table, type);
Intent intent = new Intent(this,SecondActivity.class);
intent.putExtra("results", results);
startActivity(intent); // This is the way to start a new Activity which is in seperate class
}
And, just have a look at below -
How do I pass data between Activities in Android application?
Passing data between activities in Android