問題很簡單,我對Android並不完全陌生,但我不能在我的生活中檢索通過活動A向活動B傳遞的附加內容。getIntent.getExtras()不返回空值,但沒有值。
請參閱活動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);
}
這部分只是工作,我測試了它USNG 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和Key以檢索Extra的鍵都是相同的。
任何想法到底是怎麼回事?
您可以檢查並打印'extras.isEmpty()'的結果。 – varevarao
看看我的回答... –