Android - Make Toast from another Class for Main A

2019-09-20 13:01发布

I have already searched all day for a solution. Unfortunately, have not found anything that can help me.

I have to change every 15 minutes ago values​​. I would not do in my main activity, but now a different class. I thought something like that would be no problem, but I am stuck. I wanted to print a test message, but there's something wrong with the given context. Possibly I have a general problem with my "timer".

Heres my Code:

Main Activity:

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

And here my Timer Class (not finished)

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);
          }};
}

Log:

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)

The Timer class is not linked to a layout as you can see. I do not know if could cause problems? I just needed some methods of activity.

1条回答
可以哭但决不认输i
2楼-- · 2019-09-20 13:06

You never assign a value to the toast field. When you access it in toast.setGravity(Gravity.CENTER, 0, 0); you get a null pointer exception.

查看更多
登录 后发表回答