我正在开发一个 android 应用程序,其中有 1 个编辑文本......第一个编辑文本是给出用户的名字 .. 在其中输入字母,例如用户的名字......并且每个字母都有不同的值...我的问题是如何添加所有这些值....?
如何单独获取每个字母......并放入一个数组并添加其所有值
public class MainActivity extends Activity {
Button btn1;
EditText et1, et2, et3, et4, et5, et6;
Spinner sp1,sp2,sp3;
TextView txt1,txt2,txt3,txt4,txt5,txt6,txt7,txt8,txt9,txt10;
String month,day,year;
int sum;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void calc(View v)
{
EditText et1 = (EditText) findViewById (R.id.editText1);
/*TextView txt1 = (TextView) findViewById ( R.id.textView88);*/
sum=getSum(et1.getText().toString());
Intent i=new Intent(MainActivity.this, result.class);
i.putExtra("name", sum);
startActivity(i);
/*txt1.setText(sum + "");*/
}
private int getSum(String text)
{
// TODO Auto-generated method stub
int sum = 0;
char[] name = new char[text.length()];
name = text.toCharArray();
for(int i=0; i<text.length(); i++)
{
sum += value( name[i] );
}
while (sum>9)
{
sum = findDigitSum(sum);
}
return sum;
}
public int findDigitSum(int n)
{
int sum = 0;
while (n != 0)
{
sum += n % 10;
n = n / 10;
}
return sum;
}
private int value(char a) {
// TODO Auto-generated method stub
switch(a) {
case 'A':
return 1;
case 'B':
return 2;
case 'C':
return 3;
case 'D':
return 4;
case 'E':
return 5;
case 'F':
return 6;
case 'G':
return 7;
case 'H':
return 8;
case 'I':
return 9;
case 'J':
return 1;
case 'K':
return 2;
case 'L':
return 3;
case 'M':
return 4;
case 'N':
return 5;
case 'O':
return 6;
case 'P':
return 7;
case 'Q':
return 8;
case 'R':
return 9;
case 'S':
return 1;
case 'T':
return 2;
case 'U':
return 3;
case 'V':
return 4;
case 'W':
return 5;
case 'X':
return 6;
case 'Y':
return 7;
case 'Z':
return 8;
default:
return 0;
}
}
}
第二个活动result.java
public class result extends Activity {
TextView txt2;
/*private Intent getIntent;*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result_xm);
TextView txt2 = (TextView) findViewById(R.id.textView2);
txt2.setText(getIntent().getStringExtra("name"));
/*Intent iin= getIntent();
Bundle b = iin.getExtras();
if(b!=null)
{
String name = getIntent.getStringExtra("name");
txt2.setText(name);
}*/
}
}
Logcat 详细信息
08-26 20:25:47.579: E/Trace(1873): error opening trace file: No such file or directory (2)
08-26 20:25:49.179: D/dalvikvm(1873): GC_CONCURRENT freed 39K, 6% free 2788K/2948K, paused 19ms+89ms, total 195ms
08-26 20:25:50.311: I/Choreographer(1873): Skipped 33 frames! The application may be doing too much work on its main thread.
08-26 20:25:50.579: D/gralloc_goldfish(1873): Emulator without GPU emulation detected.
08-26 20:27:38.388: D/dalvikvm(1873): GC_CONCURRENT freed 125K, 8% free 3049K/3296K, paused 81ms+21ms, total 257ms
08-26 20:27:40.869: I/Choreographer(1873): Skipped 30 frames! The application may be doing too much work on its main thread.
08-26 20:27:42.209: D/AndroidRuntime(1873): Shutting down VM
08-26 20:27:42.229: W/dalvikvm(1873): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
08-26 20:27:42.349: E/AndroidRuntime(1873): FATAL EXCEPTION: main
08-26 20:27:42.349: E/AndroidRuntime(1873): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.numerology/com.example.numerology.result}: java.lang.NullPointerException
08-26 20:27:42.349: E/AndroidRuntime(1873): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
08-26 20:27:42.349: E/AndroidRuntime(1873): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-26 20:27:42.349: E/AndroidRuntime(1873): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-26 20:27:42.349: E/AndroidRuntime(1873): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-26 20:27:42.349: E/AndroidRuntime(1873): at android.os.Handler.dispatchMessage(Handler.java:99)
08-26 20:27:42.349: E/AndroidRuntime(1873): at android.os.Looper.loop(Looper.java:137)
08-26 20:27:42.349: E/AndroidRuntime(1873): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-26 20:27:42.349: E/AndroidRuntime(1873): at java.lang.reflect.Method.invokeNative(Native Method)
08-26 20:27:42.349: E/AndroidRuntime(1873): at java.lang.reflect.Method.invoke(Method.java:511)
08-26 20:27:42.349: E/AndroidRuntime(1873): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-26 20:27:42.349: E/AndroidRuntime(1873): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-26 20:27:42.349: E/AndroidRuntime(1873): at dalvik.system.NativeStart.main(Native Method)
08-26 20:27:42.349: E/AndroidRuntime(1873): Caused by: java.lang.NullPointerException
08-26 20:27:42.349: E/AndroidRuntime(1873): at com.example.numerology.result.onCreate(result.java:19)
08-26 20:27:42.349: E/AndroidRuntime(1873): at android.app.Activity.performCreate(Activity.java:5104)
08-26 20:27:42.349: E/AndroidRuntime(1873): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-26 20:27:42.349: E/AndroidRuntime(1873): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
08-26 20:27:42.349: E/AndroidRuntime(1873): ... 11 more