2015-12-05 53 views
1

在我的Activity A中,它有一個listView。當點擊listView時,它會將ID傳遞給活動B,活動B使用適配器顯示與所選標籤相對應的數據。嘗試在空對象引用上調用虛擬方法'void android.app.Fragment.setArguments(android.os.Bundle)'

活動A

listViewEdit.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> listView, View view, 
            int position, long id) { 
       // Get the cursor, positioned to the corresponding listview_item_row in the result set 
       Cursor cursor = (Cursor) listView.getItemAtPosition(position); 

       // Get the state's capital from this listview_item_row in the database. 
       ID = 
         cursor.getLong(cursor.getColumnIndexOrThrow("_id")); 
       Intent intent=new Intent(getApplicationContext(),ActivityB.class); 
       intent.putExtra("ID",ID); 
       startActivity(intent); 


      } 
     }); 
    } 

活動B

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_b); 
     id=getIntent().getExtras().getString("ID"); 
     viewPager = (ViewPager) findViewById(R.id.viewPager); 
     tabsAdapter = new TabsFragmentPagerAdapter(getSupportFragmentManager()); 
     viewPager.setAdapter(tabsAdapter); 
     actionBar = getSupportActionBar(); 
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 
     for(int i=0; i<3; i++){ 
      actionBar.addTab(actionBar.newTab().setText(item[i]).setTabListener(this)); 
     } 
     viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { 

      @Override 
      public void onPageSelected(int arg) { 
       // TODO Auto-generated method stub 
       actionBar.setSelectedNavigationItem(arg); 
      } 

      @Override 
      public void onPageScrolled(int arg0, float arg1, int arg2) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void onPageScrollStateChanged(int arg0) { 
       // TODO Auto-generated method stub 

      } 
     }); 

    } 

TabsFragmentPagerAdapter

@Override 
    public Fragment getItem(int index) { 
     // TODO Auto-generated method stub 
     Fragment fragment=null; 

     if(index == 0) { 

      Bundle bundle = new Bundle(); 
      bundle.putLong("ID", Edit.ID); 
      Log.e("ID", Edit.ID+""); 
      fragment.setArguments(bundle); 
      return new EditInformation(); 

     } 

     if(index == 1) 
      return new Edit_WorkForce(); 
     if(index == 2) 
      return new Edit_WorkDetails(); 


     return null; 
    } 

EditInformation

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View edit_info = inflater.inflate(R.layout.edit_information, container, false); 
     Name=(EditText)edit_info.findViewById(R.id.Edit_name); 
     Status=(EditText)edit_info.findViewById(R.id.Edit_status); 
     Weather=(EditText)edit_info.findViewById(R.id.Edit_weather); 
     Date=(EditText)edit_info.findViewById(R.id.Edit_date); 
     ts = new com.example.project.myapplication.API.InfoAPI(getActivity()); 
     Bundle bundle = this.getArguments(); 
     if(getArguments()!=null) 
     { 
      ID=bundle.getLong("ID"); 
     } 
     RetrieveInformation(ID); 

     return edit_info; 
    } 

當我點擊活動A列表,應用程序崩潰,並顯示如下錯誤。

Process: com.example.project.myapplication, PID: 3103 
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.Fragment.setArguments(android.os.Bundle)' on a null object reference 
      at com.example.project.myapplication.Adapter.TabsFragmentPagerAdapter.getItem(TabsFragmentPagerAdapter.java:35) 
      at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:97) 
      at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:870) 

我可以從活動A的ID,這就是錯誤指fragment.setArguments(bundle);

+0

查看適配器中的getItem()方法。你設置了'Fragment fragment = null;',然後'if(index == 0)',你試圖在一個空Fragment上設置參數。 –

+0

@MikeM。我如何設置參數? – John

+0

在設置參數之前設置'fragment = new EditInformation();'然後'return fragment;'。 –

回答

0

在你TabsFragmentPagerAdapter行,更改

Fragment fragment=null; 
if(index == 0) { 
    Bundle bundle = new Bundle(); 
    bundle.putLong("ID", Edit.ID); 
    Log.e("ID", Edit.ID+""); 
    fragment.setArguments(bundle); 
    return new EditInformation(); 
} 
... 

@Override 
public Fragment getItem(int index) { 
    // TODO Auto-generated method stub 
    Fragment fragment=null; 
    if(index == 0) { 
     fragment = new EditInformation(); 
     Bundle bundle = new Bundle(); 
     bundle.putLong("ID", Edit.ID); 
     Log.e("ID", Edit.ID+""); 
     fragment.setArguments(bundle); 
     return fragment; 
    } 
    ... 
} 

您需要在設置其參數之前初始化EditInformation片段。

+0

我應該刪除'返回新的EditInformation();'? – John

+0

我會更新我的答案。 –

+1

不客氣:) –

相關問題