0

我有一些Slidenerd的Scrolltab視頻系列的示例代碼,這個工作非常好。但是,我無法從父活動的片段中調用方法。例如,我在片段A中包含一個名爲doS​​omething()的方法。我想從MainActivity的onResume()回調中調用它。使用ViewPager從父級活動調用片段的方法

編輯: 使用「接受的答案」適用於此應用程序,但不適用於由於nullpointerexception而正在處理的實際應用程序。

錯誤日誌

06-25 13:02:19.308 3862-3862/com.felhr.agweigh D/AndroidRuntime﹕ Shutting down VM 
06-25 13:02:19.308 3862-3862/com.felhr.agweigh W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x416ecda0) 
06-25 13:02:19.308 3862-3862/com.felhr.agweigh E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: com.felhr.agweigh, PID: 3862 
    java.lang.RuntimeException: Error receiving broadcast Intent { act=com.felhr.usbservice.USB_PERMISSION_GRANTED flg=0x10 } in [email protected] 
      at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:782) 
      at android.os.Handler.handleCallback(Handler.java:733) 
      at android.os.Handler.dispatchMessage(Handler.java:95) 
      at android.os.Looper.loop(Looper.java:136) 
      at android.app.ActivityThread.main(ActivityThread.java:5479) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:515) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
      at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.NullPointerException 
      at com.felhr.agweigh.FragmentPlotCSV.createSpinner(FragmentPlotCSV.java:102) 
      at com.felhr.agweigh.MainActivity$3.onReceive(MainActivity.java:894) 
      at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:772) 
            at android.os.Handler.handleCallback(Handler.java:733) 
            at android.os.Handler.dispatchMessage(Handler.java:95) 
            at android.os.Looper.loop(Looper.java:136) 
            at android.app.ActivityThread.main(ActivityThread.java:5479) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:515) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
            at dalvik.system.NativeStart.main(Native Method) 

清單

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.felhr.agweigh" > 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 

    <uses-feature android:name="android.hardware.usb.host" 
     android:required="true" /> 

    <application 
     android:logo="@drawable/rinstrumlogo" 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/RinstrumActionBarTheme" > 
     <activity 
      android:name="com.felhr.agweigh.MainActivity" 
      android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <service 
      android:name="com.felhr.agweigh.UsbService" 
      android:enabled="true" > 
     </service> 

    </application> 

</manifest> 

MainActivity

public class MainActivity extends FragmentActivity { 

ViewPager viewPager = null; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    viewPager = (ViewPager)findViewById(R.id.pager); 
    FragmentManager fragmentManager = getSupportFragmentManager(); 
    viewPager.setAdapter(new MyAdapter(fragmentManager)); 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
} 
} 

MyAdapter

class MyAdapter extends FragmentPagerAdapter 
{ 
public MyAdapter(FragmentManager fm) 
{ 
    super(fm); 
} 

@Override 
public Fragment getItem(int i) { 
    Fragment fragment = null; 
    Log.d("SWIPE","get Item is called"+i); 
    if (i==0) 
    { 
     fragment=new FragmentA(); 
    } 
    if (i==1) 
    { 
     fragment=new FragmentB(); 
    } 
    if (i==2) 
    { 
     fragment=new FragmentC(); 
    } 
    return fragment; 
} 

@Override 
public int getCount() { 
    Log.d("SWIPE","get count is called"); 
    return 3; 
} 

// talks to title to give the page 
@Override 
public CharSequence getPageTitle(int position) { 
    if (position == 0) { 
     return "Tab 1"; 
    } 
    if (position == 1) { 
     return "Tab 2"; 
    } 
    if (position == 2) { 
     return "Tab 3"; 
    } 
    return null; 
} 
} 

FragmentA

/** 
* Created by Luke on 20/04/2015. 
*/ 
public class FragmentA extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.fragment_a, container, false); 
    } 

    public void doSomething(){ 
     // call this method from main activity 
     } 
    } 

FragmentB

/** 
* Created by Luke on 20/04/2015. 
*/ 
public class FragmentB extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.fragment_a, container, false); 
    } 
} 

FragmentC

/** 
* Created by Luke on 20/04/2015. 
*/ 
public class FragmentC extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.fragment_a, container, false); 
    } 
} 

回答

1

你可以不喜歡這個 -

@Override 
    protected void onResume() { 
     super.onResume(); 
     FragmentA _fragA = new FragmentA(); 
     _fragA.doSomething(); 

    } 

您可以在manifes註冊您的接收器通過使用元件噸文件:

 <receiver android:name="MyReceiver" > 
      <intent-filter> 
       <action android:name="com.example.Broadcast" > 
        </action> 
      </intent-filter> 
     </receiver> 

嵌套元件用於指定接收機應進行反應的情況下。

+0

您是否也在實施BroadCart接收器,然後請讓我看看您的廣播接收器部分清單。 –

+0

是的,我是...本質上我試圖運行FragmentReadCSV中的方法後,收到一個意圖,說USB端口準備好了 –

+0

請註冊您的接收器在清單中,我給了一個樣本。請參閱我的編輯。 –

3

只是一個fyi,你的問題不是特定於使用ViewPager。

通過設計,該活動不知道任何關於您的FragmentA,而不是它是一個片段,因此它不知道它包含一個doSomething()方法。快速和骯髒的方法將是從適配器中獲取片段,並在知道它是FragmentA時將其投射到FragmentA。我認爲這是一種「快速而骯髒」的方式,因爲你想讓事情在活動和片斷之間分離 - 即,你的活動不需要知道你的FragmentA實現,只知道Fragment。

當你構建更復雜的應用程序時,你需要考慮使用事件總線以一種很好的分離方式在碎片和活動之間發送消息。這裏有一些流行的Android事件總線: http://square.github.io/otto/ https://github.com/greenrobot/EventBus