我正在做一个测试Android应用程序,即使有一些C ++经验,我也是初学者。
我在textArea中打印了一个变量,它计算了收到的推送通知的数量。我成功地找到了一种在接收事件中启动代码的方法,并希望增加它。
我找到的方法(不起作用)是获取textArea的内容,将其转换为整数,增量,转换为字符串,然后转换为char [],然后发送回文本区域。
Class ParseStarterProjectActivity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ParseAnalytics.trackAppOpened(getIntent());
TextView textView = (TextView) findViewById(R.id.textView1);
char [] test = Integer.toString(Integer.parseInt(textView.getText().toString()) + 1).toCharArray();
System.out.println(test);
textView.setText(test, 0, 10);
}
Class ParseApplication:
@Override
public void onCreate() {
super.onCreate();
// Add your initialization code here
Parse.initialize(this, Parse_ID, Parse_ID_2);
PushService.setDefaultPushCallback(this, ParseStarterProjectActivity.class);
ParseInstallation.getCurrentInstallation().saveInBackground();
ParseUser.enableAutomaticUser();
ParseACL defaultACL = new ParseACL();
// If you would like all objects to be private by default, remove this line.
defaultACL.setPublicReadAccess(true);
ParseACL.setDefaultACL(defaultACL, true);
}
因为它,应用程序崩溃了,logCat告诉我(如果我注释掉setText,测试仍然有很好的价值):
04-24 13:00:43.178: E/AndroidRuntime(1590): FATAL EXCEPTION: main
04-24 13:00:43.178: E/AndroidRuntime(1590): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.parse.starter/com.parse.starter.ParseStarterProjectActivity}: java.lang.IndexOutOfBoundsException: 0, 10
04-24 13:00:43.178: E/AndroidRuntime(1590): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
04-24 13:00:43.178: E/AndroidRuntime(1590): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
04-24 13:00:43.178: E/AndroidRuntime(1590): at android.app.ActivityThread.access$600(ActivityThread.java:130)
04-24 13:00:43.178: E/AndroidRuntime(1590): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
04-24 13:00:43.178: E/AndroidRuntime(1590): at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 13:00:43.178: E/AndroidRuntime(1590): at android.os.Looper.loop(Looper.java:137)
04-24 13:00:43.178: E/AndroidRuntime(1590): at android.app.ActivityThread.main(ActivityThread.java:4745)
04-24 13:00:43.178: E/AndroidRuntime(1590): at java.lang.reflect.Method.invokeNative(Native Method)
04-24 13:00:43.178: E/AndroidRuntime(1590): at java.lang.reflect.Method.invoke(Method.java:511)
04-24 13:00:43.178: E/AndroidRuntime(1590): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-24 13:00:43.178: E/AndroidRuntime(1590): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-24 13:00:43.178: E/AndroidRuntime(1590): at dalvik.system.NativeStart.main(Native Method)
04-24 13:00:43.178: E/AndroidRuntime(1590): Caused by: java.lang.IndexOutOfBoundsException: 0, 10
04-24 13:00:43.178: E/AndroidRuntime(1590): at android.widget.TextView.setText(TextView.java:3572)
04-24 13:00:43.178: E/AndroidRuntime(1590): at com.parse.starter.ParseStarterProjectActivity.onCreate(ParseStarterProjectActivity.java:24)
04-24 13:00:43.178: E/AndroidRuntime(1590): at android.app.Activity.performCreate(Activity.java:5008)
04-24 13:00:43.178: E/AndroidRuntime(1590): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
04-24 13:00:43.178: E/AndroidRuntime(1590): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
04-24 13:00:43.178: E/AndroidRuntime(1590): ... 11 more
答案 0 :(得分:1)
textView.setText(test, 0, 10);
导致 IndexOutOfBoundsException ,因为char [] test
的长度不是10。
因此,在尝试将其设置为TextView之前,请检查 char [] test 的长度。
类似的东西,
if(test.length >= 10)
{
textView.setText(test, 0, 10);
}else
{
textView.setText(test, 0, test.length);
}
答案 1 :(得分:1)
将该数字存储在类变量中,并在TextView
private int count = 0;
//when needed update count
count++;
//show value in TextView
private void showValue() {
textView.setText(String.valueOf(count));
}
答案 2 :(得分:0)
如果我知道你被困在哪里,你想要这样的东西
public static void main(String[] args) throws IOException {
int i = Integer.parseInt("100");
i++;
String v = String.valueOf(i);
System.out.println("v = " + v);
}
输出
v = 101
答案 3 :(得分:0)
您的代码应该是这样的:
首先,当你得到文本时,最好放入本地字符串。
ParseAnalytics.trackAppOpened(getIntent());
TextView textView = (TextView) findViewById(R.id.textView1);
String test = textView.getText().toString();
其次,当你想要一个int值时,最好用try和catch来包围它,以保证你的安全。 得到这样的价值:
int value;
try {
value = Integer.valueOf(test);
value++;
} catch (NumberFormatException e) {
value = 1;
}
第三,设置新的数字并记录如下:
System.out.println("Current value: "+value);
textView.setText(String.valueOf(test));
希望它有所帮助。