私は、ユーザーがボタンを押してテキストが生成され、次にテキストを共有/エクスポートするためのメニューオプションがあるAndroidアプリケーションを開発しています。アンドロイドで、メニュー項目のメソッドに文字列を送信するにはどうすればよいですか?
方法:ここで
は私の質問関連するコード部分(わかりやすくするために、私は全体のコードを貼り付けますが、あなたは何が必要な場合はちょうど私に知らせていない)
FloatingActionButton button = (FloatingActionButton) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String savedText = SomeMethod(); //the string used here is obtained through another method
TextView resp = (TextView) findViewById(R.id.TextOutput);
resp.setText(savedText);
}
});
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_export) {
exportPage();
return true;
}
return super.onOptionsItemSelected(item);
}
public void exportPage() {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, savedText);
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
}
です私は実際にSomeMethodメソッド(すなわちsavedText、コードの5行目のコメントを参照)から生成された文字列をexportPageメソッドに送ることはできますか?そのコードは、SomeMethodメソッドで生成された値ではなく、初期化された文字列値からsavedText文字列の値を取得します。
は、私が試してみました何:How to pass a string from one method to another method in Java
私はそれに応じて自分のコードを変更しようとしましたが、このような:
TextView resp = (TextView) findViewById(R.id.TextOutput);
resp.setText(savedText);
exportPage(); // <<< attempting to send the string to the exportPage method
public void exportPage() {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, this.savedText); //<<< modified
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
}
は、同様の質問の答えを探して、私は1つの方法は、ここで説明したものであることを発見しました しかし、これは、savedText文字列が初期化されていないというエラーを表示します。私はそれを初期化しようとしましたが、変更された値の代わりにそのグローバル値を再び使用します。
:私は(私は本当の初心者ですので、私はまだこれらの側面を学んでいる)多分私はローカル変数での作業の代わりに、インスタンス1だと思ったし、他の方法を試してみました。この時点でTextView resp = (TextView) findViewById(R.id.TextOutput);
resp.setText(savedText);
exportPage(savedText); // <<< attempting to send the string to the exportPage method
public void exportPage(String savedText) { //<<<< modified
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, savedText);
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
}
しかし、今は別のエラーがあります。メニュー項目が選択されたコード部分で:
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_export) {
exportPage();
return true;
}
return super.onOptionsItemSelected(item);
exportPageメソッドが文字列を必要とするため、エラーが発生します。私はexportPage(savedText);
でこれを修正しようとしましたが、現在はsavedTextという文字列を認識せず、初期化したいと考えています(元の問題に戻り、生成された文字列値ではなくグローバル文字列値を取得します)。
私が言ったように、私は初心者なので、これは私のために非常に単純なエラーであると確信していますが、私はそれを理解できません。何か案は?グローバル変数として
resp TextViewをグローバル変数として宣言する適切な方法(つまり、どこでどのように)ですか?私はそれが 'TextView resp =(TextView)findViewById(R.id.TextOutput);' onClickメソッドの外側で動くと思っていました( 'SetContentView(R.layout。activity.main); '行ですが、まだ"メソッドgetTextのシンボルメソッドが見つかりません "というエラーが発生します。 –
@DigitalDracula私は自分の答えを編集した、それをチェック! – AlexTa
成功!私はこれがうまくいくことを確認します - エレガントでシンプルなソリューション –