2012-10-13 53 views
4

當我試圖從FragmentActivity傳遞參數到片段時,它給了我在片段中的getArguments()中的空指針異常。從Android FragmentActivity傳遞參數到片段

這是我FragmentActivity代碼

public class IndexChartActivity extends FragmentActivity { 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 

      setContentView(R.layout.index_chart); 

      IndexFragmentActivity indexFragment = 
(IndexFragmentActivity)getSupportFragmentManager().findFragmentById(R.id.index_fragment); 
      indexFragment.newInstance("ASPI"); 

     } 

    } 

這裏是index_chart.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#FFFFFF" 
    android:orientation="vertical" > 

    <fragment 
     android:id="@+id/header_fragment" 
     android:name="com.lk.ignitionit.cse.util.HeaderFragmentActivity" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

    <fragment 
     android:id="@+id/index_fragment" 
     android:name="com.lk.ignitionit.cse.util.IndexFragmentActivity" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

    <fragment 
     android:name="com.lk.ignitionit.cse.util.ChartFragmentActivity" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

</LinearLayout> 

這裏是片段

public class IndexFragmentActivity extends Fragment { 
    protected ImageView ivASPI; 
    protected ImageView ivMPI; 
    protected ImageView ivSP; 
    protected TextView tvMain; 
    protected TextView tvTop; 
    protected TextView tvBottom; 
    String response = null; 
    String result = null; 
    String [] resultArr = null; 
    Bundle b = new Bundle(); 
    String indexType = null; 
    int layout; 
    IndexFragmentActivity f = null; 

    public IndexFragmentActivity newInstance(String index) { 
     f = new IndexFragmentActivity(); 
     Bundle args = new Bundle(); 
     args.putString("indextype", index); 
     f.setArguments(args); 
     return f; 
    } 

    public String getSelectedIndex() { 
     return f.getArguments().getString("indextype"); 
    } 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     if(getSelectedIndex().equals("ASPI")){ 
      layout = R.layout.aspi_header; 
     }else if(getSelectedIndex().equals("MPI")){ 
      layout = R.layout.mpi_header; 
     }else{ 
      layout = R.layout.sp_header; 
     } 
     View view = inflater.inflate(layout, container, false); 

     tvMain = (TextView) view.findViewById(R.id.tv_main); 
     tvTop = (TextView) view.findViewById(R.id.tv_top); 
     tvBottom = (TextView) view.findViewById(R.id.tv_bottom); 


     new ServiceAccess().execute(""); 

     tvMain.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       b.putString("type", "S&P SL20"); 
       Activity activity = getActivity(); 
       Intent intent = new Intent(activity, IndexChartActivity.class); 
       intent.putExtras(b); 
       startActivity(intent); 

      } 
     }); 

     return view; 
    } 
} 

,這裏是我的錯誤

Caused by: java.lang.NullPointerException 
at com.lk.ignitionit.cse.util.IndexFragmentActivity.getSelectedIndex(IndexFragmentActivity.java:69) 

我介紹了很多與此相關的stackoverflow問題,並試着給出http://developer.android.com/guide/components/fragments.html的示例代碼。但仍沒有運氣

非常感謝所有反饋這個,因爲這似乎是我不能想出一個很基本的問題..提前

感謝

回答

18
  1. 擺脫擺脫Activity所有不從Activity繼承的類的名稱。例如, IndexFragmentActivity不是Activity

  2. 在您的實際活動的onCreate()中,您致電findFragmentById()檢索片段,然後在該片段上調用newInstance()。但是,片段將不會在第一次調用onCreate()時存在,並且您將在此處失敗,並顯示NullPointerException。請正確處理這種情況。

  3. Java中的方法名newInstance最常與factory pattern,其中newInstance()到位的公共構造函數用於創建某些類的實例的方法static相關。您的newInstance()方法不是static。這會讓你後來維護代碼的人感到困惑。

  4. 您在onCreate()中調用newInstance(),創建新的片段實例,然後將其丟棄,這是浪費時間。

因此,假設你原來的片段的實例(無論它是從哪裏來的)沒有一個參數Bundle集,將在getSelectedIndex()解釋你NullPointerException

當進出口試圖從FragmentActivity一個參數傳遞給片段

將參數傳遞到新創建片段,使用staticnewInstance()方法和參數Bundle創建片段是完全合理的。

要將參數傳遞給已存在的片段,只需在該片段上調用一些setter方法即可。

+0

非常感謝您的指導。一旦我解決了問題,我會做工作並更新答案。 – virtualpathum

相關問題