not getting any response when clicking on send button in android app - android

I am an Android newbie and I was following this tutorial for learning android. The app takes input from the user and prints the text whenever the user presses the Submit button. It is able to take input from the user but it does not output anything when Submit button is pressed. Eclipse doesn't show any error in the code, so I don't know what's wrong. Here is the code(Sorry for pasting so much of code as I wasn't able to make out which file was exactly causing the error):
helloworld/src/com.archit.helloworld/DisplayMessageActivity.java
package com.archit.helloworld;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.archit.helloworld.R;
public class DisplayMessageActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Show the Up button in the action bar.
setupActionBar();
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
//Create new text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
//set the text view as the activity layout
setContentView(textView);
}
/**
* Set up the {#link android.app.ActionBar}, if the API is available.
*/
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() { }
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_display_message,
container, false);
return rootView;
}
}
}
helloworld/src/com.archit.helloworld/MainActivity.java
package com.archit.helloworld;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.helloworld.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
}
}
helloworld/res/layout/activity_display_message.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".DisplayMessageActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
helloworld/res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage" />
</LinearLayout>
helloworld/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.archit.helloworld"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.archit.helloworld.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.archit.helloworld.DisplayMessageActivity"
android:label="#string/title_activity_display_message"
android:parentActivityName="com.example.helloworld.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.helloworld.MainActivity" />
</activity>
</application>
</manifest>

/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
}
has to be
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}

Implement onclicklistener Where is your onclickListener

actually you forgot to add startActivity method
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}

You have missed to call
startActivity(intent);
in your
sendMessage(View V)
method. So finally it will be
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}

Related

Only works in Main Activity?

So I have a code, that's only works in the MainActivity, and I don't know how to make it work, in the second activity.
import android.app.Activity;
import android.content.res.Resources;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends Activity {
private String[] myString;
private static final Random rgenerator = new Random();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources res = getResources();
myString = res.getStringArray(R.array.myArray);
String q = myString[rgenerator.nextInt(myString.length)];
TextView tv = (TextView) findViewById(R.id.textView);
tv.setText(q);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
public void ButtonOnClick (View v) {
Button button = (Button) v;
setContentView(R.layout.activity_szabaly);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
When I start my app, in the first activity the text is changing(because it's a random text changing code), but when I press the button, that makes me go to the second activity, nothing happens, no action in the second activity.
What am I doing wrong?
My Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.example" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Szabaly"
android:label="#string/title_activity_szabaly" >
</activity>
</application>
</manifest>
you add the android:onClick attribute in the layout ? this is bad practice
better implementing View.OnClickListener
public class MainActivity extends Activity {
private String[] myString;
private static final Random rgenerator = new Random();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources res = getResources();
myString = res.getStringArray(R.array.myArray);
String q = myString[rgenerator.nextInt(myString.length)];
TextView tv = (TextView) findViewById(R.id.textView);
tv.setText(q);
//finde button
Button button =(Button)findViewById(R.id.button);
//set OnClickListener
button.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
Intent intent = new Intent(this, Szabaly.class);
startActivity(intent );
break;
}
}
}
You're not actually starting a new activity on your button click. Instead you're just changing your view. This will not be cause to call the onCreate method again.
public void ButtonOnClick (View v) {
Button button = (Button) v;
setContentView(R.layout.activity_szabaly);
}
This code is only changing the view. To start a new activity you'd need to create a new class that extends Activity (just like MainActivity) and register it in the AndroidManifest.xml.
Then you'll be able to start the new activity on click by the following
public void ButtonOnClick (View v) {
Intent i = new Intent(this, NewActivity.class);
startActivity(i);
}
See this question for more detail on starting a new Activity from a button click.

java.lang.IllegalStateException: Could not find a method sendMessage(View)

java code:
package com.example.myfirstapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.EditText;
public class MainActivity<View> extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage" />
</LinearLayout>
the error message:
06-18 15:19:12.493: E/AndroidRuntime(799): java.lang.IllegalStateException: Could not find a method sendMessage(View) in the activity class com.example.myfirstapp.MainActivity for onClick handler on view class android.widget.Button
I really do not know the reason about that,and i check the case about the method name,that's ok.
Who can help me?
you have to declare your activity as :
public class MainActivity extends Activity {
}
and also you need to declare your button in your onCreate() method like below:
Button button_send= (Button)findViewById(R.id.your_Button_Id);
update:
Also you have to assign an id for your button in your manifest and refer to it above.
Remove <View>. Also move your initialization of Editext to oncreate(). Need not initialize editext evey time on button click.
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
private EdiText editext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.edit_message);
}
....

