Android - 从主要活动的另一个类制作Toast

时间:2013-01-05 19:33:51

标签: android timer toast

我已经整天搜索了一个解决方案。不幸的是,没有找到任何可以帮助我的东西。

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

可能存在一般性问题

继承我的守则:

主要活动:

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

这是我的Timer类(未完成)

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类未链接到布局。我不知道是否会引起问题?我只需要一些活动方法。

1 个答案:

答案 0 :(得分:2)

您永远不会为toast字段指定值。当您在toast.setGravity(Gravity.CENTER, 0, 0);中访问它时,会出现空指针异常。