0

我有主要活动和一个简单的类。主要活动有对话框方法,我从另一个类调用此方法

主要活动-

public class WishareActivity extends Activity

 {

   public void onCreate(Bundle savedInstanceState)
  {
      super.onCreate(savedInstanceState);

      setContentView(R.layout.main);

   activity=this;

  //handler to call alertmsg()method

    alerthandler= new Handler() {
        public  void handleMessage(Message msg){

            alertmsg();
        } 
    }; 

      layouthandler= new Handler() {
        public  void handleMessage(Message msg){

            setContentView(R.layout .main);
        } 
    }; 


}


    //method to show the dialog box

   public static  void alertmsg()
{
     AlertDialog.Builder alert = new AlertDialog.Builder(WishareActivity.activity);

    alert.setTitle("Confirm");
    alert.setMessage("abc");

    alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which)
        {

            dialog.dismiss();
        }
    });

    alert.setNegativeButton("Deny", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {




            dialog.dismiss();
        }
    });

    Log.d("abc","alert alert");
    AlertDialog s=alert.create();
    s.setOnDismissListener(new DialogInterface.OnDismissListener() { 
        public void onDismiss( DialogInterface dialog) { 

        }
    });

     Log.d("abc1","alert alert");

  alert.show();
 Log.d("abc1","alert alert alert");
    return;

}

连接类

public class connect implements Runnable
{

 public void run()
    {

         WishareActivity.layouthandler.sendEmptyMessage(0);
         WishareActivity.alerthandler.sendEmptyMessage(0);
    }
}

在 Logcat 中它的显示

  abc : alert alert
  abc1: alert alert
  abc1 :alert alert alert

根据日志,该方法被正确调用,但没有显示对话框。

任何帮助将不胜感激

4

2 回答 2

0

尝试这个:

你像这样创建没有静态的方法

public void alertmsg();

因为类是不支持静态的。它在下面显示此错误

   Cannot use this in a static context

我相信这会奏效

于 2013-03-01T12:06:23.080 回答
0

更改您的警报对话框方法

public static  void alertmsg()

对此

public static  void alertmsg(Context context)

像这样使用这个上下文变量

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

并且在使用这种方法时这样做

WishareActivity.alertmsg(someactivity.this); // here someactivity will be the activity name where you are calling alert method

您的对话框将工作得很好。

于 2013-03-01T11:57:32.937 回答