2012-10-08 40 views
0

我想從一個頁面中的值傳遞到另一個網頁我的代碼是:我試圖將值從一個頁面傳遞到另一個頁面,但它不起作用?

public class main extends Activity implements OnClickListener { 

private static final String TAG = "AskMeShowMe2"; 
private String choice = null; 
private String fname = null; 
private Menu myMenu = null; 
final static int START =0; 



@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Log.i(TAG,"Main - onCreate() ..."); 
    setContentView(R.layout.activity_main); 

    View promptButton = findViewById(R.id.submit_button); 
    promptButton.setOnClickListener((OnClickListener) this); 
} 

public void onClick(View v) { 
    RadioGroup radioGroup = (RadioGroup) findViewById(R.id.greetingsRadioGroup); 
    int checkedRadioButton = radioGroup.getCheckedRadioButtonId(); 

    switch (checkedRadioButton) { 
    case R.id.mrButton : 
     choice = "Mr."; 
     break; 
    case R.id.mrsButton : 
     choice = "Mrs."; 
     break; 
    case R.id.msButton: 
     choice = "Ms."; 
     break; 
    case R.id.drButton: 
     choice = "Dr."; 
     break; 
    } 
    switch(v.getId()){ 
    case R.id.submit_button: 
     Intent j = new Intent(this, Info.class); 
     j.putExtra("choice", choice); 
     startActivity(j); 
     fname=String.valueOf(R.id.firstname); 
     Intent t = new Intent(this, Info.class); 
     j.putExtra("fname", fname); 
     startActivity(t); 

     break; 
    } 
} 

和它的其他Info.Class:

public class Info extends Activity { 
private static final String TAG = "Info"; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.info); 

    Intent myIntent = getIntent(); 
    Bundle b = myIntent.getExtras(); 
    String choice = b.getString("choice"); 
    String fname = b.getString("fname"); 

    Log.i(TAG,"selection: " + choice); 
    TextView textView = (TextView) findViewById(R.id.showmeText); 
    textView.append(": " + choice); 
    TextView textView1 = (TextView) findViewById(R.id.fname); 
    textView1.append(": " + fname); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.my_menu, menu); 
    return true; 
} 

}

當我運行這個程序,然後我的Info.class頁面不顯示當我點擊提交按鈕,你能幫我解決它嗎?

回答

1

改變開關塊以下幾點:

switch(v.getId()){ 
    case R.id.submit_button: 
     Intent j = new Intent(this, Info.class); 
     j.putExtra("choice", choice); 
     fname=String.valueOf(R.id.firstname); 
     j.putExtra("fname", fname); 
     startActivity(j); 

     break; 
    } 

當你開始活動信息兩次,把羣衆演員只有一個意向,目前正在起步較早。

1

使用

Intent j = new Intent(this, Info.class); 
j.putExtra("choice", choice); 
fname=String.valueOf(R.id.firstname); 
j.putExtra("fname", fname); 
startActivity(j); 

,而不是

Intent j = new Intent(this, Info.class); 
    j.putExtra("choice", choice); 
    startActivity(j); 
    fname=String.valueOf(R.id.firstname); 
    Intent t = new Intent(this, Info.class); 
    j.putExtra("fname", fname); 
    startActivity(t); 

監守目前你第一次爲intent jintent t秒時間兩次調用startActivity因此將其刪除一個,把兩個選擇,FNAME單意圖和只調用一次startActivity。

+0

感謝它的工作 – user1050667

+0

@ user1050667:最受歡迎的朋友!!! –

+0

以上ans正在運行! –

相關問題