0

我想知道为什么我的这部分编码不起作用,并且程序在切换时不断给我同样的错误;

(对于低于 1.7 的源级别,无法打开 String 类型的值。只允许可转换的 int 值或枚举变量)

我想读取我保存为 txt 文件的文件并将其显示在程序上。

例如:我在那里写了“电子邮件”作为案例,所以我写了我想要的并将它保存到一个txt文件中以便在这个开关中读取。

谁能帮我解决这个问题?深表赞赏。谢谢。

这是我的代码:

        private void ExecuteCommands(String filename) {
     //Find the directory for the SD Card using the API
    //*Don't* hardcode "/sdcard"
    File sdcard = Environment.getExternalStorageDirectory();

    //Get the text file
    File file = new File(sdcard, filename + ".txt");

    //Read text from file
    StringBuilder text = new StringBuilder();

    try {
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;

        while ((line = br.readLine()) != null) {
            String[] tmp = line.split(" ");

                        //this switch case giving me problem
                      switch(tmp[0]){

             case "Email":
                String subject = sbj.getText().toString();
                String message = messageBody.getText().toString();
                String to = destinationAddress.getText().toString();

                Intent emailActivity = new Intent(Intent.ACTION_SEND);

                //set up the recipient address
                emailActivity.putExtra(Intent.EXTRA_EMAIL, new String[] { to });

                //set up the email subject
                emailActivity.putExtra(Intent.EXTRA_SUBJECT, subject);

                //you can specify cc addresses as well
                // email.putExtra(Intent.EXTRA_CC, new String[]{ ...});
                // email.putExtra(Intent.EXTRA_BCC, new String[]{ ... });

                //set up the message body
                emailActivity.putExtra(Intent.EXTRA_TEXT, message);

                emailActivity.setType("message/rfc822");

                startActivity(Intent.createChooser(emailActivity, "Select your Email Provider :"));
                break;

            case "SMS message":
             String phoneNo = textPhoneNo.getText().toString();
              String sms = textSMS.getText().toString();

              try {
                SmsManager smsManager = SmsManager.getDefault();
                smsManager.sendTextMessage(phoneNo, null, sms, null, null);
                Toast.makeText(getApplicationContext(), "SMS Sent!",
                            Toast.LENGTH_LONG).show();
              } catch (Exception e) {
                Toast.makeText(getApplicationContext(),
                    "SMS faild, please try again later!",
                    Toast.LENGTH_LONG).show();
                e.printStackTrace();


                break;
              }
            }
        }


    }



        catch (IOException e) {
        //You'll need to add proper error handling here
    }

}

4

1 回答 1

1

如何Java在 7 下的版本中不能在字符串上切换/大小写,考虑使用枚举,但是您的切换案例字符串如何包含空格您无法通过valutOf枚举方法检索枚举常量,但您可以添加自己的方法来检索基于特定字符串的对应枚举。像这样。

enum Type {

EMAIL {
    @Override
    public boolean counterpart(String value) {
        if (value.equals(EMAIL)) {
            return true;
        }
        return false;
    }
},
SMS {
    @Override
    public boolean counterpart(String value) {
        if (value.equals(SMS_TAG)) {
            return true;
        }
        return false;
    }
};
private static final String EMAIL_TAG = "Email";
private static final String SMS_TAG = "SMS Message";

public abstract boolean counterpart(String value);

}

以及一个基于字符串值返回相应类型的任何地方的公共静态方法。

 public static Type  getType( String value ) {
    for (Type t : Type.values()) {
        if (t.counterpart(value )) {
            return t;
        }
    }
    return Type.EMAIL;
}  

那么你应该有这样的开关

    Type type = getType( param[ 0 ] );

    switch( type ){
        case EMAIL:
            break;
        case SMS:
            break;
        default:
            break;
    }
于 2013-10-20T01:45:15.507 回答