android - 尝试在自定义对话框中使用 getIntent() 会出现错误。解决此错误的解决方案?

标签 android android-intent android-activity android-dialog

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

Bundle sendToDialog = new Bundle();
sendToDialog.putString("caloreis", strCalories);
Intent a = new Intent(CaloriesLogMainActivity.this, ActivityDialog.class);
a.putExtras(sendToDialog);

这是自定义对话 Activity ,我在其中尝试从该 Activity 接收 Intent 。 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();

    }
}

最佳答案

你必须扩展 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();

    }
}

关于android - 尝试在自定义对话框中使用 getIntent() 会出现错误。解决此错误的解决方案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25269973/

相关文章:

android - 尝试在 eclipse 中使用 phonegap 创建 android 应用程序时出现错误 "Invalid resource directory name"

java - 无法在未调用 Looper 准备的线程内创建处理程序

android - 如何在底部应用栏上的 FloatingActionButton(fab)上方显示 Snackbar( Material 设计)

android - 要求用户打开 Wi-Fi

android 自定义 Intent 选择器

java - android.widget.TextView 无法转换为 android.widget.EditText 5

Android Lint 提示 Activity 未在 AndroidManifest 中注册,但它是

android - 有没有办法自动接听来电?

java - 优化 Android 上的堆大小

android - onclick 通知,在 Android 中启动一个 Activity