2014-03-13 42 views
3

我要瘋狂的Android編程。沒有什麼工作正常..意圖和putExtra不起作用

這是怎麼回事?

錯誤:getIntent() is undefined for type View

任何想法?

public static class PlaceholderFragment extends Fragment { 

    public PlaceholderFragment() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_quiz, container, 
       false); 

     TextView text = (TextView)rootView.findViewById(R.id.ttv); 

     Bundle intentBundle = rootView.getIntent().getExtras(); 
     int question_cat = intentBundle.getInt("question_cat"); 

     text.setText(question_cat); 

     return rootView; 
    } 
} 

回答

1

你可以這樣做:

Intent intentBundle = getActivity().getIntent(); 
     String question_cat = intentBundle.getStringExtra("question_cat"); 
     Log.i("Result : ", question_cat); 

你得到的值作爲字符串即便以後,你把它作爲一個int值後是這樣的:

int j = Integer.valueOf(question_cat); 

    Log.i("Result : ", String.valueOf(j)); 

對於你的另一個問題,使用getIntent(),問題是你在片段類中使用它,爲了使用它,你必須使用getActivity()來訪問它。如果這只是一個正常的活動,那並不複雜。如果一些概念清晰,Android真的很有趣。:)

+1

沒有一本書似乎真的能從零開始工作。很難學習正常活動的所有概念,然後用碎片進行工作。 – Philip

+1

真的..即使開發者的網站有點混亂。每當我懷疑我看到這個網站上的帖子,或者我更願意尋找:http://www.vogella.com/tutorials/android.html 反正,這個答案是否適合你? – mike20132013

+1

它,工作得很好。儘管我在這裏獲得解決方案並不是很滿意,但大部分都不瞭解。現在我需要找出如何創建一個標籤欄。我不期待它 – Philip