Reputation: 77
Probably a simple error but the logcat is confusing me. I have just passed a variable 'name' from a listview to this activity and want to change the title textview to that variable using... 'detailedsocietyname' being used as the textview ID in the XML file
final TextView changetitle = (TextView) findViewById(R.id.detailedsocietyname);
changetitle.setText(name);
When that didn't work I tried to use a toast to test it instead using
Toast.makeText(getApplicationContext(), name, Toast.LENGTH_LONG).show();
The issue is that when the listview choice is chosen the app shuts down.... if all the code above is commented out, it works but the default screen title of 'example' is shown.
Thanks in advance,
Full Code
package com.apolloapps.ntusoc;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;
public class DetailedScreen extends Activity {
final TextView changetitle = (TextView) findViewById(R.id.detailedsocietyname);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Show the Up button in the action bar.
//getActionBar().setDisplayHomeAsUpEnabled(true);
Intent in = getIntent();
String name = in.getStringExtra(("name"));//gets name from intent
Toast.makeText(getApplicationContext(), name, Toast.LENGTH_LONG).show();
changetitle.setText(name);
this.setContentView(R.layout.detailedscreen);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.detailedscreen, menu);
return true;
}
}
EDIT: First activity Intent Code
final String selected = (String) parent.getSelectedItem();
Intent i = new Intent(getApplicationContext(), DetailedScreen.class);
i.putExtra("name", selected);
startActivity (i);
SOLUTION: Use
final String name = items[position];
Instead of
final String selected = (String) parent.getSelectedItem();
Upvotes: 0
Views: 145
Reputation: 4567
try this:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detailedscreen); //set the layout here
final TextView changetitle = (TextView) findViewById(R.id.detailedsocietyname); //then get id of any view
Intent in = getIntent();
String name = in.getStringExtra("name");//gets name here and make sure in first activity you are not passing empty string
Toast.makeText(getApplicationContext(), name, Toast.LENGTH_LONG).show();
changetitle.setText(name);
}
EDIT
if in first activity you passed string like this:
intent.putExtra("name", selected);
then get it in second activity like this
name=in.getStringExtra("name");
your toast message is correct you can check this by displaying a simple text like this :
Toast.makeText(this,"Toast works fine " , 3000).show();
see this example how to pass string to another activity.
Upvotes: 1
Reputation: 211
You did not post the code where you sent the string to this activity but I assume either
-1. that the value you send is empty, -2. you do not send the value -3. the StringExtra name is not the same in this AND the previous activity i.e. "name"
Upvotes: 0
Reputation: 4862
Restructure your onCreate() method
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.detailedscreen);
//your code
}
Upvotes: 0