【问题标题】:Issues sharing a string between activities在活动之间共享字符串的问题
【发布时间】:2021-11-26 19:28:25
【问题描述】:

我是 Android 编程的新手,在尝试从一个活动和另一个活动传递字符串时遇到问题。 我已经在清单文件中添加了所需的代码。 我想从 MainActivity 中的用户输入中获取“myName”并将其传递给 activity_matching,但在第二个文件中,变量始终为空。 我不明白我做错了什么,所以我在这里。

package com.ium.example.packageName;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity{
    String myName;
    EditText edit;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_main);
        edit = findViewById(R.id.nickName);
        myName = edit.getText().toString();
        Button loginButton = findViewById(R.id.button);
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(MainActivity.this, activity_matching.class);
                i.putExtra("myName", myName);
                startActivity(i);
            }
        });
    }
}

上面的代码与“MainActivity”相关,下面的代码与“activity_matching”相关

    package com.ium.example.packageName;

import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.snackbar.Snackbar;

import java.util.Random;

public class activity_matching extends AppCompatActivity {
    float x1,x2,y1,y2;
    int s;
    String[] names = new String[17];
    public String myName;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_matching);
    }

    public boolean onTouchEvent(MotionEvent touchEvent){
        switch(touchEvent.getAction()) {
            case MotionEvent.ACTION_DOWN:
                x1 = touchEvent.getX();
                y1 = touchEvent.getY();
                break;

            case MotionEvent.ACTION_UP:
                x2 = touchEvent.getX();
                y2 = touchEvent.getY();
                Intent i = new Intent(activity_matching.this, SwipeLeft.class);
                i.putExtra("num", s);
                i.putExtra("arr", names);
                startActivity(i);
                break;
        }
        return false;
    }

    public void onBtnClick(View v) {
        TextView txtHello = findViewById(R.id.txtMessage);
        Intent b = getIntent();
        myName = b.getStringExtra("myName");
        String swipeLeft = "Swipe (a destra o sinistra) per l'argomento di discussione";
        Snackbar doSwipe = Snackbar.make(v, swipeLeft, 6000);
        Random rand = new Random(System.currentTimeMillis());
        names = new String[]{"Name1", "Name2", "Name3", "Name4", "Name5", "Name6", "Name7", "Name8",
                             "Name9", "Name10", "Name11", "Name12", "Name13", "Name14", "Name15",
                             "Name16", "Name17"};
        re:
        {
            s = rand.nextInt(names.length);
            if (myName.equals(names[s]))
                break re;
            txtHello.setText(myName + " vai a parlare con " + names[s] + "!");
            doSwipe.show();
        }
    }
}

感谢所有回答的人:3 祝你有美好的一天^-^

【问题讨论】:

标签: android android-studio variables android-activity


【解决方案1】:

尝试在 onCreate 方法中获取意图的额外数据可能会有所帮助。我不确定这是否会有所帮助,但我在复制您的问题时遇到了麻烦。

【讨论】:

    【解决方案2】:

    在 MainActivity 中,在单击 loginButton 之前分配 myName。那时您的 EditText 仍然是空的。移动:

    myName = edit.getText().toString();
    

    到onClick这一行上方

    i.putExtra("myName", myName);
    

    【讨论】:

    • 爱你,它有效,只是因为我心不在焉
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-31
    • 1970-01-01
    • 1970-01-01
    • 2012-07-29
    • 2017-10-25
    相关资源
    最近更新 更多