質問は簡単です。私はAndroidにまったく慣れていませんが、アクティビティAからアクティビティBにインテントを介して渡されたエクストラを取得することはできません。
アクティビティAを参照してください。これは実際にはListFragmentであり、onListItemClick()を実装して、インテントを介して別のアクティビティを開始します。
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Log.i("FragmentList", "Item clicked: " + id);
Intent i = new Intent(getActivity(), ExpandedTweetView.class);
twitter4j.Status status = adapter.getItem(position);
Bundle extras = new Bundle();
extras.putString(KEY_TEXT, status.getText());
extras.putString(KEY_HANDLE, status.getUser().getScreenName());
extras.putString(KEY_NAME, status.getUser().getName());
extras.putString(KEY_TIMESTAMPS, status.getCreatedAt().toString());
extras.putLong(KEY_RETWEETS, status.getRetweetCount());
i.putExtra(KEY_EXTRAS, extras);
startActivity(i);
}
この部分は正常に機能します。Log.v(TAG、 "status.getText()"を使用してテストし、getItem()を介して空のアイテムを渡すアダプターからエラーが発生していないことを確認しました。
アクティビティBのコードは次のとおりです。
public class ExpandedTweetView extends Activity {
TextView text;
TextView name;
TextView handle;
TextView createdAt;
TextView retweets;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.expanded_list_item);
Bundle extras = getIntent().getExtras();
ActionBar actionBar = getActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
text = (TextView) findViewById(R.id.h_content);
name = (TextView) findViewById(R.id.h_name);
handle = (TextView) findViewById(R.id.h_handle);
createdAt = (TextView) findViewById(R.id.h_timestamp);
retweets = (TextView) findViewById(R.id.h_retweet_count);
if(extras != null) {
text.setText(extras.getString(TimelineFragment.KEY_TEXT));
name.setText(extras.getString(TimelineFragment.KEY_NAME));
handle.setText(extras.getString(TimelineFragment.KEY_HANDLE));
createdAt.setText(extras.getString(TimelineFragment.KEY_TIMESTAMPS));
retweets.setText(String.valueOf(extras.getLong(TimelineFragment.KEY_RETWEETS)));
}
}
ご覧のとおり、他のアプリケーションで同じコードを使用して、エクストラを取得するために適切なコードを使用していると思います。ExpandedTweetViewがインテントを介して作成された場合、すべてのtextViewが空になる理由はわかりません。参照:https ://www.dropbox.com/s/pso6jbyn6rpks9n/empty_activity.png
さらに奇妙なのは、最初にこれを呼び出してバンドルがnullかどうかを確認しようとしたことです。
if (extras == null) {
Log.v(TAG, "Extras are empty :(");
}
しかし、その行は実行されませんでした。つまり、バンドルはnullではありません。また、バンドルから個々の文字列を取得するために使用されているキーが一致していない可能性があると思いました。しかし、それを改善するために、両側で使用できる定数を作成することにしました。コードからわかるように、Extraを設定するためのキーとExtraを取得するためのキーはどちらも同じです。
一体何が起こっているのかについて何かアイデアはありますか?