Android 尝试在自定义对话框中使用getIntent()时出现错误。解决此错误的方法?

Android 尝试在自定义对话框中使用getIntent()时出现错误。解决此错误的方法?,android,android-intent,android-activity,android-dialog,Android,Android Intent,Android Activity,Android Dialog,我正在从活动向自定义对话框发送字符串。 在这个活动中,我创建了一个bundle并插入字符串,然后将其发送到对话活动 Bundle sendToDialog = new Bundle(); sendToDialog.putString("caloreis", strCalories); Intent a = new Intent(CaloriesLogMainActivity.this, ActivityDialog.class); a.putExtras(sendToDialog); 这是我试

我正在从活动向自定义对话框发送字符串。
在这个活动中,我创建了一个bundle并插入字符串,然后将其发送到对话活动

Bundle sendToDialog = new Bundle();
sendToDialog.putString("caloreis", strCalories);
Intent a = new Intent(CaloriesLogMainActivity.this, ActivityDialog.class);
a.putExtras(sendToDialog);
这是我试图从活动中接收意图的自定义对话框活动。 getIntent()将作为错误出现。我怎样才能避免这个错误呢

public class ActivityDialog {
    Context mContext;
     Date mDate;
    public ActivityDialog(Context context, CaloriesLogMainActivity caloriesLogMainActivity) {
        mContext = context;

    }  
    public void show() {
        LayoutInflater factory = LayoutInflater.from(mContext);
        View view = factory.inflate(R.layout.dialog_activity, null);
        AlertDialog.Builder builder = new AlertDialog.Builder((mContext));
        final EditText calories = (EditText) view.findViewById(R.id.etCalories);

        Bundle recieveFromActivity = getIntent().getExtras();
        String strCaloreis = recieveFromActivity.getString("calories");
        calories.setText(strCaloreis);
        builder.setTitle(R.string.edit_log_title);
        builder.setNegativeButton(R.string.create_log_negative_button,
                new OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {

                    }
                });

        builder.setNeutralButton(R.string.create_log_neutral_button, null);
        builder.setPositiveButton(R.string.create_log_positive_button, null);
        builder.create().show();

        builder.setView(view);
        builder.create().show();

    }
}

这可能是因为您在
sendToDialog.putString(“caloreis”,strColories)中的拼写错误因为您后来将额外的热量称为“卡路里”,而不是“卡路里”。(我试图将其编辑,但长度不超过6个字符…

您的
ActivityDialog
类是一个活动,因此应该扩展
Activity
)。这是一条一般规则,但具体地说,这里的问题是由于
getIntent()
是在
Activity
中定义的,因此您必须扩展它才能使用它。

您必须扩展Activity类,然后只有您才能访问getIntent()

所以你的代码应该是这样的

public class ActivityDialog extends Activity{
    Context mContext;
     Date mDate;

 protected void onCreate(Bundle savedInstanceState){
   //here you can call getIntent()
 }
    public void show() {
     // or even from here ,you can call getIntent()
        LayoutInflater factory = LayoutInflater.from(mContext);
        View view = factory.inflate(R.layout.dialog_activity, null);
        AlertDialog.Builder builder = new AlertDialog.Builder((mContext));
        final EditText calories = (EditText) view.findViewById(R.id.etCalories);

        Bundle recieveFromActivity = getIntent().getExtras();
        String strCaloreis = recieveFromActivity.getString("calories");
        calories.setText(strCaloreis);
        builder.setTitle(R.string.edit_log_title);
        builder.setNegativeButton(R.string.create_log_negative_button,
                new OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {

                    }
                });

        builder.setNeutralButton(R.string.create_log_neutral_button, null);
        builder.setPositiveButton(R.string.create_log_positive_button, null);
        builder.create().show();

        builder.setView(view);
        builder.create().show();

    }
}

ActivityDialog
是否扩展
Activity
?要想让您调用
getIntent()
,需要使用该命令。你看到的错误到底是什么?不,不是。getintent的下划线是红色的。啊,这正是问题所在。不管怎样,您的所有活动都应该扩展类
Activity
,但具体来说,您的问题是因为
getIntent()
Activity
中定义了
Activity,所以您需要扩展它才能使用它。还要注意马克N的回答中指出的打字错误:)谢谢。把你的答案贴在下面,这样我就可以接受你的答案了。我刚才想到的另一个注意事项是,一旦这个类扩展了活动,它将拥有自己的适当上下文。在这种情况下,我认为您应该能够删除构造函数和
mContext
变量,然后重构活动以使用更传统的生命周期方法,如
onCreate()
等。否则,我不确定这是否能按预期工作。另外,如果您只想在另一个活动中显示这个对话框,您可以考虑将其扩展为<代码> AlrtCudio,并通过构造函数传递字符串,而不是<代码>意图< /代码>。不过只有我的两分钱。很抱歉没回来。我更改文本只是为了显示,并在问题上搞砸了,但在我的原始代码中它是好的。