Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Threads and Services Threads in Android Upgrading Our Thread

Chris Reeder
Chris Reeder
3,205 Points

non-static method cannot be referenced from a static context

I implemented a thread which does some work and then wants to call a method from the main activity using runonuithread. The main activity thread should make a toast. but I get this error and I'm not sure how to fix it.

Error Message: non static method cannot be referenced from a static context


public class AppLoading extends AppCompatActivity {
    private static final String TAG = AppLoading.class.getSimpleName();
    private EventObject eventObject;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_app_loading);

        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        eventObject = new EventObject();
        Handler handler = new Handler();

        checkExistingActivity(handler);
    }

    private void checkExistingActivity(Handler handler){
        checkExistEventThread thread = new checkExistEventThread(handler);
        thread.setName("checkExistingActivityThread");
        thread.start();
    }

    public void startEventCriteria(){
        Toast.makeText(this, "Leggo", Toast.LENGTH_SHORT).show();
    }
}

public class checkExistEventThread extends Thread{
    private static final String TAG = checkExistEventThread.class.getSimpleName();
    Handler handler;

    public checkExistEventThread(Handler handler){
        this.handler = handler;
    }
    @Override
    public void run() {
        startCheck();
    }

    private void newEventObject(){
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                Log.e(TAG,"It worked");
                AppLoading.startEventCriteria();  <<<<<<<<<<<<<<<<<<<<<ERROR HERE>>>>>>>>>>>>>>>>>>>>>>>>>
            }
        };
        handler.post(runnable);
    }
}
Ben Deitch
Ben Deitch
Treehouse Teacher

Hey Chris! Can you share your project or post your code?

2 Answers

Ben Deitch
STAFF
Ben Deitch
Treehouse Teacher

Ah, I see. 'startEventCriteria' is not a static method inside AppLoading. In order to access the method you'll first need an instance of the AppLoading class which you should be able to pass into your CheckExistingThread class. Here's what worked for me:


public class AppLoading extends AppCompatActivity {
    private static final String TAG = AppLoading.class.getSimpleName();
    private EventObject eventObject;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_app_loading);

        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        eventObject = new EventObject();
        Handler handler = new Handler();

        checkExistingActivity(handler);
    }

    private void checkExistingActivity(Handler handler){
        checkExistEventThread thread = new checkExistEventThread(handler, this);
        thread.setName("checkExistingActivityThread");
        thread.start();
    }

    public void startEventCriteria(){
        Toast.makeText(this, "Leggo", Toast.LENGTH_SHORT).show();
    }
}

public class checkExistEventThread extends Thread{
    private static final String TAG = checkExistEventThread.class.getSimpleName();
    Handler handler;
    AppLoading appLoading;

    public checkExistEventThread(Handler handler, AppLoading appLoading){
        this.appLoading = appLoading;
        this.handler = handler;
    }

    @Override
    public void run() {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                Log.e(TAG,"It worked");
                appLoading.startEventCriteria();
            }
        };
        handler.post(runnable);
    }
}
Chris Reeder
Chris Reeder
3,205 Points

PLOTTWIST

I never saw that coming. Brilliant...