2012-11-05 120 views
2

問題很簡單,我對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的鍵都是相同的。

任何想法到底是怎麼回事?

+0

您可以檢查並打印'extras.isEmpty()'的結果。 – varevarao

+0

看看我的回答... –

回答

1

嘗試添加額外的變量,以意圖而不是捆綁 例:

i.putExtra(KEY_1, a); 
i.putExtra(KEY_2, b); 
i.putExtra(KEY_3, c); 

然後從其他活動的意圖 防爆檢索:

+1

我試了一下@ Sandy09說過的話後試了一下,這次它工作的很好。 唯一的區別是我叫做extras.getString(Key)。 謝謝! –

1
Bundle extras = getIntent().getExtras(); 
     if (extras != null) { 
      extras = extras.getBundle("KEY_EXTRAS"); 
      String status = extras.getString("KEY_TEXT"); 
     } 
0
//put value 
    Intent inatent = new Intent(this,text.class); 
    inatent_logo.putExtra("message","hello"); 
    startActivity(inatent); 

//get vale 
    String msg = getIntent().getStringExtra("message").toString(); 
0

這是如果您的數據數量較多,難以維護捆綁和捆綁到意圖的意圖螞蟻從一項活動分享給其他活動。

只需使用Intent與PuExtra()不同的參數。

你可以傳遞數據的數量在同樣的意圖:

發件人端:

創建你的意圖。

Intent My_Intent = new Intent(FromClass.this,ToClass.class); 

添加要與其他活動,分享你的價值。

My_Intent.putExtra("VAR_A",a_value); 
My_Intent.putExtra("VAR_B",b_value); 
My_Intent.putExtra("VAR_C",c_value); 

打電話給你的意圖。

StartActivity(My_Intent); 

接收方一端:

Intent My_Intent = getIntent(); 
First_Value=My_Intent.getStringExtra("VAR_A"); 
Sec_Value=My_Intent.getStringExtra("VAR_B"); 
Thred_Value=My_Intent.getStringExtra("VAR_C"); 

我認爲它方便您將數據從亨德爾一個活動到其他。

1

在活動A:

Intent i = new Intent(MainActivity.this, AnotherActivity.class); 
    Bundle b = new Bundle(); 
    b.putString("thisc", "my name"); 
    i.putExtra("bundle", b); 
    startActivity(i); 

在活性B:

**束髻= getIntent()getBundleExtra( 「捆綁」);

如果(bun.containsKey( 「thisc」)){

Log.i("TAG", bun.getString("thisc")); 

}否則{

Log.i("TAG", "no thisc"); 

} **

檢查的代碼的第一行中的活動B,那就是的主要區別居然!!