0

我已经整天寻找解决方案。不幸的是,还没有找到任何可以帮助我的东西。

我必须每 15 分钟更改一次值​​​。我不会在我的主要活动中做,但现在是不同的班级。我认为这样的事情不会有问题,但我被困住了。我想打印一条测试消息,但给定的上下文有问题。可能我的“计时器”有一个普遍的问题。

这是我的代码:

主要活动:

tm = new TimerLoerg(this.getApplicationContext());
tm.startTimer();    

这里是我的计时器课(未完成)

package at.android.dertestloerk;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Handler;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.Toast;

    public class TimerLoerg extends Activity{

    private Handler handler = new Handler();
    int ms;
    Toast toast;

    private ImageView imageViewLoerg;
    private AnimationDrawable animatedLoerg;

    private Context context;
    Activity activity;

    public TimerLoerg(Context context, Activity activity){
        this.context = context;
        this.activity = activity;
    }

    public void startTimer() {
        ms = 5000;
        handler.postDelayed(timedTask, ms);

        activity.runOnUiThread(new Runnable() {
            public void run() {
                toast = Toast.makeText(context, "Timer gestartet", Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
            }
        });
    }

    public void changeLoerg() {
        int stufe;
        // layout = (RelativeLayout)findViewById(R.id.LayoutMain);
        imageViewLoerg = (ImageView)activity.findViewById(R.id.imageViewLoerg);

        stufe = SettingsLoerg.getLevel(context);

        Object tag = imageViewLoerg.getTag();

        if (stufe == 1) {
            int loergId = R.drawable.animatedegg;

            if( tag != null && ((Integer)tag).intValue() == loergId) {
                loergId = R.drawable.animatedloerg;
                animatedLoerg.stop();
                //playAnimationNodelay();
                imageViewLoerg.setTag(loergId);
                imageViewLoerg.setBackgroundResource(loergId);
                playAnimationNodelay();
            }
        }
    }

    public void changeStats() {
        int hunger;

        hunger = SettingsLoerg.getHunger(context);
        if (hunger < 15) {
            toast = Toast.makeText(context, "Loerg is very hungry!", Toast.LENGTH_SHORT);
            toast.show();
        }
        else {
            hunger = hunger - 15;
            SettingsLoerg.setHunger(hunger, context);
        }
    }

    private void playAnimation() {
        if (this.imageViewLoerg.getDrawable() instanceof AnimationDrawable) {
            this.animatedLoerg = (AnimationDrawable)this.imageViewLoerg.getDrawable();
            Handler handler = new Handler(getMainLooper());
            handler.postDelayed(new Runnable() {

                @Override
                public void run() {
                    animatedLoerg.start();              
                }
            }, 1500);
        }
    }

    private void playAnimationNodelay() {
        if (this.imageViewLoerg.getDrawable() instanceof AnimationDrawable) {
            this.animatedLoerg = (AnimationDrawable)this.imageViewLoerg.getDrawable();
            animatedLoerg.start();
        }
    }

    private Runnable timedTask = new Runnable(){
          @Override
          public void run() {
           changeLoerg();
           handler.postDelayed(timedTask, 5000);
          }};
}

日志:

01-05 20:33:02.521: E/AndroidRuntime(7199): FATAL EXCEPTION: main
01-05 20:33:02.521: E/AndroidRuntime(7199): java.lang.NullPointerException
01-05 20:33:02.521: E/AndroidRuntime(7199):     at at.android.dertestloerk.TimerLoerg$2.run(TimerLoerg.java:29)
01-05 20:33:02.521: E/AndroidRuntime(7199):     at android.os.Handler.handleCallback(Handler.java:615)
01-05 20:33:02.521: E/AndroidRuntime(7199):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-05 20:33:02.521: E/AndroidRuntime(7199):     at android.os.Looper.loop(Looper.java:137)
01-05 20:33:02.521: E/AndroidRuntime(7199):     at android.app.ActivityThread.main(ActivityThread.java:4745)
01-05 20:33:02.521: E/AndroidRuntime(7199):     at java.lang.reflect.Method.invokeNative(Native Method)
01-05 20:33:02.521: E/AndroidRuntime(7199):     at java.lang.reflect.Method.invoke(Method.java:511)
01-05 20:33:02.521: E/AndroidRuntime(7199):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-05 20:33:02.521: E/AndroidRuntime(7199):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-05 20:33:02.521: E/AndroidRuntime(7199):     at dalvik.system.NativeStart.main(Native Method)

如您所见, Timer 类未链接到布局。不知道会不会出问题?我只需要一些活动方法。

4

1 回答 1

2

您永远不会为该toast字段分配值。当您访问它时,toast.setGravity(Gravity.CENTER, 0, 0);您会得到一个空指针异常。

于 2013-01-05T19:39:50.993 回答