MyFirstApp send button error

I've gone through the MyFirstApp tutorial to the point where the app should run and when you press the send button a new screen should appear , but instead of a new screen appearing it crashes with the 'Unfortunately, MyFirstApp has stopped' message.
I'm new to app development so I need basic instructions.
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.myfirstapp.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.myfirstapp.DisplayMessageActivity"
android:label="#string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
</application>
main activity java
package com.example.myfirstapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendMessage(View view) {
//do something
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}'
DisplayMessageActivty
package com.example.myfirstapp;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
import android.widget.TextView;
public class DisplayMessageActivity extends Activity {
#SuppressLint("NewApi")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_display_message
package com.example.myfirstapp;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
import android.widget.TextView;
public class DisplayMessageActivity extends Activity {
#SuppressLint("NewApi")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}'
Activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<EditText
android:id="#+id/edit_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendmessage" />
</LinearLayout>'
Strings.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<EditText
android:id="#+id/edit_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendmessage" />
</LinearLayout>'
Thanks
Your callback for the Button called sendMessage while you declared sendmessage in xml.

intent not working when swiching from one activity to another

I have made a simple android program with two activities,in that 1st activity contains an edittext and a button,and second activity contain a textview.Now when the button in 1st activity pressed the text from Edittext should go to 2nd activity's textView.I have tried code as below,but it's not working:
MainActivity.java
package com.example.myweb;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myweb";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b =(Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText ed = (EditText)findViewById(R.id.edit_msg);
Intent i = new Intent(getApplicationContext(),Act2.class);
String s= ed.getText().toString();
i.putExtra("EXTRA_MESSAGE", s);
startActivity(i);
}
});
}
}
Act2.java
package com.example.myweb;
import org.w3c.dom.Text;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Act2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act2);
Button b1=(Button)findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextView tv = (TextView)findViewById(R.id.tv1);
Intent i =getIntent();
String msg = i.getStringExtra(MainActivity.EXTRA_MESSAGE);
tv.setText(msg);
setContentView(tv);
}
});
}
}
please help me.thank you
Just try this:
In MainActivity.java:
declare Button b and EditText ed as a class field (i.e. keep it outside onCreate())
class MainActivity.java
{
Button b;
EditText ed;
...
onCreate() {
...
b =(Button)findViewById(R.id.button1);
ed = (EditText)findViewById(R.id.edit_msg);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this,Act2.class);
String s= ed.getText().toString();
i.putExtra("EXTRA_MESSAGE", s);
startActivity(i);
}
});
...
In Act2.java:
...
Intent i = getIntent();
String msg = i.getStringExtra("EXTRA_MESSAGE");
TextView tv = (TextView)findViewById(R.id.tv1);
tv.setText(msg);
Button b1=(Button)findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
...
You are using
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
So, in MainActivity
i.putExtra(EXTRA_MESSAGE, s);
startActivity(i);
(OR)
String message = intent.getStringExtra("EXTRA_MESSAGE");
also,
i.putExtra("EXTRA_MESSAGE", s);
Change
String msg = i.getStringExtra(MainActivity.EXTRA_MESSAGE);
to
String msg = i.getStringExtra("EXTRA_MESSAGE");
MainActivity.EXTRA_MESSAGE would mean a static variable of MainActivity.java class. Hope you get the difference. You need the variable EXTRA_MESSAGE which you had put in intent i
Edit: For your crash, we need a logcat o/p and activity, manifest code. But possible reasons:
Activity Act2.java is not declared in manifest file.
You said you have only a textview in second activity. But you are trying to get button1 from act2.xml. So you are getting Force close.
Change Act2.java 's onCreate() as
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act2);
TextView tv = (TextView)findViewById(R.id.tv1);
Intent i =getIntent();
String msg = i.getStringExtra("EXTRA_MESSAGE");
tv.setText(msg);
}
You don't need to call setContentView(tv); as it is already there in act2.xml and you are using setContentView(R.layout.act2);
Hope your problem gets solved.
Use this code
**MainActivity.java**
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.button1);
final EditText editText = (EditText)findViewById(R.id.editText1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
intent.putExtra("EXTRA_MESSAGE", editText.getText().toString());
startActivity(intent);
}
});
}
}
**activity_main.xml**
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="74dp"
android:layout_marginTop="26dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/editText1"
android:layout_marginLeft="69dp"
android:layout_marginTop="47dp"
android:text="Button" />
</RelativeLayout>
**SecondActivity.java**
public class SecondActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TODO Auto-generated method stub
setContentView(R.layout.second);
Intent intent = getIntent();
final String message = intent.getStringExtra("EXTRA_MESSAGE");
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextView textView = new TextView(SecondActivity.this);
textView.setText(message);
setContentView(textView);
}
});
}
}
**second.xml**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tester"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.tester.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity"></activity>
</application>
</manifest>
Try
Intent i = new Intent(MainActivity.this,Act2.class);
You are using
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
So, in MainActivity
i.putExtra(EXTRA_MESSAGE, s);
startActivity(i);
please review and change your code its works
package com.example.activityact;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b =(Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText ed = (EditText)findViewById(R.id.editText1);
Intent i = new Intent(getApplicationContext(),Act2.class);
String s= ed.getText().toString();
i.putExtra("EXTRA_MESSAGE", s);
startActivity(i);
}
});
}
}
Act2.java
package com.example.activityact;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class Act2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act2);
// TODO Auto-generated method stub
TextView tv = (TextView) findViewById(R.id.textView1);
Intent i =getIntent();
// String msg = i.getStringExtra(EXTRA_MESSAGE);
//String receiver = getIntent().getStringExtra(EXTRA_MESSAGE);
String message = i.getStringExtra("EXTRA_MESSAGE");
tv.setText(message);
}
}
Value of the EXTRA_MESSAGE variable in MainActivity.java is different then the key value that you are putting message in to intent.
MainActivity.java:
public final static String EXTRA_MESSAGE = "com.example.myweb";
i.put("EXTRA_MESSAGE",s);
Act2.java: (you are accessing by extra_message variable which is not correct)
Intent i = getIntent();
String text = i.getStringExtra(MainActivity.EXTRA_MESSAGE); //WHICH IS DIFFER FROM THE KEY VALUE
So key value should be the same at the time of setting and getting the value. here while setting the value key is "EXTRA_MESSAGE" and while getting "com.example.myweb" so you will get null pointer exception.
SOLUTION:
just change the line in Act2.java
Intent i = getIntent();
String text = i.getStringExtra("EXTRA_MESSAGE");
Also check your menifest file for the activities declaration.
Hope it may help you.
Did you remember to correctly define your second activity in the manifest XML?
Check out this great guide for anything else you might have missed: http://developer.android.com/training/basics/firstapp/starting-activity.html

