I currently have this in and android activity but when the button is clicked the application crashes. I cant find what;s wrong.
public class SearchActivity extends Activity implements OnClickListener{
private ListView recipes;
Intent intent;
Button button;
EditText input;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.searchlist);
button = (Button)findViewById(R.id.submit);
input = (EditText)findViewById(R.id.recipeName);
//recipes = (ListView)findViewById(R.id.recipes);
//recipes.setAdapter(new ArrayAdapter<String> (this, R.layout.main, getResources().getStringArray(R.array.BaconSandwich)));
button.setOnClickListener(this);
}
public void onClick(View clicked) {
if(clicked.getId() == R.id.submit) {
//recipes = (ListView)findViewById(R.array.recipes);
String value = input.getText().toString();
Toast.makeText(this, value, Toast.LENGTH_SHORT).show();
Intent i = new Intent(this, RecipeMethodActivity.class);
SearchActivity.this.startActivity(i);
}
}
}
The activity is set in the android manifest and the next activity is blank
The log cat message I get is:
Error in RecipeMethodActivity onCreate().
My RecipeMethodActivity is:
package com.finalyearproject.cookmefood;
import android.app.ListActivity;
import android.os.Bundle;
public class RecipeMethodActivity extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
i checked your code..
see below
public class DemoActivity extends Activity implements OnClickListener{
private Button button;
private EditText input;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button)findViewById(R.id.submit);
input = (EditText)findViewById(R.id.recipeName);
button.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId() == R.id.submit) {
//recipes = (ListView)findViewById(R.array.recipes);
String value = input.getText().toString();
Toast.makeText(this, value, Toast.LENGTH_SHORT).show();
Intent i = new Intent(this, RecipeMethodActivity.class);
DemoActivity.this.startActivity(i);
}
}
}
main.xml
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<EditText
android:id="#+id/recipeName"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
<Button
android:id="#+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
RecipeMethodActivity
package com.Demo;
import android.app.ListActivity;
import android.os.Bundle;
public class RecipeMethodActivity extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.new_layout);
}
}
new_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="#+id/#android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView>
</LinearLayout>
manifest file:
<uses-sdk android:minSdkVersion="3" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".DemoActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".RecipeMethodActivity"></activity>
</application>
Related
In this case two activities A and B should interact in that way that A calls B for a result.
But A never gets any data back from B.
This code looks like any tutorial or SO question I have seen.
The debugging session confirmed that setResult is called.
This problem is robbing me of my motivation, would be nice if someone could point out what I am missing.
EDIT: The code doesn't work when executed on my physical target device, but runs find on a nexus 5 device. What could I try to find out why?
MainActivity.java
package iifym.apps.rnoennig.de.myapplication;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onClick(View view) {
Intent in = new Intent(this, SecondActivity.class);
in.putExtra("mau", "bar");
startActivityForResult(in, 1);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// data is null ;(
Toast.makeText(this, String.valueOf(data), Toast.LENGTH_SHORT).show();
}
}
SecondActivity.java
package iifym.apps.rnoennig.de.myapplication;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class SecondActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Toast.makeText(this, String.valueOf(getIntent()), Toast.LENGTH_SHORT).show();
}
public void onClick(View view) {
setResult(RESULT_OK, new Intent().putExtra("foo", "bar"));
finish();
}
}
layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_gravity="center_horizontal"
android:onClick="onClick"
/>
</LinearLayout>
layout/second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button2"
android:layout_gravity="center_horizontal"
android:onClick="onClick"
/>
</LinearLayout>
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="iifym.apps.rnoennig.de.myapplication">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<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>
Works for me. (Same layouts and Manifest).
Main
public class MainActivity extends AppCompatActivity {
public static final int MAIN_REQUEST = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View v) {
Intent in = new Intent(MainActivity.this, SecondActivity.class);
in.putExtra("mau", "start 2nd");
startActivityForResult(in, MAIN_REQUEST);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MAIN_REQUEST) {
if (resultCode == RESULT_OK) {
Toast.makeText(MainActivity.this, data.getStringExtra("foo"), Toast.LENGTH_SHORT).show();
}
}
}
}
Second
public class SecondActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent sent = getIntent();
if (sent != null) {
Toast.makeText(SecondActivity.this, sent.getStringExtra("mau"), Toast.LENGTH_SHORT).show();
}
}
public void onClick(View v) {
setResult(RESULT_OK, new Intent().putExtra("foo", "finish 2nd"));
finish();
}
}
Please help me out how to start other activity
Main activity
package my.apps.news;
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;
public class MainActivity extends Activity {
Button btn1,btn2,btn3,btn4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.crazyworldview_button);
btn2 = (Button)findViewById(R.id.wordpress_button);
btn3 = (Button)findViewById(R.id.itsourplanet_button);
btn4 = (Button)findViewById(R.id.carsandbike_button);
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this, Blogger.class);
startActivity(intent);
}
});
btn2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this, Wordpress.class);
startActivity(intent);
}
});
btn3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this, Itsourplanet.class);
startActivity(intent);
}
});
btn4.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this, Carsandbikes.class);
startActivity(intent);
}
});
}
}
This is my layout 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"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<Button
android:id="#+id/crazyworldview_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/crazyworldview"
android:layout_margin="5dp"
android:onClick="crazyclick"/>
<Button android:id="#+id/wordpress_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/wordpress"
android:layout_margin="5dp"
android:onClick="wordclick"/>
<Button android:id="#+id/itsourplanet_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/itsourplanet"
android:layout_margin="5dp"
android:onClick="planetclick"/>
<Button android:id="#+id/carsandbike_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/carsandbikes"
android:layout_margin="5dp"
android:onClick="carsclick"/>
</LinearLayout>
This my manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="my.apps.news"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="21" />
<uses-permission android:name="android.premission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#drawable/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=".Blogger"></activity>
<activity android:name=".Wordpress"></activity>
<activity android:name=".Itsourplanet"></activity>
<activity android:name=".Carsandbikes"></activity>
</application>
</manifest>
Im not able to start activities with button there are four buttons and four activities in my app. i used webview in my other layouts
Tell me how to start the activities . thank you in advance
I have two classes; the first.class and second.class. On my first.class there is a button and on my second.class there is no button. I need it so when the button from the first.class is clicked there will be a text that will appear on the second.class with an option to save it.
first.class
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
public class first{
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
second.setText(text);
}
});
}
public class second{
private static String textFromFirst;
public static void setText(String text){
second.textFromFirst = text;
}
}
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" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="57dp"
android:layout_marginTop="23dp"
android:text="Click" />
</RelativeLayout>
MainActivity.java
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;
public class MainActivity extends Activity {
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent in = new Intent(getBaseContext(), Second.class);
startActivity(in);
}
});
}
}
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" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Second.java
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Second extends Activity {
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
tv = (TextView)findViewById(R.id.textView1);
}
#Override
public void onResume()
{
super.onResume();
tv.setText("Hello");
}
}
manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.f"
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.f.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=".Second" />
</application>
</manifest>
do this:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent p = new Intent(first.this,
second.class);
startActivity(p);
}
});
and then in second class load a xml file containing textview
package ......;
import java.io.File;
import java.io.IOException;
import dolphin.devlopers.com.R;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
public class gmail1 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second); //the file which contains textview and ui
TextView tv = (TextView)findViewById(R.id.textView1);
}
Manifest file:
<Activity
android:name="first">
</Activity>
<Activity
android:name="second">
</Activity>
second.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hi"
/>
</RelativeLayout >
Your question is a bit unclear.
When you say first.class and second.class, do you mean they both extends Activity?
Meaning, they are two different "Screens" in your app, and clicking the button on the first activity would launch the second and change its text? if so, You can use intents to pass info between activities. Something like this:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(first.this, second.class);
// NOW COMES THE IMPORTANT PART, Put the text you want to be passed
intent.putExtra("text_identifier", "The text to show");
startActivity(intent);
}
});
and in Second.java:
private TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
tv = (TextView) findViewById(R.id.textView);
// Retreive the text you sent eralier
String theText = getIntent().getStringExtra("text_identifier");
tv.setText(theText);
// Do any extra onCreate things
}
If it's not what you're after, let me know and I'll see what I can think off, but please try to explain a bit better
Good luck!
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
(Home.java), I am trying to create a loading screen that eventually leads to this page (Home.java.)
package com.androidpeople.splash;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class VirtualSkiInstructor extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textView = new TextView(this);
textView.setText("Main Activity");
setContentView(textView);
}
}
Loading Screen (SplashScreen.java). This is the code that creates the Splash Screen, and the loading page
package com.androidpeople.splash;
import your.custom.splash.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class SplashScreen extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
final int welcomeScreenDisplay = 3000;
/** create a thread to show splash up to splash time */
Thread welcomeThread = new Thread() {
int wait = 0;
#Override
public void run() {
try {
super.run();
while (wait < welcomeScreenDisplay) {
sleep(100);
wait += 100;
}
} catch (Exception e) {
System.out.println("EXc=" + e);
} finally {
startActivity(new Intent(SplashScreen.this,
VirtualSkiInstructor.class));
finish();
}
}
};
welcomeThread.start();
}
}
(splash.xml)
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center" android:background="#6B8AAD">
<TextView android:id="#+id/TextView01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textSize="18sp"
android:textStyle="bold" android:textColor="#fff"></TextView>
</LinearLayout>
(strings.xml)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Compass</string>
<string name="app_name">Compass</string>
</resources>
Add below code to add VirtualSkiInstructor activity into your manifest file.
<activity
android:name=".VirtualSkiInstructor"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Add VirtualSkiInstructor Activity in the AndroidManifest.xml file.
<activity android:name=".VirtualSkiInstructor"> </activity>