【问题标题】:intent issue when passing from one activity to the other从一项活动传递到另一项活动时的意图问题
【发布时间】:2012-02-25 06:59:25
【问题描述】:
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 应用程序中从一个活动传递到另一个活动。所以当第二个按钮被点击时,程序必须加载新的活动。我得到的错误是行 - Intent viewDataIntent = new Intent(this, Survey2.class);它说构造函数 Intent(SurveyActivity.clicker, Class) 未定义。有什么想法吗?

【问题讨论】:

    标签: android android-activity android-intent


    【解决方案1】:
    Intent viewDataIntent = new Intent(this, Survey2.class);
    

    应该是

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

    因为SurveyActivity.this 是您的实际Context

    【讨论】:

    • 这真的很有帮助,让 SurveyActivity.this 作为上下文是关键
    【解决方案2】:

    意图 viewDataIntent = new Intent(getApplicationContext(), Survey2.class);

    startActivity(viewDataIntent);

    【讨论】:

      【解决方案3】:

      注意你是在 View.OnClickListener 类中创建新的 Intent 对象,所以这是 OnClickListener 的引用,而

      public Intent (Context packageContext, Class cls) 需要一个上下文对象作为第一个参数,所以改为:

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

      【讨论】:

        猜你喜欢
        • 2012-01-24
        • 2014-09-19
        • 2014-09-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多