我正在与问题作斗争。我希望我的Textviews显示整数值并每秒刷新一次。
我的整数对象
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);
}
在MainActivity中,我将其作为
MainHolder Main = new MainHolder();
我得到了这样的textview:
<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" />
在mainActivity onCreate方法中获得了“ĢameLoop”
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();
获得了Refresh方法
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();
}
我没有在代码中显示任何错误,但在启动应用时,它会自动关闭。 在logcat中,得到了错误:
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)
答案 0 :(得分:1)
如错误所示,即使在初始化后,textview仍保持为null, 这意味着。
夸大布局的问题。
检查一次,如果问题仍然存在,请告诉我。
快乐编码