I got 2 classes in my project. The first class(main) have a listview and this is the onclick():
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// Get the item that was clicked
Object o = this.getListAdapter().getItem(position);
String keyword = o.toString();
class2 sec = new Class2();
Intent intent = new Intent(this, Class2.class);
startActivity(intent) ;
if (keyword == "hello"){
sec.setInfo(keyword);
}
}
so and then in my other class which have a defferent layout.xml. The code is:
public class det extends Activity {
static WebView map;
public TextView header;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.details);
}
public void setInfo(String mystring){
header = (TextView) findViewById(R.id.text01);
header.setText(mystring);
//Toast.makeText(this, map, Toast.LENGTH_LONG).show();
//return;
}
Ye, i keep getting force close on my android phone. The App i meant to change the header text to text that ive tapped on the listview. But when i click a item it pop up a window with FC.
ive try to comment away the:
header = (TextView) findViewById(R.id.text01);
header.setText(mystring);
and it worked without a FC however the headertext is stil null.
Thank you!
Your Friend!
First of all, you need to post the stack trace for us to have any idea how to help.
Second, I'm assuming Class2 extends Activity or else Intent intent = new Intent(this, Class2.class); doesn't make any sense. That being the case, class2 sec = new Class2(); is ALWAYS wrong. You never ever call new on a class that extends Activity.
You can't call methods on another activity like that. Your only real option is to send the keyword in the intent by using putExtra, and then getting retrieving it in Class2
Related
I need to pass some value from 1st activity into the third. I already pass it form 1st to 2nd like this.
my 1st activity: (I do it in on create method)
mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(DisplayRecepit.this, DisplayLogs.class);
intent.putExtra("recepitID", receiptList.get(position).getId());
startActivity(intent);
}
});
And I recieved it in 2nd activity like this: (I do it in on create method)
final long forwardedId = (long) getIntent().getExtras().get(String.valueOf("recepitID"));
List<Logs> logsList = new Select().from(Logs.class).where("Receipt = " + forwardedId).execute();
Now I need somehow pass it from 2nd activity to my third activity.
In my 2nd activity I have a button that takes me to 3rd activity.
I saw some examples on web but I didn't make my app working, so any help is welcome.
Question: I have pass value via intent from 1st activity to 2nd activity. How should I pass this same value from 2nd activity to my 3rd activity?
In your second activity pass this value using intent
Intent i = new Intent(getApplicationContext, Third.class);
i.putExtra("forwardedId",forwardedId);
startActivity(i)
First you have to pass data to 2nd Activity.then you can pass from 2nd to 3rd Activity.
Using default android mechanism its not possible. Still if you cant to achieve this you can use Eventbus : "https://github.com/greenrobot/EventBus" where you post message (can be anything a string, integer, even a class pojo with arrylist) from first activity and catch it anywhere.
Go to above mentioned link add the dependency in your app level build.gradle and sync it.
Create this event Pojo :
public class SomeEvent {
private ArrayList<String> message;
public SomeEvent(ArrayList<String> message) {
this.message = message;
}
public ArrayList<String> getMessage() {
return message;
}
}
Activity 1 :
in onResume() do this :
EventBus.getDefault().register(this);
in onDestroy() do this :
EventBus.getDefault().unregister(this);
to post an event :
Do this only after Eventbus registration.
EventBus.getDefault().post(new SomeEvent(some arraylist);
Now in Activity 3:
just write this method. Don't call it explicitly. Eventbus handles that internally. Make sure the argument of this method is your event class which you post in Eventbus.
public void onEvent(SomeEvent event){
// you got your arraylist which you posted from Activity 1;
ArrayList<String> list = event.getMessage();
}
I am trying to make a button in one activity (SetupMenu) that, when pressed, puts an int into the intent and carries that over to the next activity (IntroActivity) where a textView will retrieve the int and display it.
Problem is, when the app runs and I get to the activity and press the button, the app crashes and my emulator tells me that "Unfortunately [my app] has stopped working."
I feel like I've tested every possible angle to get this to work. I should note that the button has worked fine, the textview has worked fine, everything else is working smoothly - I only run into issues when I try retrieving the intent and displaying it in textView. I tried passing through a String instead of an Int and also had issues (my string would not appear). Any pointers?
SetupMenu activity (here I put an int into my intent):
public class SetupMenu extends Activity {
public final static String extra_progress_key = "com.example.angelsanddemons.track_players";
public int track_players = 0;
public void to_intro(View view) {
Intent intent = new Intent(this, IntroActivity.class);
intent.putExtra(extra_progress_key, track_players);
startActivity(intent);
}
IntroActivity activity (here I try to retrieve the int from the intent):
public class IntroActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
int temp = intent.getIntExtra(SetupMenu.extra_progress_key, 0 );
TextView textView = new TextView(this);
textView.setText(temp);
setContentView(textView);
}
}
One problem is that you can't set a TextView's text to an int; you'll need to first convert it to an string. It's also not a good idea to be manipulating views before you've inflated them, so perhaps your onCreate() should be:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
int temp = intent.getIntExtra(SetupMenu.extra_progress_key, 0 );
TextView textView = new TextView(this);
setContentView(textView);
textView.setText(String.valueof(temp));
}
I see nothing that ensure that SetupMenu activity is created and in memory when IntroActivity is launched. To make sure, don't pass the variable, but the string itself and check if it work:
int temp = intent.getIntExtra("com.example.angelsanddemons.track_players", 0 );
When I'm trying to pass data to another activity it for some reason gets lost. My sending activity looke like:
protected OnItemClickListener onArtistItemClick = new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View arg1, int cursor, long arg3) {
Intent artistCardIntent = new Intent(getBaseContext(), ArtistCardActivity.class);
Artist artist = (Artist) adapter.getItemAtPosition(cursor);
artistCardIntent.putExtra("artist_id", artist.getId());
artistCardIntent.putExtra("tt", "tt");
startActivity(artistCardIntent);
};
};
And while debugging I can see that artistCardIntent gets populated, however in the receiving activity Intent i doesn't contain any extra inforamation:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.artist_card);
Intent i = getIntent();
What am I doing wrong?
PS both activities extends FragmentActivity.
Thanks.
To receive the data use getextras() method. Find the following code. i think it helps you
Intent i = getIntent();
Bundle b=i.getExtras();
if(b!=null)
{
String artist= ("artist_id");
}
I don't see that you are getting the extra information.
Check out this answer How do I get extra data from intent on Android?
convert those values to string and try it , It will work
startActivity(new Intent(Home.this,galmenu.class).putExtra("page","home"));
Intent myIntent = getIntent();
test=myIntent.getExtras().getString("page");
it is working perfectly
I have the following code. I have the data in the object o. There are three set of values in o (description, name, image url). I need these data to be initialized to a string and pass it to other activity using intent. How do I get each value in the object. Each list item has a image, item name and item description.
fp_list.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView parentView, View v, int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "pos"+position, Toast.LENGTH_LONG).show();
Object o = parentView.getItemAtPosition(position);
Intent i = new Intent(FeaturedProductsActivity.this, ShowProduct.class);
}
}
Better thing is to make you Custom Object Parcelable
the Other option is to pass these values one by one as:
Intent i=new Intent(FeaturedProductsActivity.this,ShowProduct.class);
i.putString("desc", o.getDescription());
i.putString("name", o.getName());
// rest of your values
ang get these values in ShowProduct Activity as:
Intent in = this.getIntent();
String name = in.getStringExtra("name");
String desc = in.getStringExtra("desc");
//rest of you values
You can't pass object from One activity to another using Intent. Yo have to use Parcelable interface . see this.
http://prasanta-paul.blogspot.com/2010/06/android-parcelable-example.html
Bundle bundle =new Bundle();
bundle.putString("memId",o.getId() );
Intent newIntent=new Intent(friendsOffrinends.this,RestaurantDetails.class);
newIntent.putExtras(bundle);
startActivityForResult(newIntent, 0);
simple way :
1-make a class which extend Application.
2-make an object of what you want to transfer.
3-make its set and get.
4-in onitemclick set the value.
5-get the value wherever you want.
Example:
1-make a class which extend Application.
2-make an object of what you want to transfer.
3-make its set and get.
public class App extends Application {
private static yourObject obj;
public yourObject getobj() {
return obj;
}
public void setobj(yourObject obj) {
this.obj= obj;
}
}
in your class
fp_list.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView parentView, View v, int position, long id) {
// TODO Auto-generated method stub
yourObject o =(yourObject) parentView.getItemAtPosition(position);
App.setobj(o);
}
}
in any other where all over the application classes
//here you will get your object which you have set on item click
yourObject obj=App.getobj;
hope this help.
When you have data you can use Bundle or you can directly attach data with Intent e.g. in.putExtra(...).
You can do it like this:
Intent intent = new Intent(YourActivity.this, NewActivity.class);
intent.putExtra("description", o.get(position).getdescription())
startActivity(descriptionIntent);
And in the second activity:
Intent descriptionIntent = getIntent();
String description = descriptionIntent.getExtras().getString("description");
I am creating a chat application & for that purpose i have used TabHost.
In that first tab contains List of Buddies, and as soon as user clicks on any of the buddy
from buddy it should create another tab for that buddy in order to chat.
I am completed up to this but my problem is I am using a single Activity to perform Chat
but It always shows the same activity for each buddy.
Any Help will be highly appreciated. Here is my code,
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
RosterEntry entry = List.get(position);
String userName = entry.getName();
Intent intent = new Intent().setClass(RosterScreen.this,
com.spotonsoft.chatspot.ui.ChatScreen.class);
TabSpec tabSpec = Home.tabHost.newTabSpec("chat")
.setIndicator(userName).setContent(intent);
Home.tabHost.addTab(tabSpec);
}
Best Regards,
~Anup
in onCreate of ChatScreen you only setup basic stuff like getting View and store it in private fields
onResume you "recreate" ChatScreen with buddy-specific data ... how to do this (pls, read comments in code)?
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.*;
public class ChatScreen extends Activity {
TextView textview = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
textview = new TextView(this);
setContentView(textview);
}
#Override
public void onResume(){
super.onResume();
Intent intent = getIntent();
if(intent!=null){
//we read buddy-specific data here
textview.setText(intent.getStringExtra("chatwith"));
//we only setting textview with user name
//in real app you should store conversation somewere (fx in db)
//and load it here
}
}
}
and your code
Intent intent = new Intent().setClass(RosterScreen.this, com.spotonsoft.chatspot.ui.ChatScreen.class);
// you shoud add this line and provide some information fx useName or userID to ChatScreen Activity
intent.putExtra("chatwith", userName);
TabSpec tabSpec = Home.tabHost.newTabSpec("chat").setIndicator(userName).setContent(intent);
Home.tabHost.addTab(tabSpec);
You can add data to your intent before starting it, for example
intent.putExtra("user", userName);
In the onCreate of your activity you can read this data and use it to setup your activity.
Also, make sure that you have set the proper launchmode for your activity.