Use of External class with error (non-static method)

Asked

Viewed 150 times

0

I created a Class external call Useful to leave some methods that I always use, but is giving error when using them, follows code from Useful:

package com.hs.gui.testelayout.util;

import android.support.v7.app.AppCompatActivity;
/**
 * Created by Gui_j on 25/04/2016.
 */
public class Util extends AppCompatActivity {

    public void backAppBar(String telaTitulo){
     //getSupportActionBar() são métodos nativos do _Android_ que chamam um botão na tela
     getSupportActionBar().setDisplayHomeAsUpEnabled(true);
     getSupportActionBar().setHomeButtonEnabled(true);
     getSupportActionBar().setTitle(telaTitulo);
    }
}

if I keep it the way it is there, I have an error of return: erros

To make the mistake I should make

public void backAppBar(String telaTitulo){...}

in

public static void backAppBar(String telaTitulo){...}

But by doing so I earning another mistake: erros2

That to correct I must remove the static added, but with that we return to the initial problem. About the getSupportActionBar()

  • Where is this method "getSupportActionBar()"? if it is from the Util class, it should be static as well.

  • It would not be better to pass the context as an argument to retrieve the actionbar from it instead of creating a class that extends actionbar?

  • @user5978 the method is in the library, I import it here import android.support.v7.app.AppCompatActivity;

  • @Diegof as so?

  • The method is not static. You cannot access a non-static method within a static method. Unless you have an instance of the object in question. You can instantiate the Appcom object..., or take it as a parameter, instead of extending it.

  • Instanciar Util, I believe it is the worst solution among the already suggested here.

Show 1 more comment

1 answer

3


This is because the method getSupportActionBar is not static, so it will not have access in a static method.

There are two ways to resolve this situation:

Abstract class

This class will have all common methods the other screens.

Example:

public abstract class ActivityModel extends AppCompatActivity{
    public void backAppBar(String telaTitulo){
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setTitle(telaTitulo);
    }

}

On your screens, instead of extending AppCompatActivity, should extend the ActivityModel:

public class MainActivity extends ActivityModel {
…
}

Utility Class

Another way would be to create a utility class with common methods. The difference is that instead of inheriting, you should pass via parameters the information of Activity (as sitado in the above comments by Diegof) :

public class Utils  {
    public static  void backAppBar(String telaTitulo, AppCompatActivity activity){
        activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        activity.getSupportActionBar().setHomeButtonEnabled(true);
        activity.getSupportActionBar().setTitle(telaTitulo);
    }

}

  • +1 because the second suggestion was that I gave in the comments :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.