私は Udacity で Android アプリ (この場合は天気) を作成するコースを受講しています。共有アクションの実装に問題があります。別のフォーラムからアドバイスを受けた後、これは単なる学習活動であるため、SDK の最小バージョンを 10 または 11 から 17 に変更しました。現在、アクションバーに「共有」ボタンが表示されていますが、タップしても何も起こりません。オーバーフローメニューに入れてみましたが、それでも何もありません。デバッグを試みましたが、ボタンのクリックが処理される場所がわかりません。デバッガーが通過して shareIntent オブジェクトを作成しますが、その後は何も起こらないようです。このドキュメントを見ましたが、ビューの onOptionsItemSelected で共有を処理しようとすると、createShareIntent の呼び出しで null ポインター例外が発生します。
ネストされたフラグメントの onCreateOptionsMenu は次のとおりです。
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.detail_fragment, menu);
MenuItem item = menu.findItem(R.id.action_share);
ShareActionProvider mShareActionProvider = new ShareActionProvider(getActivity());
if(mShareActionProvider != null) {
mShareActionProvider.setShareIntent(createShareIntent());
} else {
Log.d(LOG_TAG, "Share action provider is null");
}
}
問題のあるコードをコメントアウトした、含まれているビューの onOptionsItemSelected を次に示します。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
startActivity(new Intent(this, SettingsActivity.class));
return true;
} else if (id == R.id.action_share) {
//DetailFragment details = (DetailFragment) getFragmentManager().findFragmentByTag("detailFragment");
//startActivity(details.createShareIntent());
}
return super.onOptionsItemSelected(item);
}
createShareIntent メソッドは次のとおりです。
private Intent createShareIntent() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, mForecastStr + FORECAST_SHARE_HASHTAG);
return shareIntent;
}