【问题标题】:How can I use a method inside an activity from external class in javajava - 如何在java中的外部类的活动中使用方法
【发布时间】:2012-05-21 15:42:22
【问题描述】:

是否可以从常规 Java 类中调用 Activity 的方法?

这是activity中的方法:

public void showMessage(String mensaje) {
   AlertDialog.Builder builder = new AlertDialog.Builder(this);
   builder.setMessage("Are you sure you want to exit?")
          .setCancelable(false)
          .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int id) {
                   dialog.cancel();
              }
          })
          .setNegativeButton("No", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int id) {
                   dialog.cancel();
              }
          });
   AlertDialog alert = builder.create();
   alert.show();

这是外部(简单java)类中代码的方法:

public void write(String importanChange) {
    Context context = //Here I dont know what to do here (It's my question,
                  //but I tried other aproaches but all failed                     
    ((MyActivity)mContext).showMessage(message);

原因是我有一个框架可以检测模拟的变化(Android 应用程序),然后这个模拟会通知这个框架的变化,并决定这个变化是否重要。

因此,如果更改很重要,则框架必须在活动中执行 showMessage 方法。

【问题讨论】:

    标签: java android android-activity android-context


    【解决方案1】:

    由于AlertDialog 不需要Context,您可以选择创建一个实用方法:

    public static void showMessage(String message) {
        ...
    }
    
    public void write(String importanChange) {
        MyActivity.showMessage(msg);
        ...
    }
    

    【讨论】:

      【解决方案2】:

      您应该将您的方法定义为public static void write(String importantChange) 而不是public void write(String importantChange)

      将您的方法设置为“静态”意味着您不必实例化包含您的方法的类来使用此方法。

      编辑:我刚刚看到您的“编辑”。因此,要在您的方法中使用上下文,我建议您将上下文作为方法的参数传递,如下所示:public void write(String importantChange, Context context)

      【讨论】:

      • 05-21 15:19:21.917: E/AndroidRuntime(406): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
      • 是否必须将 -Context- 传递给许多 .class 或 .java 以导致最后一个方法调用执行 write 方法的相应类?
      • 此异常通常与您尝试在非 UI 线程上对 UI 元素执行操作的问题相关联。
      • 是的,你的权利..五天后我找到了解决方案......感谢大家的回答(帮助我找到这个)......现在相应的解决方案是(显示我的最后一篇文章)
      【解决方案3】:

      试试这个,

      例如:

      Activity Class:
      
      
            public class MyActivity extends Activity {
      
                          onCreate(.....){
      
                     }
      
               public void go(){
      
               //Some code
      
           }
      
      
      
      Java External Class in the same package:
      
      
      public class MyDemo {
      
           MyActivity ac;
                         public MyDemo(MyActivity ac) {
      
      
                                 this.ac = ac;
                           }
      
                       public void foo() {
      
                                ac.go();    // Access the activity's method
                         }
      
           }
      

      【讨论】:

        【解决方案4】:
        public static void showMessage(String importantMensaje){         
                final String mensajeAMostrar = importantMessage;
                Runnable runnable = new Runnable() {
                    public void run() {
                        handler.post(new Runnable() { // This thread runs in the UI
                            public void run() {
                                MyActivity.builder.setMessage(mensajeAMostrar)
                                .setCancelable(false)
                                .setPositiveButton("Of Course", new DialogInterface.OnClickListener() {
                                 public void onClick(DialogInterface dialog, int id) {
                                      dialog.cancel();
                                 }
                                });
                                AlertDialog alert = MyActivity.builder.create();
                                alert.show();
                            }
                        });
                    }
                };
                new Thread(runnable).start(); 
         }
        

        【讨论】:

          猜你喜欢
          • 2013-05-07
          • 2012-02-21
          • 1970-01-01
          • 1970-01-01
          • 2011-10-12
          • 2012-05-16
          • 1970-01-01
          • 2021-02-22
          • 2012-07-02
          相关资源
          最近更新 更多