2012-10-13 59 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