I'm a PHP developer but today I need to face the Android Studio.
I want to send an input text to another activity. But I had some errors:
error: non-static method putExtra(String,String) cannot be
referencedfrom a static context error: cannot find symbol method
StartActivity(Intent)
public class MainActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE = "com.proj.proj";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendMessage(View view){
Intent intent = new Intent(this, main.class);
EditText editText = (EditText) findViewById(R.id.main);
String message = editText.getText().toString();
Intent.putExtra(EXTRA_MESSAGE, message);
StartActivity(intent);
}
}
what is wrong?
Use proper instance of intent not Intent.
public class MainActivity extends AppCompatActivity {
EditText editText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.main);
}
public void sendMessage(View view){
Intent intent = new Intent(this, main.class);
String message = editText.getText().toString();
intent.putExtra("com.proj.proj", message);
startActivity(intent);
}
}
Related
From First Activity Code
String value="7u1JgjTqaYo";
Intent loginintent = new Intent(MainActivity.this, Main2Activity.class).putExtra("VIDEO_KEY",value);
startActivity(loginintent);
Second Activity Code here
public class Main2Activity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
Intent intent = getIntent();
String VIDEO_ID = intent.getStringExtra("VIDEO_KEY");
YouTubePlayerView youTubePlayerView;
Button btnPlay;
YouTubePlayer.OnInitializedListener onInitializedListener;
}
This is my MainActivity intent:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String value="7u1JgjTqaYo";
Intent loginintent = new Intent(MainActivity.this, SecondActivity.class)
.putExtra("VIDEO_KEY",value);
startActivity(loginintent);
}
And this is my SecondActivity where I get the bundle:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
String apiKey = getIntent().getStringExtra("VIDEO_KEY");
Toast.makeText(this, apiKey, Toast.LENGTH_SHORT).show();
}
This code is working properly. Are you getting the Bundle in the onCreate method?
I hope it helps you
I am trying to send data through an activity to another but there is a variable error that I didn't understand, please Help. Thank you very much for your time and assistance in this matter.
public class MainActivity extends AppCompatActivity {
EditText Message_Text;
public final static String MESSAGE_KEY="com.example.zeeshan.userinterface.message_key";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendMessage(View views) {
Message_Text= (EditText) findViewById(R.id.Message_Text);
String message=Message_Text.getText().toString();
Intent intent= new Intent(this, SecondActivity.class);
intent.putExtra(MESSAGE_KEY,message);
startActivity(intent);
}
}
the second activity code is:
public class SecondActivity extends AppCompatActivity {
public final static String MESSAGE_KEY="com.example.zeeshan.userinterface.message_key";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent= getIntent();
String message = intent.getStringExtra(MESSAGE_KEY);
TextView textView = new TextView(this);
textView.setTextSize(35);
// setContentView(R.layout.second_layout);
}
}
you have to write like below.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendMessage();
}
public void sendMessage() {
EditText Message_Text= (EditText) findViewById(R.id.Message_Text);
String message=Message_Text.getText().toString();
Intent intent= new Intent(this, SecondActivity.class);
intent.putExtra("MESSAGE_KEY",message);
startActivity(intent);
}
public class MainActivity extends Activity
{
public static String EXTRA_MESSAGE;
private Intent ceec;
private EditText cc;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);}
public void sendMessage(View view)
{
ceec = new Intent(this, ToActivity.class);
cc = (EditText) findViewById(R.id.edit_message);
String message = cc.getText().toString();
ceec.putExtra(EXTRA_MESSAGE, message);
startActivity(ceec);
}}
And
public class ToActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setText(message);
textView.setTextColor(Color.rgb(5,8,100));
if( message == "hi"){
textView.setTextSize(80);
}
// Set the text view as the activity layout
setContentView(textView);
}}
[ if( message == "hi"){
textView.setTextSize(80);
} ]
It didn't work why? And how to fix it and thank you
Instead of
message == "hi"
You should do:
message.equal("hi")
Never compare Strings with ==, check out this question to understand why.
Use .equals() method as,
if(message.equals("hi")){
textView.setTextSize(80);
}
else
{
// do your else stuff
}
this should work.
Can someone help me? How do I open a new activity after passing basic login argument, here's my code and I don't know what's going, I get an error:
public class MainActivity extends AppCompatActivity {
EditText usern = (EditText)findViewById(R.id.user_name);
String user_name = usern.getText().toString();
EditText passw = (EditText)findViewById(R.id.password);
String pass_word = passw.getText().toString();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/** Called when the user clicks the Send button */
public void nextPage(View view) {
// Do something in response to button
if (user_name.equals("sheldonmad") && pass_word.equals("password")) {
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
}
}
}
#Sheldon Madison : Try this way . Need proper Initialization of Global or local variables .
Please read Official Documents
http://developer.android.com/intl/es/index.html
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/** Called when the user clicks the Send button */
public void nextPage(View view) {
EditText usern = (EditText)findViewById(R.id.user_name);
String user_name = usern.getText().toString();
EditText passw = (EditText)findViewById(R.id.password);
String pass_word = passw.getText().toString();
// Do something in response to button
if (user_name.equals("sheldonmad") && pass_word.equals("password")) {
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
}
}
}
Try
public class MainActivity extends AppCompatActivity {
EditText usern,passw;
String user_name,pass_word;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
usern = (EditText)findViewById(R.id.user_name);
passw = (EditText)findViewById(R.id.password);
}
/** Called when the user clicks the Send button */
public void nextPage(View view) {
user_name = usern.getText().toString();
pass_word = passw.getText().toString();
// Do something in response to button
if (user_name.equals("sheldonmad") && pass_word.equals("password")) {
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
}
}
}
I am trying to pull in a string from one activity to another but every time I try to open the one activity I get a forceclose on .getString.
Logcat Error
10-26 10:36:45.444: ERROR/AndroidRuntime(15112): at http.www.hotapp.com.timeandlocation.email.EmailSettings.onCreate(ThisActivity.java:29)
Activity Calling the string
public class ThisActivity extends Activity{
private TextView reciever;
private String rec;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.elayout);
reciever = (TextView) findViewById (R.id.Reciver);
rec =
getIntent()
.getExtras()
.getString
("send");
reciever.setText(rec);
}
Activity with the string
public class OtherActivity extends Activity{
private EditText sendto;
private Button save;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.emailactivity);
sendto = (EditText) findViewById(R.id.sendto);
save = (Button) findViewById(R.id.Save);
save.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Intent intent = new Intent (EmailActivity.this,EmailSettings.class);
intent.putExtra("send", sendto.getText().toString());
startActivity(intent);
}
});
}
}
Thanks for the help.
try using getIntent().getStringExtra("send");
If you want to check if the extra is there use:
getIntent().hasExtra("send");
Try this:
Bundle extras = getIntent().getExtras();
if(extras !=null)
{
rec = extras.getString("send");
}
try this by eliminating NULL EXCEPTION from getString() function