-1

So am trying to use runOnUiThread() to update my LogUI (found in my apps MainActivity) which is a TextView. The issue is am trying to update the TextView using runOnUiThread() from another class by getting the strings to be made on the View. Here is details of my code to elaborate my issue:

private LoggingClass getLogs;

getLogs.AddtoLogUI(String.format("Established on port: %d", obj));

Then the LoggingClass code:

public class LoggingClass {

private MainActivity updateUI;
private String stringValue;

public void AddtoLogUI(final String format) {
    this.stringValue = format;
    updateUI.runOnUiThread(new Runnable(){

        @Override
        public void run() {
            MainActivity.log_this(stringValue);
        }

    });
}
}

The method MainActivity.log_this() code is like this:

public static void log_this(final String msg){

    if(editable.toString().split("\n").length >=50) {
        editable.delete(0, editable.toString().indexOf("\n"));
    }

    Runnable runnable = new Runnable(){
        @Override
        public void run() {
            editable.append(msg);
            editable.append("\n");
        }
    };

    LogView.post(runnable);
}

PS: LogView is a TextView.

The NullpointerException is thrown when am trying to get the Strings using the getLogs.AddtoLogUI() method. Any suggestions?

Additional Info as regards the object:

`Object obj[] = new Object[1];
    obj[0] = Integer.valueOf(Port);`
CodeZero
  • 179
  • 2
  • 15
  • You can't not create Activity object. Put more source code. –  Dec 23 '14 at 06:45
  • What you want to achieve by doing this? i think we should need to restructure your logic to achieve desired results – ρяσѕρєя K Dec 23 '14 at 06:47
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) –  Dec 23 '14 at 06:49
  • Please post the exception stack trace or some more code. – HarshalK Dec 23 '14 at 07:05
  • Have added more codes. What am trying to achieve is getting specific strings to the TextView in my apps MainActivity. This requires me calling a global method to get this Stings from different classes. – CodeZero Dec 23 '14 at 07:18

2 Answers2

1

You declare private LoggingClass getLogs; in your Activity getLogs=new LoggingClass (MainActivity.this) in your onCreate

then u can use getLogs.AddtoLogUI(String.format("Established on port: %d", obj));

createLoggingClass constructor

public class LoggingClass {

private MainActivity updateUI;
private String stringValue;


public LoggingClass (MainActivity updateUI){
this.updateUI=updateUI;
}


public void AddtoLogUI(final String format) {
    this.stringValue = format;
    updateUI.runOnUiThread(new Runnable(){

        @Override
        public void run() {
            MainActivity.log_this(stringValue);
        }

    });
}
}
J.K
  • 2,290
  • 1
  • 18
  • 29
  • I couldn't initialize the loggingclass like you stated as the class am trying to do that implements a runnable. What am trying to achieve is getting the different strings to the TextView in the MainActivity using a global method called from different classes. – CodeZero Dec 23 '14 at 07:16
0

I solved the issue myself using a handler instead. Here is a sample code of what I did:

 Handler handler = new Handler(Looper.getMainLooper());
    handler.post(new Runnable() {
     public void run() {
          // UI code goes here
     }
});
CodeZero
  • 179
  • 2
  • 15