上下文不是上下文? (不启动对话框)

时间:2012-05-12 14:59:44

标签: android android-activity android-context

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

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

  1. 为什么设备/ android版本的区别?
  2. 上下文应该不一样吗?我知道Application Context&查看上下文不同。但不确定为什么这应该有效。我知道我不应该使用getApplicationContext()作为视图..但仍然有点困惑。
  3. (请注意MyTestActivity是MyActivity的子类)

    编辑:在选项#1中,MyActivity从类变量中获取Context并使用它来启动对话框。 在选项#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();
            }
        }
    

1 个答案:

答案 0 :(得分:1)

答案很简单。如果您未在覆盖super.onCreate(savedInstanceState);中拨打onCreate(),则会收到垃圾而不是context。如果您致电super.onCreate(savedInstanceState);,则可以使用context