0

I'm fighting with problem. I want my Textviews to display Integer values and be refreshed every second.

My Integer Objects

  public Integer Money[] = new Integer[1];

 public void MoneyMaker(int count,int c){
        Money[c] = count;
    }

 public void MakeMoney(){
        int count = 0;
        MoneyMaker(0, count);
    }

In MainActivity I reach them as

MainHolder Main = new MainHolder();

I got textview like these:

    <TextView
        android:text="Money: "
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/MoneyText"
        android:layout_marginLeft="67dp"
        android:layout_marginStart="67dp"
        android:layout_alignTop="@+id/btn_Money"
        android:layout_toRightOf="@+id/btn_Money"
        android:layout_toEndOf="@+id/btn_Money"
        android:layout_marginTop="38dp" />

  <TextView
        android:text="Box: "
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/BoxesText"
        android:layout_marginLeft="67dp"
        android:layout_marginStart="67dp"
        android:layout_alignTop="@+id/btn_Boxes"
        android:layout_toRightOf="@+id/btn_Boxes"
        android:layout_toEndOf="@+id/btn_Boxes"
        android:layout_marginTop="38dp" />

Got "ĢameLoop" in mainActivity onCreate method

   Thread t = new Thread() {

        @Override
        public void run() {
            try {
                while (!isInterrupted()) {
                    Thread.sleep(1000);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Refresh();
                            SaveFile();
                        }
                    });
                }
            } catch (InterruptedException e) {
            }
        }
    };

    t.start();

And got Refresh method

 public void Refresh(){
    TextView MoneyTXT = (TextView) findViewById(R.id.MoneyText);
    MoneyTXT.setText("Money: " +Main.Money[0]);
    TextView BoxesTXT = (TextView) findViewById(R.id.BoxesText);
    BoxesTXT.setText("Boxes: " +Main.Boxes[0]);
}

OnCreate Class

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_navigation);

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    LoadFile();

    Thread t = new Thread() {

        @Override
        public void run() {
            try {
                while (!isInterrupted()) {
                    Thread.sleep(1000);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Refresh();
                            SaveFile();
                        }
                    });
                }
            } catch (InterruptedException e) {
            }
        }
    };

    t.start();
}

I don't show any error in code, but when launching app, it automatically shuts of. In logcat, got error:

11-07 19:10:10.007 29809-29809/com.crelix.crelix E/AndroidRuntime: FATAL EXCEPTION: main
                                                               Process: com.crelix.crelix, PID: 29809
                                                               java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                                   at com.crelix.crelix.NavigationActivity.Refresh(NavigationActivity.java:147)
                                                                   at com.crelix.crelix.NavigationActivity$1$1.run(NavigationActivity.java:66)
                                                                   at android.os.Handler.handleCallback(Handler.java:810)
                                                                   at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                   at android.os.Looper.loop(Looper.java:189)
                                                                   at android.app.ActivityThread.main(ActivityThread.java:5529)
                                                                   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:950)
                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
MsCrelix
  • 55
  • 8

1 Answers1

1

as error shows, the textview remain null even after initialisation, it means.

the issue with inflating the layout.

  1. maybe forgot or inflate after your method called Refresh().
  2. inflated layout don't contain that textview.(due to another version of the same layout like landscape and portrait or multiple size layout)

check once and let me know if the issue is still there.

Happy Coding

RBK
  • 2,481
  • 2
  • 30
  • 52
  • I added my Refresh method lines into my Fragment "onCreateView" method. Now it shows, but my problem now is refreshing fragment. becouse my refresh method dosn't work in fragment. – MsCrelix Nov 07 '16 at 17:39