我一直在阅读Observer模式以保持我的UI最新,但我仍然无法让它工作,据我所知,Observer是我的UI,它监视我的Pet类的任何变量更改是否有任何运行update();它什么都不做,甚至没有Log.d。
观测/ MainActivity
package com.grim.droidchi;
import java.util.Observable;
import java.util.Observer;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements Observer, OnClickListener {
private static final String TAG = "VPET";
private static final String APP_PREFS = "VPET";
private static final int REQUEST_CODE = 1;
TextView happiness_display, health_display, hunger_display, level_display;
Button PunchPet, UpdateHunger;
public static Pet pet = new Renamon();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.pet_display);
myWebView.loadUrl("file:///android_asset/renamon.gif");
myWebView.setInitialScale(10000);
myWebView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
PunchPet = (Button) findViewById(R.id.PunchPet);
UpdateHunger = (Button) findViewById(R.id.UpdateHunger);
final TextView hunger_display = (TextView) findViewById(R.id.hunger_display);
TextView happiness_display = (TextView) findViewById(R.id.happiness_display);
TextView level_display = (TextView) findViewById(R.id.level_display);
TextView health_display = (TextView) findViewById(R.id.health_display);
hunger_display.setText(Integer.toString(pet.getHunger()));
health_display.setText(Integer.toString(pet.getHP()));
level_display.setText(Integer.toString(pet.getLVL()));
happiness_display.setText(Integer.toString(pet.getHappy()));
Intent intent = new Intent(this, Gameloop.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getBaseContext(), REQUEST_CODE, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + (5 * 1000), 1800000, pendingIntent);
// 1800000 ms = 30 mins
pet.feed();
pet.addObserver(this);
pet.notifyObservers(pet);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
protected void onPause() {
super.onPause();
}
@Override
public void update(Observable o, Object data) {
Toast.makeText(getApplicationContext(), "soemthing has changed", Toast.LENGTH_LONG).show();
}
@Override
public void onClick(View v) {
if (v == PunchPet) {
pet.setLVL(50);
pet.notifyObservers(pet);
Log.d(TAG, "PUNCHPET" + pet.getHP());
}else {
}
}
}
可观测/宠物
package com.grim.droidchi;
import java.util.Observable;
import android.util.Log;
public class Pet extends Observable implements PetInterface {
private static final String TAG = "VPET";
private int Health = 100;
private int Happiness = 10;
private int Level = 1;
private int Hunger = 0;
private int Exp = 0;
private String Name;
private Boolean isAlive = true;
private Boolean isSick = false;
public void setisAlive(boolean answer) {
this.isAlive = answer;
}
public void setisSick(boolean answer) {
this.isAlive = answer;
}
public void setHP(int hp) {
this.Health = hp;
notifyObservers(hp);
}
public void setLVL(int lvl) {
this.Level = lvl;
notifyObservers(lvl);
}
public void setXP(int xp) {
this.Exp = xp;
notifyObservers(xp);
}
public void setHunger(int hunger) {
this.Hunger = hunger;
notifyObservers(hunger);
}
public void setHappy(int happy) {
this.Happiness = happy;
notifyObservers(happy);
}
public int getHP() {
return Health;
}
public int getLVL() {
return Level;
}
public int getXP() {
return Exp;
}
public int getHunger() {
return Hunger;
}
public int getHappy() {
return Happiness;
}
public boolean isAlive() {
return isAlive;
}
public boolean isSick() {
return isSick;
}
@Override
public void sleep() {
// TODO Auto-generated method stub
}
@Override
public void clean() {
// TODO Auto-generated method stub
}
@Override
public void feed() {
Log.d(TAG, "FEEDING FROM INTERFACE THING");
}
@Override
public void passtime() {
}
}
是的我也问过这个,但是你知道,它有两个答案(虽然它们可能是对的但是没有帮助我)并且现在不会再收到它了,它会在列表中掉得太远,如果我可以删除它,我会
logcat的
02-25 22:51:50.381: E/AndroidRuntime(12026): FATAL EXCEPTION: main
02-25 22:51:50.381: E/AndroidRuntime(12026): java.lang.NullPointerException
02-25 22:51:50.381: E/AndroidRuntime(12026): at com.grim.droidchi.MainActivity.update(MainActivity.java:93)
02-25 22:51:50.381: E/AndroidRuntime(12026): at java.util.Observable.notifyObservers(Observable.java:138)
02-25 22:51:50.381: E/AndroidRuntime(12026): at com.grim.droidchi.Pet.setHP(Pet.java:33)
02-25 22:51:50.381: E/AndroidRuntime(12026): at com.grim.droidchi.MainActivity$1.onClick(MainActivity.java:47)
02-25 22:51:50.381: E/AndroidRuntime(12026): at android.view.View.performClick(View.java:4202)
02-25 22:51:50.381: E/AndroidRuntime(12026): at android.view.View$PerformClick.run(View.java:17340)
02-25 22:51:50.381: E/AndroidRuntime(12026): at android.os.Handler.handleCallback(Handler.java:725)
02-25 22:51:50.381: E/AndroidRuntime(12026): at android.os.Handler.dispatchMessage(Handler.java:92)
02-25 22:51:50.381: E/AndroidRuntime(12026): at android.os.Looper.loop(Looper.java:137)
02-25 22:51:50.381: E/AndroidRuntime(12026): at android.app.ActivityThread.main(ActivityThread.java:5191)
02-25 22:51:50.381: E/AndroidRuntime(12026): at java.lang.reflect.Method.invokeNative(Native Method)
02-25 22:51:50.381: E/AndroidRuntime(12026): at java.lang.reflect.Method.invoke(Method.java:511)
02-25 22:51:50.381: E/AndroidRuntime(12026): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
02-25 22:51:50.381: E/AndroidRuntime(12026): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
02-25 22:51:50.381: E/AndroidRuntime(12026): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:3)
问题是您已覆盖notifyObservers()
,但未覆盖notifyObservers(java.lang.Object)
。你正在调用后者,但你没有定义它。
@Override
public void notifyObservers(Object o) {
observers.notify(o);
super.notifyObservers(o);
}
此外,您在内部可能会更改值,但观察者不知道。在你的“宠物”(Observable)上调用setChanged()
来解决这个问题。
答案 1 :(得分:3)
当您从Observable
继承时,您的类会继承其所有功能。除非您添加自己的功能,否则无需覆盖其方法或保留自己的Observers
集。正如Bailey S
所述,在您的setter方法中,在致电notifyOservers()
之前,请致电setChanged()
。
尝试从Observable
类中删除您已覆盖的所有方法。此外,每次拨打notifyObservers(object)
时,请稍后致电clearChanged()
。
答案 2 :(得分:1)
问题是除非之前调用notifyObservers()
,否则setChanged()
方法不会执行任何操作。这不直观,因为它似乎总是应该通知观察者,但事实并非如此。不需要使用clearChagned()
方法,因为notifyObervers()
也会清除更改的标志。在getter和setter中使用这样的代码:
public void setHP(int hp) {
this.Health = hp;
setChanged();
notifyObservers(hp);
}
另外,不要覆盖Observable
中的方法,只需要实现接口`Observer中的方法。
您的update()
方法需要做一些事情。这是一些非常简单的示例代码。在其中,Observable
对象和数据被忽略,因为您已经知道它是什么对象,我们只是更新所有字段。
@Override
public void update(Observable o, Object data) {
Toast.makeText(getApplicationContext(), "soemthing has changed",Toast.LENGTH_LONG).show();
//Update the GUI fields with values from the local pet object.
hunger_display.setText(Integer.toString(pet.getHunger()));
health_display.setText(Integer.toString(pet.getHP()));
level_display.setText(Integer.toString(pet.getLVL()));
happiness_display.setText(Integer.toString(pet.getHappy()));
}
在onCreate()
中,您需要正确初始化实例变量。
onCreate() {
...
hunger_display = (TextView) findViewById(R.id.hunger_display);
happiness_display = (TextView) findViewById(R.id.happiness_display);
level_display = (TextView) findViewById(R.id.level_display);
health_display = (TextView) findViewById(R.id.health_display);
...
}
换句话说,您需要删除TextView
声明,因为它们正在创建新变量,而不是设置您实际想要设置的变量。