是否可以從常規Java類調用Activity
的方法?如何在java中使用外部類的活動內的方法
這是活性的方法:
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方法。
我做到了,但編譯器給了我下一個錯誤: – user1408219
05-21 15:19:21.917:E/AndroidRuntime(406):java.lang.RuntimeException:無法在未調用Looper.prepare的線程中創建處理程序() – user1408219
如果必須將-Context-傳遞給許多.class或.java,導致調用執行寫入方法的相應類的最後一個方法,請不要緊? – user1408219