简体   繁体   中英

Android Toast throwing error

My main activity "My_Gallery" is calling a function from another class "DataCall". In DataCall I am trying to Toast a variable to make sure I have the correct info in there.

Toast.makeText(DataCall.this, url_param_string, Toast.LENGTH_SHORT).show();

But I am getting the following error under DataCall.this

Cannot use this in a static context

Not sure what I am missing. Thanks in advance for any feedback.

Also I am new to Eclipse and I see in debug mode there is a window for variables but it is empty.

That's exactly the error. You can't call this on a static context, like a static function.

If you need a context for Toast. Try the application context: Context.getApplicationContext()

Dentro del metodo agrega un parametro adicional que indique el contexto: ejemplo:

public static void sendMessage(String message, Context contexto) 
{
    if (mRfcommClient.getState() != Bluetooth_lib.STATE_CONNECTED) 
    {   
      Toast.makeText(contexto, "No conectado", Toast.LENGTH_SHORT).show(); 
    } 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM