0

-- 这可能不是问题 -- 在修复设备并尝试恢复后,我现在无法使用设备重现此问题。

所以我有点困惑,但这里有一些代码。我更改了代码以简化它,所以我可能有错字,但这是我所看到的“要点”。但基本上在某些设备上示例 1 不会启动对话框,而选项 2 在所有设备上启动它:

  1. 为什么设备/安卓版本不同?
  2. 上下文不应该相同吗?我知道应用程序上下文和视图上下文是不同的。但不确定为什么这应该起作用。我知道我不应该使用getApplicationContext()视图..但仍然有点困惑。

(请注意 MyTestActivity 是 MyActivity 的子类)

编辑:在选项 #1 中,MyActivity 从类变量中获取上下文并使用它来启动对话框。在选项#2 MyTestActivity 中获取上下文(应该与 MyActivity 相同的上下文?)并将其传递给 showMessage。(showMessage 在示例 #2 中采用了一个新参数)

示例 #1

///////////////////////////////////////////
// THIS Example works on Galaxy Nexus, but not on some other devices, such as : 
// SAMSUNG-SGH-I997. (infuse) - this just simply does not display anything.

public class MyTestActivity extends MyActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_layout);
        Intent i = new Intent(MyTestActivity.this, ActivityTwo.class);
        startActivityForResult(i, Constants.FROM_ACTIVITY);
    }

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) {     
        super.onActivityResult(requestCode, resultCode, data); 
        IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode, 
                   resultCode, data);
        if ((intentResult != null) && (resultCode == RESULT_OK)) {
             playsound(SOUND_DING); // User hears this - SoundPool
             showMessage("Test Title","Test Message"); // User does not see message
        }
    }
}

public class MyActivity extends Activity {
    private Context mContext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext=this;
    }

    public void showMessage(String title, String message) {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
    builder.setTitle(title);
    builder.setMessage(message)
               .setPositiveButton(mContext.getString(R.string.ok), 
                new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {

                            finish();

                   }
               });
    AlertDialog alert = builder.create();
    alert.show();
    }
}

示例#2

/////////////////////////////////////////////////////////////
// This works on all devices
    public class MyTestActivity extends MyActivity {
        private Context context;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.my_layout);
            context = this;
        Intent i = new Intent(MyTestActivity.this, ActivityTwo.class);
        startActivityForResult(i, Constants.FROM_ACTIVITY);
        }

        @Override 
        public void onActivityResult(int requestCode, int resultCode, Intent data) {     
            super.onActivityResult(requestCode, resultCode, data); 
            IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode, 
                       resultCode, data);
            if ((intentResult != null) && (resultCode == RESULT_OK)) {
                 playsound(SOUND_DING); // user hears this
                 showMessage("Test Title","Test Message", context); // user sees this
            }
        }
    }

    public class MyActivity extends Activity {
        private Context mContext;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mContext=this;
        }

        public void showMessage(String title, String message, Context context) {
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(title);
        builder.setMessage(message)
                   .setPositiveButton(context.getString(R.string.ok), 
                    new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {

                                finish();

                       }
                   });
        AlertDialog alert = builder.create();
        alert.show();
        }
    }
4

1 回答 1

1

答案很简单。如果你不调用super.onCreate(savedInstanceState);你的覆盖,onCreate()你会得到一个垃圾而不是context. 如果你打电话super.onCreate(savedInstanceState);,你会得到一个工作context

于 2012-05-12T16:07:41.367 回答