我想知道为什么我的这部分编码不起作用,并且程序在切换时不断给我同样的错误;
(对于低于 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
}
}