【发布时间】:2014-05-10 09:48:51
【问题描述】:
我有这个代码用于选择一个 textView 并复制到剪贴板:
txt=(TextView)findViewById(R.id.textView1);
String stringYouExtracted = txt.getText().toString();
int startIndex = txt.getSelectionStart();
int endIndex = txt.getSelectionEnd();
stringYouExtracted = stringYouExtracted.substring(startIndex, endIndex);
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
clipboard.setText(stringYouExtracted);
我想放一个按钮,当我按下它时,发送文本启用并运行,我也有这个代码:
btn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, stringYouExtracted);
sendIntent.setType("text/plain");
startActivity(sendIntent);
}
});
但是这个错误出现在setOnClickListener(setOnClickListener的第三行):
不能在不同方法中定义的内部类中引用非最终变量 stringYouExtracted
SDK 建议我在第一行代码的第二行之前添加 final。当我这样做时,会出现第 5 行第一个代码的另一个错误:
最终的局部变量 stringYouExtracted 无法赋值。它必须为空且不使用复合赋值
并建议我从我为解决先前错误而添加的第一行代码的第二行中删除 final
我能做什么?
【问题讨论】:
标签: android android-intent send