【问题标题】:Trouble with Intent in Android StudioAndroid Studio 中的 Intent 问题
【发布时间】:2016-03-13 17:14:08
【问题描述】:

我一直在尝试让一个字符串从一个 java 类转到另一个我的项目。我一直在试验的代码不起作用。当我按下按钮时,我知道它会打开另一个 Java 类,因为它创建了另一个布局,但它不显示字符串。请帮帮我。

第一个 Java 类:

public class MainActivity extends AppCompatActivity {

private Button button;
Context context;
private EditText editText;
String number = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    context = this;

    editText = (EditText) findViewById(R.id.editText);
    button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (editText.getText().toString() != null) {
                String value = "value";
                Intent secondscreenIntent = new Intent(context, SecondScreenJ.class);
                secondscreenIntent.putExtra("Number", editText.getText().toString());
                startActivity(secondscreenIntent);


            }
        }
    });
}
}

第二个 Java 类:

public class SecondScreenJ extends Activity {

String number = null;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.secondscreen);

    textView = (TextView) findViewById(R.id.textView);

    Bundle extras = getIntent().getExtras();
    if (extras != null){
        number = extras.getString("number");
    }

    textView.setText(number);

}


}

【问题讨论】:

  • 您使用"Number" 作为放置键,"number" 用于检索。请注意前者大写N,后者小写n
  • 是的,没错。您的密钥是“数字”,您正在尝试使用“数字”。所以您的代码不起作用
  • 当您打开 10 个帖子的那一刻,您可能知道答案,当您找到其中一个并回答时,已经有 3 人回答了:D

标签: java android android-intent android-studio


【解决方案1】:

您将“数字”作为键,但在您的第二个活动中,您试图检索“数字” 所以将数字更改为数字,它将起作用。 区分大小写。

【讨论】:

    【解决方案2】:

    不要以这种方式对您的密钥进行硬编码。 只需在 MainActivity 中声明公共静态变量并从 SecondScreenJ 中使用它

    public class MainActivity extends AppCompatActivity {
    
    private Button button;
    Context context;
    private EditText editText;
    public static String NUMBER_KEY = "Number";
    String number = null;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    context = this;
    
    editText = (EditText) findViewById(R.id.editText);
    button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (editText.getText().toString() != null) {
                String value = "value";
                Intent secondscreenIntent = new Intent(context, SecondScreenJ.class);
                secondscreenIntent.putExtra(NUMBER_KEY , editText.getText().toString());
                startActivity(secondscreenIntent);
    
    
            }
        }
    });
     }
     }
    

    第二个 Java 类:

    public class SecondScreenJ extends Activity {
    
    String number = null;
    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.secondscreen);
    
     textView = (TextView) findViewById(R.id.textView);
    
     Bundle extras = getIntent().getExtras();
     if (extras != null){
        number = extras.getString(MainActivity.NUMBER_KEY);
     }
    
     textView.setText(number);
    
     }
    
    
    }
    

    【讨论】:

      【解决方案3】:

      使用钥匙放置和获取额外物品时要小心。它们区分大小写。在您的第二个活动中将 "number" 替换为 "Number"

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-04-06
        • 1970-01-01
        • 1970-01-01
        • 2019-03-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多