Android Intent not Carrying a Bundle of Strings

In below code, intent from the first activity to the second only carries 1 string and I need it to carry 2. I have tried to use bundle to achieve this but I think my problem is in this line
extras.putString(EXTRA_MESSAGE, message);
The second problem that you might notice is I need to get the text to be colored by using a string entered on the first page, however I need to spend more time researching this and the first problem is more important.
If someone could help me to solve this first problem it would be most appreciated
I have all the relevant code below.
Main Activity
package com.example.myfirstapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
public final static String EXTRA_MESSAGE_COLOR = "com.example.myfirstapp.MESSAGE2";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
/** Called when the user clicks the Send button */
public void sendMessage (View view) {
Intent i = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
EditText editTextcolor = (EditText) findViewById(R.id.edit_message_color);
String message = editText.getText().toString();
String messagecolor = editTextcolor.getText().toString();
i.putExtra(EXTRA_MESSAGE_COLOR, messagecolor);
Bundle extras = new Bundle();
extras.putString(EXTRA_MESSAGE, message);
startActivity(i);
// Do something in response to button
}
}
Second Activity
package com.example.myfirstapp;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
import android.widget.TextView;
public class DisplayMessageActivity extends Activity {
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Bundle bundle = getIntent().getExtras();
String messagecolor = bundle.getString(MainActivity.EXTRA_MESSAGE_COLOR);
String message = bundle.getString(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextColor(getResources().getColor(R.color.blue));
textView.setTextSize(100);
textView.setText(message);
textView.setText(messagecolor);
// Set the text view as the activity layout
setContentView(textView);
// Make sure we're running on Honeycomb or higher to use ActionBar API
if (Build.VERSION.SDK_INT >=Build.VERSION_CODES.HONEYCOMB) {
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
XML File
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical" >
<EditText
android:id="#+id/edit_message"
android:layout_weight="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="95dp"
android:layout_marginTop="50dp"
android:textSize="20sp"
android:hint="#string/edit_message" />
<EditText
android:id="#+id/edit_message_color"
android:layout_weight="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="95dp"
android:layout_marginTop="50dp"
android:textSize="20sp"
android:hint="#string/edit_message_color" />
<Button
android:layout_weight="0"
android:layout_marginLeft="55dp"
android:layout_marginTop="50dp"
android:layout_width="250dp"
android:layout_height="70dp"
android:text="#string/button_send"
android:textColor="#color/royal_blue"
android:textStyle="bold"
android:textSize="30sp"
android:background="#color/pale_green"
android:onClick="sendMessage" />
</LinearLayout>
Just put that extras Bundle in intent extra and then in next activity you will get the correct response
Bundle extras = new Bundle();
extras.putString(EXTRA_MESSAGE, message);
extras.putString(EXTRA_MESSAGE_COLOR, messagecolor );
i.putExtras(extras);
startActivity(i);
try this..
public void sendMessage (View view) {
Intent i = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
EditText editTextcolor = (EditText) findViewById(R.id.edit_message_color);
String message = editText.getText().toString();
String messagecolor = editTextcolor.getText().toString();
i.putExtra(EXTRA_MESSAGE_COLOR, messagecolor);
i.putExtra(EXTRA_MESSAGE, message);
startActivity(i);
// Do something in response to button
}
Just Edit your sendMessage method to
public void sendMessage (View view)
{
Intent i = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
EditText editTextcolor = (EditText) findViewById(R.id.edit_message_color);
String message = editText.getText().toString();
String messagecolor = editTextcolor.getText().toString();
i.putExtra(EXTRA_MESSAGE_COLOR, messagecolor);
startActivity(i);
}
Add id attribute in button in layout file and implement onClick method on this button in your main activity.
package com.example.myfirstapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
public final static String EXTRA_MESSAGE_COLOR = "com.example.myfirstapp.MESSAGE2";
Button b;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button)findVIewById(R.id.buttonSend);
b.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
sendMessage();
}
)};
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
/** Called when the user clicks the Send button */
public void sendMessage (View view) {
Intent i = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
EditText editTextcolor = (EditText) findViewById(R.id.edit_message_color);
String message = editText.getText().toString();
String messagecolor = editTextcolor.getText().toString();
i.putExtra(EXTRA_MESSAGE_COLOR, messagecolor);
Bundle extras = new Bundle();
extras.putString(EXTRA_MESSAGE, message);
startActivity(i);
// Do something in response to button
}
}
And your layout code :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical" >
<EditText
android:id="#+id/edit_message"
android:layout_weight="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="95dp"
android:layout_marginTop="50dp"
android:textSize="20sp"
android:hint="#string/edit_message" />
<EditText
android:id="#+id/edit_message_color"
android:layout_weight="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="95dp"
android:layout_marginTop="50dp"
android:textSize="20sp"
android:hint="#string/edit_message_color" />
<Button
android:layout_weight="0"
android:id="#+id/buttonSend"
android:layout_marginLeft="55dp"
android:layout_marginTop="50dp"
android:layout_width="250dp"
android:layout_height="70dp"
android:text="#string/button_send"
android:textColor="#color/royal_blue"
android:textStyle="bold"
android:textSize="30sp"
android:background="#color/pale_green" />
</LinearLayout>
Use getIntent().getExtras().getStringArray("key") to get array.

Categories

Resources