shaon007
shaon007

Reputation: 163

Whats wrong with this code(Using intent)?

i have done the below IntentApp application for testing intent. Its running fine but after i click the button1 in appActivity1 it show error- "Unfortunately the application have been stopped ". Where did i go wrong? Below is the codes:

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=".AppActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="This is Screen1" 
    android:textAppearance="?android:attr/textAppearanceLarge"
    />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="22dp"
    android:text="Click to go to another activity" 
    android:onClick="passIntentMethod"
    />

</RelativeLayout>

AppActivity.java

public class AppActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}
public void passIntentMethod()
{
    Intent myIntent = new Intent(this, App2Activity.class);
    myIntent.putExtra("Name", "Shaon");
    myIntent.putExtra("Age", 30);
    myIntent.putExtra("Salary", 30000.50);
    startActivity(myIntent);
    finish();
}
}

main2.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=".App2Activity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="I am Activity 2" 
    android:textAppearance="?android:attr/textAppearanceLarge"
    />
</RelativeLayout>

App2Activity.java

 public class App2Activity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main2);

    TextView txtvw = (TextView) findViewById(R.id.textView1);

    Bundle resultIntent = getIntent().getExtras();

    //String aa = getIntent().getStringExtra("name");
    //double xx = getIntent().getDoubleExtra(name, defaultValue)

    if(resultIntent != null)
    {
        String nameValue = resultIntent.getString("Name");
        int ageValue = resultIntent.getInt("Age");
        double salaryValue = resultIntent.getDouble("Salary");

        //Toast.makeText(this, nameValue + "-" + ageValue + "-" + salaryValue , 2000).show;
        txtvw.setText(nameValue + " - " + ageValue + " - " + salaryValue );

    }
}
}

Upvotes: 1

Views: 94

Answers (2)

Looking Forward
Looking Forward

Reputation: 3585

You need to create method like public void passIntentMethod(View v) and also add permission in manifest file

<activity android:name=".App2Activity " />

Upvotes: 1

Chintan Soni
Chintan Soni

Reputation: 25267

Simply add the parameter View view to your method passIntentMethod.

public void passIntentMethod(View view)
{
 ...
}

Upvotes: 2

Related Questions