2012-05-13 2 views
-1

間で変数を渡す:エラーこれは私のManual.javaファイルで活動

public class Manual extends Activity implements OnClickListener{ 
    Button create; 
    TextView gotAnswer, NoAnsV; 
    EditText NoQues, NoAnswer; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.manualask);  
     NoQues = (EditText) findViewById (R.id.NoQues); 
     NoAnswer = (EditText) findViewById (R.id.NoAnsw); 
     create = (Button) findViewById (R.id.create); 
     create.setOnClickListener(this);   
    } 
    public void onClick(View v) { 
     // TODO Auto-generated method stub  
     switch (v.getId()){  
     case R.id.create: 
      Intent intent = new Intent(Manual.this, MCQSample.class); 
      Bundle b = new Bundle(); 
      Integer Number = Integer.parseInt(NoQues.getText().toString());   
      intent.putExtras(b); 
      startActivity(intent); 
      finish(); 
      break; 

    } 
    } 
} 

その後MCQSample.java:

public class MCQSample extends Activity{ 

    TextView title; 
    String gotBread; 
    int value; 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.mcqsample); 
     title = (TextView) findViewById(R.id.abc); 

     Bundle b = getIntent().getExtras(); 
     int value = b.getInt("key", 0); 
     title.setText(value); 
    } 
} 

その後mcqsample.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_gravity="center" 
    android:orientation="vertical" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:text="Text" 
     android:id="@+id/abc"/> 

</LinearLayout> 

両方私が既にAndroidManifestに追加したクラス。

Manual.javaで[作成]ボタンをクリックすると、常にクラッシュします。私の授業に何が悪いですか?あなたがバンドルに番号を設定しない

答えて

4

、あなたがBundle#putIntを呼び出す必要があります:

Bundle b = new Bundle(); 
Integer number = Integer.parseInt(NoQues.getText().toString());   
b.putInt("key", number); 
intent.putExtras(b); 

(クラッシュの原因となる)第二の問題は、あなたがint型、テキストはないように設定する必要があること、である:

title.setText("" + value); 

それ以外の場合は、id = valueの文字列を探しますが、そのようなIDは存在しません(TextView#setText(int)参照)。

+0

ああTKSヨタバイト。私は一日中この部分をくっついた! ~~。私はちょうどAndroidを学んだ –

+0

あなたを歓迎します。幸せなコーディング:) – MByD

+0

btw最後の質問、私たちはどのようにintの代わりに配列を転送できますか?方法は同じですか? –

0

マニュアル

Intent intent = new Intent(Manual.this, MCQSample.class); 
intent.putExtras("val",NoQues.getText().toString()); 
startActivity(intent); 

MCQSample

int value= Integer.parseInt(getIntent().getExtras().getString("val")); 
関連する問題