2014-01-11 21 views
1

我已經完成了以下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); 

    } 
} 
} 
+0

崩潰日誌添加權限?無論如何刪除完成()當你開始意圖..也許這是問題。 –

回答

2

簡單的參數View view添加到您的方法passIntentMethod

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

非常感謝! :-) – shaon007

+0

@ user2751449不客氣...... :) –

+2

@ user2751449,檢查此鏈接http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work並接受答案其他如果它可以幫助你... – InnocentKiller

1

你需要像public void passIntentMethod(View v)創建方法,並在清單文件

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