我正在创建一个Android应用程序,需要在我的一个布局中更改某些textView的文本。这是我的代码。
public void hudSetup() {
Player stats = new Player(this);
stats.open();
String playerName = stats.getStringField(stats.KEY_NAME);
String playerCity = stats.getStringField(stats.KEY_CITY);
int playerHealth = stats.getIntField(stats.KEY_HEALTH);
int playerEnergy = stats.getIntField(stats.KEY_ENERGY);
int playerRank= stats.getIntField(stats.KEY_RANK);
int playerCash = stats.getIntField(stats.KEY_CASH);
stats.close();
name.setText(playerName);
city.setText(playerCity);
energy.setText(playerEnergy);
health.setText(playerHealth);
cash.setText(playerCash);
rank.setText(playerRank);
}
我确保我的所有textView都已初始化。代码编译,但是当我运行应用程序并运行hudSetup()方法时,我得到一个nullPointerException。
org.eclipse.swt.SWTException: Failed to execute runnable (java.lang.NullPointerException)
at org.eclipse.swt.SWT.error(SWT.java:4282)
at org.eclipse.swt.SWT.error(SWT.java:4197)
at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:138)
at org.eclipse.swt.widgets.Display.runAsyncMessages(Display.java:4140)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3757)
at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2696)
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2660)
at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2494)
at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:674)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:667)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:123)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:344)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:622)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:577)
at org.eclipse.equinox.launcher.Main.run(Main.java:1410)
Caused by: java.lang.NullPointerException
at com.android.ddmuilib.logcat.LogCatReceiverFactory.newReceiver(LogCatReceiverFactory.java:57)
at com.android.ddmuilib.logcat.LogCatPanel.deviceSelected(LogCatPanel.java:229)
at com.android.ddmuilib.SelectionDependentPanel.deviceSelected(SelectionDependentPanel.java:52)
at com.android.ide.eclipse.ddms.views.SelectionDependentViewPart.selectionChanged(SelectionDependentViewPart.java:67)
at com.android.ide.eclipse.ddms.DdmsPlugin$6.run(DdmsPlugin.java:690)
at org.eclipse.swt.widgets.RunnableLock.run(RunnableLock.java:35)
at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:135)
... 22 more
我做错了什么?任何帮助将不胜感激。
谢谢!
答案 0 :(得分:3)
TextView.setText(int)
使用在R.java中定义为整数的资源。
出于您的目的,您可能希望首先将整数转换为String。
像:
cash.setText(playerCash + "");
或者像@Pedro一样说:
cash.setText(String.valueOf(playerCash));