如何从(非活动)Java类调用Activity类中定义的方法

时间:2014-05-14 00:08:19

标签: java android multithreading

在我的DataTrak活动中,我定义了以下方法:

    public void updateTotal(IAmount totalAmount, int transactionType) {
        switch (transactionType) {
            case AccountTotals.VALUE_CANCELS:
                txtView_cancels_value.setText("" +     (Long.parseLong(txtView_cancels_value.getText().toString()) + totalAmount.getValue()));          
                break;
            case AccountTotals.VALUE_PAYS:
                txtView_pays_value.setText("" + (Long.parseLong(txtView_pays_value.getText().toString()) + totalAmount.getValue()));            
                break;
            case AccountTotals.VALUE_SALES:
                txtView_sales_value.setText("" + (Long.parseLong(txtView_sales_value.getText().toString()) + totalAmount.getValue()));
                break;
            default:
                break;
    }
    btn_total.setBackgroundResource(R.drawable.button_green );

}

该方法计算并更新一些TextView并更改按钮的颜色。然后我需要从Java抽象类中调用此方法。方法调用出现在非UI线程上运行的方法中。以下是我如何称呼该方法:

            new Thread()
        {
            public void run()
            {
                new DataTrak().runOnUiThread(new Runnable()
                {
                    public void run()
                    {           
                        new DataTrak().updateTotal(totalAmount,transactionType);
                    }
                });
            }
        }.start();

问题是我遇到了运行时异常。这是LogCat输出:

05-13 16:52:13.325: E/AndroidRuntime(22101): FATAL EXCEPTION: Thread-1577
05-13 16:52:13.325: E/AndroidRuntime(22101): Process: com.ilts.lct, PID: 22101
05-13 16:52:13.325: E/AndroidRuntime(22101): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
05-13 16:52:13.325: E/AndroidRuntime(22101):    at android.os.Handler.<init>    (Handler.java:200)
05-13 16:52:13.325: E/AndroidRuntime(22101):    at android.os.Handler.<init>(Handler.java:114)
05-13 16:52:13.325: E/AndroidRuntime(22101):    at android.app.Activity.<init>(Activity.java:786)
05-13 16:52:13.325: E/AndroidRuntime(22101):    at com.ilts.lct.ap.mainframe.DataTrak.<init>(DataTrak.java:37)
05-13 16:52:13.325: E/AndroidRuntime(22101):    at com.ilts.lct.ap.customerfunctions.CustomerTransaction$2.run(CustomerTransaction.java:736)

实际上,最初我只使用了方法调用的行,但是我得到了相同的运行时异常。我搜索了&#34;无法在没有调用Looper.prepare()&#34;的线程中创建处理程序。我在这个问题上发现了几个帖子。其中一个让我把方法调用放在一个新的Thread中,如上所示。但我仍然得到相同的运行时异常。我应该改变什么?你能帮我理解实际问题是什么以及如何修复它吗?提前谢谢。

在我阅读@Metero的答案之后,这里是Java抽象类中的代码:

public abstract class CustomerTransaction extends Transaction {
...................................................

    public interface CallBack {
    public void updateTotal(IAmount a,int n);
}

private static CallBack callback;

public static void registerCallback(CallBack callback1){
   callback = callback1;
}

/**
 * Method to update the transaction state
 *
 * @param  state  The new transaction state
 **/
public void setState(final int state) {
    this.state = state;
    /*
     * Update the status
     */
    if (state == TRANSACTION_COMPLETE) {
                    new Thread()
        {
            public void run() {
                Looper.prepare();
                new DataTrak().runOnUiThread(new Runnable()
                {
                    public void run() {
                        Log.i("HERE", "HERE");
                        Looper.prepare(); 
                        callback.updateTotal(totalAmount,transactionType);
                        Looper.loop();
                    }
                });
            }
        }.start();

   }
   }
....................
}

4 个答案:

答案 0 :(得分:1)

你永远不应该自己实例化一个Activity,如果这样做,它将不是一个正常的活动,它只是一个有问题的计划Java对象。

