2012-02-25 5 views
0
package nidhin.survey; 

import android.app.Activity; 
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.*; 
import android.os.Bundle; 
import android.widget.CheckBox; 
import android.widget.CompoundButton; 
import android.widget.RadioGroup; 
import android.widget.CompoundButton.OnCheckedChangeListener; 
import android.widget.TextView; 

public class SurveyActivity extends Activity implements OnCheckedChangeListener 

{ 
CheckBox cb; 
String myChoice; 

RadioButton radio1; 
RadioButton radio2; 
RadioButton radio3; 
RadioButton radio4; 
RadioGroup rg; 
EditText text1; 
Button button1; 
Button button2; 

public void onCreate(Bundle savedInstanceState) 

{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    cb=(CheckBox)findViewById(R.id.check); 
    cb.setOnCheckedChangeListener(this); 
    RadioGroup rg=(RadioGroup)findViewById(R.id.rg); 
    radio1=(RadioButton)findViewById(R.id.radio1); 
    radio2=(RadioButton)findViewById(R.id.radio2); 
    radio3=(RadioButton)findViewById(R.id.radio3); 
    radio4=(RadioButton)findViewById(R.id.radio4); 
    // rg.setOnCheckedChangeListener(this); 
    rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 

     public void onCheckedChanged(RadioGroup group, int checkedId) { 

      switch(checkedId) 
      { 
      case R.id.radio1: 
       myChoice = "one"; 
       text1.setText(myChoice); 
       break; 
      case R.id.radio2: 
       myChoice = "two"; 
       text1.setText(myChoice); 
       break; 
      case R.id.radio3: 
       myChoice = "three"; 
       text1.setText(myChoice); 
       break; 
      case R.id.radio4: 
       myChoice = "four"; 
       text1.setText(myChoice); 
       break; 
      } 

     } 
    }); 
    text1=(EditText)findViewById(R.id.etext1); 
    text1.setText(myChoice); 
    button1 = (Button) findViewById(R.id.button01); 
    button1.setOnClickListener(new clicker()); 
    button2 = (Button) findViewById(R.id.button02); 
    button2.setOnClickListener(new clicker()); 
} 

    public void onCheckedChanged(CompoundButton buttonView, 
    boolean isChecked) 
    { 
     if (isChecked) 
      { 
      cb.setText("Yes , I have a car!"); 
      } 
     else 
      { 
      cb.setText(" "); 
      } 

        if (isChecked) { 
        TextView tv= new TextView (this); 
        tv.setText("You have a car , nice!"); 
         } 
    } 


     class clicker implements Button.OnClickListener 
     { 
       public void onClick(View v) 
       {       
        if(v==button1) 
        { 
         text1.setText(myChoice); 
         Toast.makeText(getBaseContext(), 

          "~~~~Successfully submitted~~~", 
          Toast.LENGTH_LONG).show(); 
        } 

         if(v==button2) 
        { 
         Intent viewDataIntent = new Intent(this, Survey2.class); 
         String myData = "You should see this"; 
         viewDataIntent.putExtra("valueOne", myData); 
         startActivity(viewDataIntent); 
        } 
       } 
     } 



} 

こんにちは、これは簡単な問題ですが、何とか解決策を見つけることができません。私は自分のAndroidアプリケーションで1つのアクティビティから別のアクティビティに移動しようとしています。だから、2番目のボタンをクリックすると、プログラムは新しいアクティビティを読み込まなければなりません。私が得るエラーは次の行です - Intent viewDataIntent = new Intent(this、Survey2.class); Intent(SurveyActivity.clicker、Class)のコンストラクタは定義されていません。何か案は? SurveyActivity.thisは、あなたの実際のContextあるので1つのアクティビティから別のアクティビティに移動するときのインテントの問題

答えて

1

あなたはView.OnClickListenerクラスに新しいインテントオブジェクトを作成している注意してください、これはOnClickListenerの参照です。

Intent viewDataIntent = new Intent(v.getContext(), Survey2.class); 
         String myData = "You should see this"; 
         viewDataIntent.putExtra("valueOne", myData); 
         startActivity(viewDataIntent); 

又は

Intent viewDataIntent = new Intent(SurveyActivity.this, Survey2.class); 
         String myData = "You should see this"; 
         viewDataIntent.putExtra("valueOne", myData); 
         startActivity(viewDataIntent); 
+0

でした。 –

3
Intent viewDataIntent = new Intent(this, Survey2.class); 

Intent viewDataIntent = new Intent(SurveyActivity.this, Survey2.class); 

でなければなりません。

公共テント(コンテキストpackageContext、クラスCLS)は最初のパラメータとしてコンテキストオブジェクトを必要とし、その代わりに行うのに対し、

+0

これは実際に役立っていました。コンテキストとしてSurveyActivity.thisを作成すると、本当に役に立ちましたキー –

3

意図viewDataIntent =新しいテント(getApplicationContext()、Survey2.class)。

startActivity(viewDataIntent);

関連する問題