0

In MainActivity:

 private TextView log;

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

    log= (TextView) findViewById(R.id.log);
 }

 public void showMessage(final int messageType, final String msg){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
               log.setText(msg); // here is the problem
            }
         }
 });

In logClass:

 private static MainActivity mainActivity;

 public logClass() {
    mainActivity = new MainActivity();
 }

 public void log() {
    try {
        mainActivity.showMessage(MessageTag.TIP, "Connecting...");
    } catch(Exception el) {
        el.printStackTrace();
    }
 }

In callFragment:

@Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.start:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        logClass.log();

                    }
                }).start();
        }
    }

But I get this problem:

FATAL EXCEPTION: main
              Process: com.test.testing, PID: 7636
              java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
                  at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:117)
                  at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:149)
                  at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:29)
                  at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:54)
                  at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:202)
                  at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:183)
                  at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)
                  at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
                  at com.test.testing.MainActivity$2.run(MainActivity.java:97)
                  at android.os.Handler.handleCallback(Handler.java:739)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:135)
                  at android.app.ActivityThread.main(ActivityThread.java:5254)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:372)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)
Alvin
  • 8,219
  • 25
  • 96
  • 177
  • 1
    looks like your `log` is null. This could happen either because you do not have `log` named TextView in `activity_main.xml` OR your `oncreate` method in MainActivity is not called yet . If you had passed instance of mainActivity from the MainActivity itself to your `Another` class, then you can ignore the second possibility. – Jimmy Apr 12 '18 at 15:07
  • I update the question. I think I call it from another fragment. – Alvin Apr 12 '18 at 15:14
  • If you want you can set your TextView public. And you can call it from another activities like MainActivity.textview.settext(""); – Hayk Mkrtchyan Apr 12 '18 at 15:16
  • Instead of writing "In MainActiivty", you should just write the code: `class MainActivity extends Activity { ... }`. Furthermore, your code is missing at least one variable declaration. Please give a [mcve] that does not have any compiler errors. – Code-Apprentice Apr 12 '18 at 15:16
  • 1
    Also provide the full stack trace. – Code-Apprentice Apr 12 '18 at 15:20
  • show how you are getting instance of `MainActivity` in your log class. If you are getting this from the "MainActivity" itself, then the issue is something else. You need to add full stacktrace . – Jimmy Apr 12 '18 at 15:23
  • @Jimmy I updated. – Alvin Apr 12 '18 at 15:42
  • 1
    you can not call like this 'mainActivity = new MainActivity();' . You have to get this instance from the 'MainActivity' itself . something like this from MainActivity , new LogClass(this); where `this` is instance of `MainActivity`. In simple word, NEVER create instance of Activity class yourself unless you have absolute reason to do , which I have never felt . – Jimmy Apr 12 '18 at 15:46

2 Answers2

0

I do not think you can edit things in another activity from another activity.

edit array from another activity

This answer elaborates on how it is done if possible.

Maaz Bin Musa
  • 479
  • 5
  • 9
  • The `Another` class here is not necessarily Activity. Using callback from service class to update UI is not unusual. – Jimmy Apr 12 '18 at 15:11
  • I update the question. I think I call it from another fragment. – Alvin Apr 12 '18 at 15:14
0

if callFragment is inflated on MainActivity then you can simply do this.

@Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.start:
                ((MainActivity) getActivity()).showMessage(MessageTag.TIP, "Connecting...");
        }
    }

also i did not find use of Thread in your code. Because if android runs on main thread by default which can change your views. So you can remove redundant your code of wrapping in thread.

Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212