在2个Activity之间传递字符串

时间:2012-12-31 15:23:37

标签: android xml android-intent

从数据类

我得到一个字符串并尝试将其传递给OpenedClass但是当我点击按钮时实际上没有发生任何事情。因为调试器没有显示错误。我认为我的活动不起作用,但我不知道为什么?

这是我的数据类

package com.android;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Data extends Activity implements OnClickListener {
    Button start, startFor;
    EditText sendET;
    TextView gotAnswer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.get);
    }

    private void initialize() {
        start = (Button) findViewById(R.id.bSA);
        startFor = (Button) findViewById(R.id.bSAFR);
        sendET = (EditText) findViewById(R.id.etSend);
        gotAnswer = (TextView) findViewById(R.id.tvGOT);
        start.setOnClickListener(this);
        startFor.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        case R.id.bSA:
            String bread = sendET.getText().toString();
            Bundle basket = new Bundle();
            basket.putString("key", bread);
            Intent a = new Intent(Data.this, OpenedClass.class);
            a.putExtras(basket);
            startActivity(a);
            break;

        case R.id.bSAFR:
            break;
        }
    }
}

这里我的代码与AndroidManifest.xml相关

<activity
            android:name=".Data"
            android:exported="false"
            android:label="Data" >
        </activity>
        <activity
            android:name=".OpenedClass"
            android:exported="false"
            android:label="OpenedClass" >
        </activity>

1 个答案:

答案 0 :(得分:1)

您没有在initialize()的“{1}}”中致电onCreate,请在setContentView之后将其称为:

   @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.get);

        initialize() ;  //<<< call initialize method here
    }