錯誤產生:
Java代碼{
dp.sh.Toolbar.CopyToClipboard(this);return false;
}" href="http://blog.csdn.net/zeng622peng/archive/2010/12/25/6097346.aspx#">
- private Context mcontext;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {mcontext = getApplicationContext();
- System.out.println("mcontext=" + mcontext);
-
- }
Java代碼{
dp.sh.Toolbar.CopyToClipboard(this);return false;
}" href="http://blog.csdn.net/zeng622peng/archive/2010/12/25/6097346.aspx#">
- new AlertDialog.Builder(mcontext)
- .setIcon(android.R.drawable.ic_dialog_alert)
- .setTitle("Warnning")
- .setMessage(
- "You forget to write the message. Do you want to fill out it ??")
- .setPositiveButton("Yes", positiveListener).setNegativeButton(
- "No", negativeListener).create().show();
導致報這個錯是在於new AlertDialog.Builder(mcontext),雖然這裡的參數是AlertDialog.Builder(Context context)但我們不能使用getApplicationContext()獲得的Context,而必須使用Activity,因為只有一個Activity才能添加一個表單。
解決方案:將new AlertDialog.Builder(Context context)中的參數用Activity.this(Activity是你的Activity的名稱)來填充就可以正確的建立一個Dialog了。
Java代碼{
dp.sh.Toolbar.CopyToClipboard(this);return false;
}" href="http://blog.csdn.net/zeng622peng/archive/2010/12/25/6097346.aspx#">
- new AlertDialog.Builder(MyActivity.this)
- .setIcon(android.R.drawable.ic_dialog_alert)
- .setTitle("Warnning")
- .setMessage(
- "You forget to write the message. Do you want to fill out it ??")
- .setPositiveButton("Yes", positiveListener).setNegativeButton(
- "No", negativeListener).create().show();
問題得以解決!