2016-11-27 118 views
0

我创建了一个TabLayout与Android中的一个MainActivity和两个片段。如何调用片段另一个片段功能

但是,如何获得第二个Fragment来接收按钮事件并调用第一个Fragment函数?

我试过以下,但它不工作,我问。

public interface CustomSearchPOI { 
    void requestSearch(String query); 
} 

@Override 
public void onAttach(Context context) { 
    super.onAttach(context); 
    try { 
     customSearchPOI = (CustomSearchPOI)context; 
    } catch (ClassCastException ex) { 
     throw new ClassCastException(context.toString() + "must implement CustomSearchPOI"); 
    } 
} 

第二块碎片代码

if(info_cursor != null) { 
     info_cursor.moveToFirst(); 
     while(!info_cursor.isAfterLast()) { 
      if(info_cursor.getInt(0) == id) { 
       out_subject.setText(info_cursor.getString(1)); 
       out_college.setText(info_cursor.getString(2)); 
       out_classroom.setText(info_cursor.getString(3)); 
       break; 
      } 
      info_cursor.moveToNext(); 
     } 
    } 
    choice_dialog.setPositiveButton(getString(R.string.Location_navigate), new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialogInterface, int i) { 
      TabsFragment.viewPager.setCurrentItem(0); 
      customSearchPOI.requestSearch(out_college.getText().toString()); 
     } 
    }); 
    choice_dialog.setNegativeButton(getString(R.string.tt_modify), new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialogInterface, int i) { 
      update_timetable_dialog(id); 
     } 
    }); 
    choice_dialog.show(); 

调用功能码

@Override 
public void requestSearch(String query) { 
    String FRAGMENT_TAG = "LOCATION_TAG"; 
    Fragment fragment = new MapFragment(); 
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); 
    fragmentTransaction.add(fragment, FRAGMENT_TAG).commitAllowingStateLoss(); 
    try { 
     ((MapFragment)getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG)).searchPOI(query); 
    } catch(NullPointerException ex) { 
     ex.printStackTrace(); 
     Log.d("[TAG]", fragment.getTag()); 
    } 
} 

MainActivity代码

public void searchPOI(String query) { 
    query = Searchcheck(query); 
    TMapData data = new TMapData(); 
    if(!TextUtils.isEmpty(query)) { 
     data.findAllPOI(query, new TMapData.FindAllPOIListenerCallback() { 
      @Override 
      public void onFindAllPOI(final ArrayList<TMapPOIItem> arrayList) { 
       getActivity().runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
         tMapView.removeAllMarkerItem(); 
         for (TMapPOIItem poi : arrayList) { 
          addMarker(poi); 
         } 
         if(arrayList.size() > 0) { 
          TMapPOIItem poi = arrayList.get(0); 
          moveMap(poi.getPOIPoint().getLatitude(), poi.getPOIPoint().getLongitude()); 
          if(poi.getPOIPoint().getLatitude() > 36.832311 && poi.getPOIPoint().getLongitude() < 127.165038) { 
           Snackbar.make(coordinatorLayout, getString(R.string.No_place), Snackbar.LENGTH_LONG).show(); 
           if(location_me.getVisibility() != View.INVISIBLE) { 
            location_me.hide(); 
           } 
           return; 
          } 
          dst = new TMapPoint(poi.getPOIPoint().getLatitude(), poi.getPOIPoint().getLongitude()); 
          searchRoute(src, dst); 
         } 
        } 
       }); 
      } 
     }); 
    } 
} 

第一块碎片代码(函数调用)

根据谷歌开发文档,片段和片段之间的直接通信是不可能的。

因此,当我尝试通过活动从Fragment调用Fragment函数时,出现NullPointerException错误。

我使用try catch语法来查看标记名称中是否有错误或查询中有错误,但它们都不是问题。

+0

为什么不能在一个片段创建一个接口,而在另一个实施呢? – BiGGZ

+0

你是否还需要实现第二个片段? 但是,如何移动TabLayout中的ViewPager? –

+0

更好,因为两个碎片都在同一个活动中,为什么不把你的方法放在那里呢? – BiGGZ

回答

1

FragmentX:

public class MyFragment extends Fragment{ 

    interface MyInterface{ 

     void doSomething(); 
    } 

    public void someMethodInYourFragment(){ 
    ((MyInterface)getContext()).doSomething();//throws ClassCastException if not implemented in Activity 
    } 

} 

X类:

public class MyActivity extends Activity implements MyFragment.MyInterface{ 

    @Override 
    public void doSomething(){ 
    //do stuff here 
    } 
} 
+0

我修复了问题栏中的代码。 这是正确的代码?如果你这样做,你会得到一个NullPointerException错误。 –

+0

我不关注...所以你的代码现在工作? – BiGGZ

+0

仍是NullPointerException错误.. –