我有一些Slidenerd的Scrolltab視頻系列的示例代碼,這個工作非常好。但是,我無法從父活動的片段中調用方法。例如,我在片段A中包含一個名爲doSomething()的方法。我想從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);
}
}
您是否也在實施BroadCart接收器,然後請讓我看看您的廣播接收器部分清單。 –
是的,我是...本質上我試圖運行FragmentReadCSV中的方法後,收到一個意圖,說USB端口準備好了 –
請註冊您的接收器在清單中,我給了一個樣本。請參閱我的編輯。 –