应用程序在尝试使用Intent传递数据时强制关闭

时间:2012-08-25 12:25:38

标签: android android-intent

我的主要活动包含很少的单选按钮。他们是一个名为config的按钮。 我想要做的是点击配置新活动将开始,并将显示选定的单选按钮文本。 但是当我点击配置应用程序被迫关闭时。我无法找出错误。 我已经通过以下链接了解意图 1)http://www.androidaspect.com/2012/07/passing-data-using-intent-object.html 2)http://www.androidaspect.com/2012/02/how-to-start-new-activity-using-intent.html 以下是主要活动的onclick代码:

configbtn.setOnClickListener(new Button.OnClickListener(){
        @Override
        public void onClick(View arg0) {            
            //Toast.makeText(getApplicationContext(), "You have clicked on 'Config'...", Toast.LENGTH_LONG).show();
            if(!mode.equals(""))
            {
            Intent i = new Intent("com.av.android.profiles.configActivity");
            Bundle myBundle = new Bundle();
            myBundle.putString("promode",mode);
            i.putExtras(myBundle);
            startActivityForResult(i, 1);
            }
             else
             {
                Toast.makeText(getApplicationContext(), "Please select profile mode to config...!", Toast.LENGTH_LONG).show();
             }

         }});

第二项活动代码:

public class configActivity extends Activity{


Bundle myBundle1 = new Bundle();
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.config);
    String myName = null;       
    Bundle extras = getIntent().getExtras();
    if(extras != null)
    {
        myName = extras.getString("promode");
    }


    TextView tvData = (TextView) findViewById(R.id.tvData);
    tvData.setText(myName);
    }}

提前致谢

2 个答案:

答案 0 :(得分:1)

使用

if(extras != null)
    {
        myName = extras.getString("promode");
    }

而不是

if(extras != null)
    {
        myName = extras.getString("Name");
    }

因为您将promode作为关键而不是Name

传递

答案 1 :(得分:0)

尝试

Intent i = new Intent(CurrentActvity.this,configActivity.class);
Bundle myBundle = new Bundle();
myBundle.putString("promode",mode);
i.putExtras(myBundle);
startActivityForResult(i, 1);

在清单文件中声明configActivity

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

并使用myName = extras.getString("promode");