2015-01-04 74 views
0

她的是我的代碼無法理解爲什麼字符串值突然變爲空?

public static class DetailFragment extends Fragment { 
     private String forecastData; 

     private static final String LOG_TAG = DetailFragment.class.getSimpleName(); 

     private static final String FORECAST_SHARE_HASHTAG ="#SunshineApp"; 

     public DetailFragment() { 

     } 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setHasOptionsMenu(true); 

     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
           Bundle savedInstanceState) { 
      View rootView = inflater.inflate(R.layout.fragment_detail, container, false); 
      //receive forecast data from the ForeCast Fragment 
      Intent intent=getActivity().getIntent(); 
      forecastData=intent.getStringExtra(Intent.EXTRA_TEXT); 
      Log.v(LOG_TAG,"data is "+forecastData); 
      TextView textView=(TextView)rootView.findViewById(R.id.detail_text); 
      textView.setText(forecastData); 

      return rootView; 
     } 

     @Override 
     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 
      super.onCreateOptionsMenu(menu, inflater); 
      inflater.inflate(R.menu.menu_detailfragment,menu); 

      MenuItem menuItem=menu.findItem(R.id.action_share); 
      // Get the provider and hold onto it to set/change the share intent. 
      ShareActionProvider mShareActionProvider = 
        (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem); 

      // Attach an intent to this ShareActionProvider. You can update this at any time, 
      // like when the user selects a new piece of data they might like to share. 
      if (mShareActionProvider!= null) { 
       mShareActionProvider.setShareIntent(createShareIntent()); 

      } 
      else 
      { 
       Log.v(LOG_TAG,"Share Action Provider is null"); 
      } 
     } 
     public Intent createShareIntent() 
     { 
      Log.v(LOG_TAG,"data is "+forecastData); 
      Intent intent=new Intent(Intent.ACTION_SEND); 
      intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); 
      intent.setType("text/plain"); 
      intent.putExtra(Intent.EXTRA_TEXT,forecastData+DetailFragment.FORECAST_SHARE_HASHTAG); 
      Log.v(LOG_TAG,"data is "+forecastData); 
      return intent; 
     } 

我絕對相信的意圖是正確的解僱,因爲我可以與其他應用程序共享和它的作品,很清楚,如果你看到下面

01-04 18:36:02.370 6121-6121/com.example.droid.sunshine V/DetailFragment﹕ in optionMenu method data is null 
01-04 18:36:02.380 6121-6121/com.example.droid.sunshine V/DetailFragment﹕ in create intent data is null 
01-04 18:36:02.380 6121-6121/com.example.droid.sunshine V/DetailFragment﹕ in create intent data is null 
01-04 18:36:02.650 6121-6121/com.example.droid.sunshine V/DetailFragment﹕ in view method data is Wed, Jan 7 - Clear - 27/24 

日誌爲什麼是這樣,我認爲OnCreate()視圖方法之前調用OnCreateOptionsMenu() ?我該如何糾正這一點?

+0

編輯:我移動了'Intent intent = getActivity()。getIntent(); forecastData = intent.getStringExtra(Intent.EXTRA_TEXT);'到'onCreate()'方法,仍然sam結果 – 2015-01-04 13:11:26

+2

**瞭解如何使用調試器**。試圖在沒有它的情況下構建應用程序就像試圖用丟失一半工具來構建房屋。對於基本測試, – Simon 2015-01-04 13:15:27

+0

記錄不夠? – 2015-01-04 13:18:42

回答

3

這裏有一個想法:

  • 根據你的日誌,輸出首先從onCreateOptionsMenu()方法顯示,只有最後一個是從onCreateView()。這意味着onCreateOptionsMenu()onCreateView之前被調用,這將解釋爲什麼一切都爲空。

  • 嘗試移動setHasOptionsMenu(true)別的地方,也許在構造函數,應該確保onCreateOptionsMenu()onCreateonCreateView後調用。嘗試一下。

+0

不,不,請再讀一遍,一切都很好,我將天氣數據傳遞給DetailActivityFragment,我將它提取到一個字符串forecastData(當前的片段類的pivotate變量),當我記錄它的值是正確的日誌但記錄相同的值在另一個片段的方法(沒有靜態方法,你可以看到),顯示的值爲空 – 2015-01-04 13:16:45

+0

順便說一句我沒有在這裏傳遞數據,它通過另一個活動的意圖作爲額外的傳遞和接受這個片段 – 2015-01-04 13:17:59

+0

你能否幫我一個忙,並用'forecastData = new String(intent.getStringExtra(Intent.EXTRA_TEXT));'替換'forecastData = intent.getStringExtra(Intent.EXTRA_TEXT);'並且讓我知道if它仍然是空的? – AndreiBogdan 2015-01-04 13:24:28

1

imho您應該將您的代碼從onCreateView()移動到onActvivityCreated()。

onCreateView是爲了返回視圖。

onActivityCreated表示關聯的活動已完成onCreate()方法的設置。