强制关闭问题

时间:2014-04-22 07:35:59

标签: java android

我试图构建一个最多占用6个整数然后将其转换为单词的程序,不幸的是,当我尝试运行时,我无法确定我是做对还是错。它强制关闭。

这是我的MainActivity.java

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_main);

    final EditText numbers =  (EditText) findViewById(R.id.editText1);
    String numberz =numbers.getText().toString();
    final long number = Long.parseLong(numberz);

    Button btnConvert = (Button) findViewById(R.id.button1);     
    btnConvert.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            String returnz = Words.convert(number); 

                       }  });


        }}

这是我单击按钮Words.java时应该调用的类:

import android.support.v7.app.ActionBarActivity;
import android.widget.EditText;

public class Words extends ActionBarActivity {

final EditText result = (EditText) findViewById(R.id.editText2);


      private static final String[] tensNames = {
        "",
        " ten",
        " twenty",
        " thirty",
        " forty",
        " fifty",
        " sixty",
        " seventy",
        " eighty",
        " ninety"
      };

      private static final String[] numNames = {
        "",
        " one",
        " two",
        " three",
        " four",
        " five",
        " six",
        " seven",
        " eight",
        " nine",
        " ten",
        " eleven",
        " twelve",
        " thirteen",
        " fourteen",
        " fifteen",
        " sixteen",
        " seventeen",
        " eighteen",
        " nineteen"
      };

      private Words() {}

      private static String convertLessThanOneThousand(int number) {
        String soFar;

        if (number % 100 < 20){
          soFar = numNames[number % 100];
          number /= 100;
        }
        else {
          soFar = numNames[number % 10];
          number /= 10;

          soFar = tensNames[number % 10] + soFar;
          number /= 10;
        }
        if (number == 0) return soFar;
        return numNames[number] + " hundred" + soFar;
      }


      public static String convert(long number) {
        // 0 to 999 999 999 999
        if (number == 0) { return "zero"; }

        String snumber = Long.toString(number);

        // pad with "0"
        String mask = "000000000000";
        DecimalFormat df = new DecimalFormat(mask);
        snumber = df.format(number);

        // XXXnnnnnnnnn
        int billions = Integer.parseInt(snumber.substring(0,3));
        // nnnXXXnnnnnn
        int millions  = Integer.parseInt(snumber.substring(3,6));
        // nnnnnnXXXnnn
        int hundredThousands = Integer.parseInt(snumber.substring(6,9));
        // nnnnnnnnnXXX
        int thousands = Integer.parseInt(snumber.substring(9,12));

        String tradBillions;
        switch (billions) {
        case 0:
          tradBillions = "";
          break;
        case 1 :
          tradBillions = convertLessThanOneThousand(billions)
          + " billion ";
          break;
        default :
          tradBillions = convertLessThanOneThousand(billions)
          + " billion ";
        }
        String result =  tradBillions;

        String tradMillions;
        switch (millions) {
        case 0:
          tradMillions = "";
          break;
        case 1 :
          tradMillions = convertLessThanOneThousand(millions)
             + " million ";
          break;
        default :
          tradMillions = convertLessThanOneThousand(millions)
             + " million ";
        }
        result =  result + tradMillions;

        String tradHundredThousands;
        switch (hundredThousands) {
        case 0:
          tradHundredThousands = "";
          break;
        case 1 :
          tradHundredThousands = "one thousand ";
          break;
        default :
          tradHundredThousands = convertLessThanOneThousand(hundredThousands)
             + " thousand ";
        }
        result =  result + tradHundredThousands;

        String tradThousand;
        tradThousand = convertLessThanOneThousand(thousands);
        result =  result + tradThousand;

        // remove extra spaces!
        return result.replaceAll("^\\s+", "").replaceAll("\\b\\s{2,}\\b", " ");}



      public void main(String[] args) {
        result.setText("*** " + Words.convert(0));
        result.setText("*** " + Words.convert(1));
      result.setText("*** " + Words.convert(16));
      result.setText("*** " + Words.convert(100));
      result.setText("*** " + Words.convert(118));
      result.setText("*** " + Words.convert(200));
      result.setText("*** " + Words.convert(219));
      result.setText("*** " + Words.convert(800));
      result.setText("*** " + Words.convert(801));
      result.setText("*** " + Words.convert(1316));
      result.setText("*** " + Words.convert(1000000));
      result.setText("*** " + Words.convert(2000000));
      result.setText("*** " + Words.convert(3000200));
      result.setText("*** " + Words.convert(700000));
      result.setText("*** " + Words.convert(9000000));
      result.setText("*** " + Words.convert(9001000));
      result.setText("*** " + Words.convert(123456789));
      result.setText("*** " + Words.convert(2147483647));
      result.setText("*** " + Words.convert(3000000010L));

      }}

我的logcat:

