Java Android意图包传递总是返回null

Java Android意图包传递总是返回null,java,android,string,list,bundle,Java,Android,String,List,Bundle,我正在尝试将文本从自定义对话框传递到主活动。bundle总是显示为null,而不是我试图传递的值,我不知道为什么。我已经试着研究过类似的问题,但我还没有找到解决办法 对话 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_add_ingredients_dialog);

我正在尝试将文本从自定义对话框传递到主活动。bundle总是显示为null,而不是我试图传递的值,我不知道为什么。我已经试着研究过类似的问题,但我还没有找到解决办法

对话

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

    Button addButton = findViewById(R.id.addButton);
    addButton.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(AddIngredientsDialog.this,
                            android.R.layout.select_dialog_item, fruits);
                    editText.setAdapter(arrayAdapter);
                    text = editText.getText().toString();

                    Intent intent = new Intent(AddIngredientsDialog.this, AddIngredientsActivity.class);
                    intent.putExtra("Text", text);
                    startActivity(intent);
                    dialog.dismiss();
            }

     });
}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u add\u Components\u对话框);
Button addButton=findViewById(R.id.addButton);
addButton.setOnClickListener(
新建视图。OnClickListener(){
@凌驾
公共void onClick(视图v){
ArrayAdapter ArrayAdapter=新的ArrayAdapter(AddIngredientsDialog.this,
android.R.layout.select_对话框_项目,水果);
setAdapter(arrayAdapter);
text=editText.getText().toString();
意向意向=新意向(AddIngredientsDialog.this,AddIngredientsActivity.class);
意图。额外(“文本”,文本);
星触觉(意向);
dialog.dismise();
}
});
}
家庭活动

 ingredientList = findViewById(R.id.listView);
 ArrayList<String> ingredients = new ArrayList<>();
 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_ingredients);

    //bundle
    Bundle extras = getIntent().getExtras();
    text = extras.getString("Text");

    button = findViewById(R.id.add);
    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            ingredients.add(text);
            adapter = new ArrayAdapter<>(AddIngredientsActivity.this, android.R.layout.simple_list_item_1, ingredients);
            ingredientList.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }
    });
}
ingredientList=findviewbyd(R.id.listView);
ArrayList Components=新的ArrayList();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u add\u components);
//捆
Bundle extras=getIntent().getExtras();
text=extras.getString(“text”);
按钮=findViewById(R.id.add);
setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图arg0){
添加(文本);
adapter=new ArrayAdapter(AddIngredientsActivity.this,android.R.layout.simple\u list\u item\u 1,配料);
IngreditList.setAdapter(适配器);
adapter.notifyDataSetChanged();
}
});
}

在家庭活动中,尝试使用
getIntent().getStringExtra(“文本”)
,而不是
extras.getString(“文本”)

问题可能就在这里

`text = editText.getText().toString();`

确保您已在EditText中编写了一些内容,最好使用startActivityForResult启动对话活动。检查这个答案

`text = editText.getText().toString();`