这段代码有什么问题(使用意图)?

时间:2014-01-11 07:31:43

标签: android android-intent

我已经完成了以下用于测试意图的IntentApp应用程序。它运行正常,但在我单击appActivity1中的button1后,它显示错误 - “不幸的是应用程序已停止”。我哪里做错了? 以下是代码:

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 );

    }
}
}

2 个答案:

答案 0 :(得分:2)

只需将参数View view添加到您的方法passIntentMethod

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

答案 1 :(得分:1)

您需要创建类似public void passIntentMethod(View v)的方法,并在清单文件中添加权限

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