0

コーディングのこのセクションが機能しない理由を知りたいのですが、プログラムはスイッチで同じエラーを出し続けます。

(ソースレベルが 1.7 未満の場合、String 型の値をオンにすることはできません。変換可能な int 値または enum 変数のみが許可されます)

保存したファイルを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

7未満のバージョンで文字列にスイッチ/ケースをJava使用できない方法、列挙型の使用を検討しますが、スイッチケース文字列に空白を含める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 メソッド。

 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 に答える