Using setContentView in static methods

Asked

Viewed 135 times

1

I am creating an application using Bluetooth, where the method that receives the messages is static.

Depending on the message the device receives, I’d like the app to do something other than simply show the message in a textView (also static) on the screen, such as calling another screen (setContentView), create a Intent any or present a AlertDialog.Builder.

The problem is when a method static tries to use these resources. I tried through this method static create a class object ActivityMain to use any other method that performed this process, however, errors also occur.

What to do?

  • 1

    It seems to me that its architecture contains serious structural problems. View static is a bad sign. Manually create objects of type Activity (instead of ordering from the system through the startActivity) is a lousy signal. I think we will need more details about your current architecture to better understand what to suggest.

  • 1

    Good! I will try to present the problem in a simpler way. We have the main class:

  • We have a STATIC method being started public class Mainactivity extends Actionbaractivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // I’ll call a method Static start(); }}

  • private Static void inicio() { // None of the attempts compile... setContentView(R.layout.activity_main); Alertdialog.Builder Builder = new Alertdialog.Builder(this); Builder.setTitle("title"); Builder.setPositiveButton("OK",null); &#xTo; Intent Intent = new Intent(Mainactivity.this, otherActivity.class); // An option would be... // Instantiate an object in that class to use a NONSTATIC Mainactivity mainActivity = new Mainactivity(); mainActivity.call; }

  • private void nameOutraTela(){ // Here all above attempts compile... however also NAÕ WORKS // The App hangs when running }

No answers

Browser other questions tagged

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