04-22 02:18:05.144: D/dalvikvm(1066): GC_FOR_ALLOC freed 109K, 6% free 3078K/3256K, paused 30ms, total 32ms
04-22 02:18:05.334: D/gralloc_goldfish(1066): Emulator without GPU emulation detected.
04-22 02:18:09.134: I/Choreographer(1066): Skipped 78 frames!  The application may be doing too much work on its main thread.
04-22 02:18:21.704: I/Choreographer(1066): Skipped 37 frames!  The application may be doing too much work on its main thread.
04-22 02:51:00.024: I/Choreographer(1066): Skipped 33 frames!  The application may be doing too much work on its main thread.
04-22 03:00:00.054: I/Choreographer(1066): Skipped 40 frames!  The application may be doing too much work on its main thread.
04-22 03:00:01.084: I/Choreographer(1066): Skipped 46 frames!  The application may be doing too much work on its main thread.
04-22 03:27:10.474: I/Choreographer(1066): Skipped 121 frames!  The application may be doing too much work on its main thread.
04-22 03:29:35.064: D/AndroidRuntime(1122): Shutting down VM
04-22 03:29:35.064: W/dalvikvm(1122): threadid=1: thread exiting with uncaught exception (group=0xb2abfba8)
04-22 03:29:35.094: E/AndroidRuntime(1122): FATAL EXCEPTION: main
04-22 03:29:35.094: E/AndroidRuntime(1122): Process: com.example.torres, PID: 1122
04-22 03:29:35.094: E/AndroidRuntime(1122): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.torres/com.example.torres.MainActivity}: java.lang.NumberFormatException: Invalid long: ""
04-22 03:29:35.094: E/AndroidRuntime(1122):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-22 03:29:35.094: E/AndroidRuntime(1122):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-22 03:29:35.094: E/AndroidRuntime(1122):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-22 03:29:35.094: E/AndroidRuntime(1122):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-22 03:29:35.094: E/AndroidRuntime(1122):     at android.os.Handler.dispatchMessage(Handler.java:102)
04-22 03:29:35.094: E/AndroidRuntime(1122):     at android.os.Looper.loop(Looper.java:136)
04-22 03:29:35.094: E/AndroidRuntime(1122):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-22 03:29:35.094: E/AndroidRuntime(1122):     at java.lang.reflect.Method.invokeNative(Native Method)
04-22 03:29:35.094: E/AndroidRuntime(1122):     at java.lang.reflect.Method.invoke(Method.java:515)
04-22 03:29:35.094: E/AndroidRuntime(1122):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-22 03:29:35.094: E/AndroidRuntime(1122):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-22 03:29:35.094: E/AndroidRuntime(1122):     at dalvik.system.NativeStart.main(Native Method)
04-22 03:29:35.094: E/AndroidRuntime(1122): Caused by: java.lang.NumberFormatException: Invalid long: ""
04-22 03:29:35.094: E/AndroidRuntime(1122):     at java.lang.Long.invalidLong(Long.java:124)
04-22 03:29:35.094: E/AndroidRuntime(1122):     at java.lang.Long.parseLong(Long.java:345)
04-22 03:29:35.094: E/AndroidRuntime(1122):     at java.lang.Long.parseLong(Long.java:318)
04-22 03:29:35.094: E/AndroidRuntime(1122):     at com.example.torres.MainActivity.onCreate(MainActivity.java:19)
04-22 03:29:35.094: E/AndroidRuntime(1122):     at android.app.Activity.performCreate(Activity.java:5231)
04-22 03:29:35.094: E/AndroidRuntime(1122):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-22 03:29:35.094: E/AndroidRuntime(1122):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-22 03:29:35.094: E/AndroidRuntime(1122):     ... 11 more
04-22 03:29:35.134: D/dalvikvm(1122): GC_FOR_ALLOC freed 100K, 6% free 3087K/3256K, paused 39ms, total 43ms

2 个答案:

答案 0 :(得分:0)

应用程序启动时,您的numberz变量为null。试试这个....

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.fragment_main);

  final EditText numbers =  (EditText) findViewById(R.id.editText1);

  Button btnConvert = (Button) findViewById(R.id.button1);     
  btnConvert.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
       String numberz =numbers.getText().toString();
       try {
           final long number = Long.parseLong(numberz);
           String returnz = Words.convert(number); 
       } catch ( NumberFormatException e) {
           //Toast.makeToast("illegal number or empty number" , toast.long)
       }


    } 
     });
    }
  }

答案 1 :(得分:0)

String numberz =numbers.getText().toString(); 

将在numberz="";类中返回convert(Words}方法,结果为NumberFormatException : Invalid Long。因此,您必须在onClickListener()按钮中编写以下代码。

btnConvert.setOnClickListener(new OnClickListener() 
   {
    @Override
    public void onClick(View arg0) {
       String numberz =numbers.getText().toString();
       try {
           final long number = Long.parseLong(numberz);
           String returnz = Words.convert(number); 
       } catch ( NumberFormatException e) {
           //Toast.makeToast("illegal number or empty number" , toast.long)
       }
    } 
     });