【问题标题】:Can't transition from a Dialog to an Activity无法从 Dialog 转换为 Activity
【发布时间】:2013-03-02 08:13:18
【问题描述】:

我似乎无法从 Dialog 转换为 Activity,我想知道是否有人足够聪明地找出原因!

这是我在提交此项目之前需要修复的最后一个错误。

包 com.mkyong.android;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class AppActivity extends Activity {

final Context context = this;
private Button button;

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    button = (Button) findViewById(R.id.button1);

    // add button listener
    button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            context);

        // set title
        alertDialogBuilder.setTitle("Settings Menu");

        // set dialog message
        alertDialogBuilder
            .setMessage("Link or Delete?")
            .setCancelable(false)
            .setPositiveButton("Link",new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,int id) {
            //start new activity

            Intent intentApp2Activity = new Intent(AppActivity.this, App2Activity.class);
            startActivity(intentApp2Activity);

            // if this button is clicked, close
            // current activity
            AppActivity.this.finish();
        }
      })
            .setNegativeButton("Delete",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    // if this button is clicked, just close
                    // the dialog box and do nothing
                    dialog.cancel();
                }
            });

            // create alert dialog
            AlertDialog alertDialog = alertDialogBuilder.create();

            // show it
            alertDialog.show();
        }
    });
}

}

03-02 08:16:32.219: E/AndroidRuntime(5484): FATAL EXCEPTION: main
03-02 08:16:32.219: E/AndroidRuntime(5484): java.lang.Error: Unresolved compilation problems: 
03-02 08:16:32.219: E/AndroidRuntime(5484): Intent cannot be resolved to a type
03-02 08:16:32.219: E/AndroidRuntime(5484): Intent cannot be resolved to a type
03-02 08:16:32.219: E/AndroidRuntime(5484): at com.mkyong.android.AppActivity$1$1.onClick(AppActivity.java:44)
03-02 08:16:32.219: E/AndroidRuntime(5484): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
03-02 08:16:32.219: E/AndroidRuntime(5484): at android.os.Handler.dispatchMessage(Handler.java:99)
03-02 08:16:32.219: E/AndroidRuntime(5484): at android.os.Looper.loop(Looper.java:137)
03-02 08:16:32.219: E/AndroidRuntime(5484): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-02 08:16:32.219: E/AndroidRuntime(5484): at java.lang.reflect.Method.invokeNative(Native Method)
03-02 08:16:32.219: E/AndroidRuntime(5484): at java.lang.reflect.Method.invoke(Method.java:511)
03-02 08:16:32.219: E/AndroidRuntime(5484): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-02 08:16:32.219: E/AndroidRuntime(5484): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-02 08:16:32.219: E/AndroidRuntime(5484): at dalvik.system.NativeStart.main(Native Method)

【问题讨论】:

  • 你能解释一下你遇到了什么问题,有没有例外
  • 道歉 - 单击“链接”后强制关闭(应该转换到其他活动)
  • 请发布 logcat 堆栈跟踪。顺便说一句,“贝宝”的东西既不想要也不想要。我已经编辑出来了。这是一个由志愿者管理的免费社区,旨在造福社区。​​span>
  • stackoverflow.com/questions/1124788/… - 你的代码还能编译吗?
  • 看起来 import android.content.Intent 不见了

标签: android eclipse android-intent android-activity


【解决方案1】:

确保您已导入 android.content.Intent,清理您的项目并重建。

这是一个不稳定的 Android 部分。作为异常提示,您有一个未解决的编译时问题,应该会产生编译时错误。对我来说,闻起来像是 ADT 或 Eclipse 中的错误(我假设您使用的是 Eclipse)。

【讨论】:

    猜你喜欢
    • 2018-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-07
    • 1970-01-01
    相关资源
    最近更新 更多