所以你可以解决问题的方法是使用Observer pattern你可以用'callback'方法定义一个接口,你让Activity实现它并让它订阅''''通知。所以基本上,当这个更新线程运行时,你将通过订阅的监听器列表运行并调度该调用,它就像一个普通的方法调用。

请记住:'订阅'和'取消订阅'尊重活动生命周期......赞成onCreate()并取消订阅onDestroy()。

的活动:

public class YourActivity extends Activity implements ControlListener {

public void onCreate(...) {
 ....
 control.registerListener(this);

 control.performOperation();

}

public void onDestroy(...) {
 ....
 control.unregisterListener(this);
}

public void updateTotal(String newValue) {
  runOnUiThread(new Runnable() {
   public void run() {
     textView.setText(newValue);
   } 
  });

   }
  }

控制类:

   public class Control {

      private Set<ControlListener> listeners = new HashSet<ControlListener>();

     public synchronized void registerListener(ControlListener listener) {
       listeners.add(listener);
     }

    public synchronized void unRegisterListener(ControlListener listener) {
       listeners.remove(listener);
    }

    public synchronized void notifyListeners(String newValue) {
      for(ControlListener listener : listeners) {
        listener.updateTotal(newValue);
      }
    }

     public void performOperation() {

      new Thread() {
        public void run() { 
          String newValue= service.performBackgroundOperationToGetNewValue();

         notifyListeners(newValue);
        }
      }.start();
     }
   }

控制侦听器:

 public interface ControlListener {
    public void updateTotal(String newValue);
  }

或者,您可以使用非常HANDY库在项目中应用Observer模式,它是 Otto http://square.github.io/otto/使用Otto,您不需要拥有注册表/ unregister / notifylisteners方法在你的控件中,它会自动放在其他地方。

答案 1 :(得分:1)

这个问题的本质表明MVC违规。它不应该在Activity中调用模型,而应该调用Activity在模型上注册的一些回调方法。该回调应该在UI线程中排队。

答案 2 :(得分:0)

这不是最好的选择。

new Thread()
    {
        public void run()
        {
            new DataTrak().runOnUiThread(new Runnable()
            {
                public void run()
                {
                    Looper.prepare();//This is not the best choice.
                    new DataTrak().updateTotal(totalAmount,transactionType);
                }
            });
        }
    }.start();

我建议使用AsyncTask,就像这样。

class SampleTask extends AsyncTask<Boolean, Boolean, Boolean> {

    private int totalAmount;
    private yourActivity activity;

    //passing parameters
    public void SampleTask(yourActivity activity){
        this.activity = activity;
    }

    @Override
    protected Boolean doInBackground(Boolean... params) {
        while (totalAmount < 10){
            totalAmount++;
        }
    }
    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
// this is not the best choice
// because you are creating two instances.
        new DataTrak().updateTotal(totalAmount,transactionType);

        //If you pass a parameter, this for me is the best option.

        activity.updateTotal(totalAmount,transactionType);
    }
}

答案 3 :(得分:0)

我同意@Alécio,请使用回调来执行此操作。在非活动类中添加回调接口:

   class yourclass{
        public interface callBack{
               public void updateTotal(...);
         }

        private callBack callback;

        public void registerCallback(callBack callback){
              this.callback = callback;
        }

        //somewhere call the updateTotal(...)
        callback.updateTotal(...);
  }

在活动

  //the activity implement the callback and then register it, and call the callback when neccesary

    class yourActivity extends Activity implement callBack{

        @Override
        onCreate(...){
        ...

        yourclass.registerCallback(this);

        }

        @Override
        public void updateTotal(...){
         .......
        }

    }

多类通信的示例代码。   

public class Controller {
    callBack callBack;

    public void registerCallBack(callBack back){
        this.callBack = back;
    }

    public void show(){
          callBack.update(1, "my name");
    }

    public interface callBack{
        public void update(int type, String message);
    }

    public callBack getCallBack(){
        return callBack;
    }

}

public class AnotherClass {
    Controller controller = new Controller();

    public void registerCallBack(callBack back){
        controller.registerCallBack(back);
    }

    public void show(){
        controller.getCallBack().update(1, "show me!");
    }

}


public class MyActivity extends Activity implements callBack {
    AnotherClass myclass = new AnotherClass();

    @Override
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        Log.d("TAG", "This is message!"); 
        setContentView(R.layout.main);
        myclass.registerCallBack(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        myclass.show();
    }

    @Override
    public void update(int type, String message) {
        ((TextView) findViewById(R.id.display)).setText(message);
    }
}