我正在开发一个在主页中包含3个标签的应用程序。
第一个选项卡(Fragment类)有Async任务方法,我在父类(Tab Host Parent类)中有一个操作栏,当我点击那个动作v = bar菜单项我想调用那个Async Task方法时我可以称之为这种方法
我这样称呼它的错误
((new first_Fragment()))。new Load_Task.execute();
错误就像这样
04-10 19:35:48.768: E/AndroidRuntime(9716): FATAL EXCEPTION: main
04-10 19:35:48.768: E/AndroidRuntime(9716): Process: com.example.com, PID: 9716
04-10 19:35:48.768: E/AndroidRuntime(9716): java.lang.NullPointerException
04-10 19:35:48.768: E/AndroidRuntime(9716): at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:143)
04-10 19:35:48.768: E/AndroidRuntime(9716): at android.app.AlertDialog.<init>(AlertDialog.java:98)
04-10 19:35:48.768: E/AndroidRuntime(9716): at android.app.ProgressDialog.<init>(ProgressDialog.java:77)
04-10 19:35:48.768: E/AndroidRuntime(9716): at com.example.com$Load_Task.onPreExecute(Bol_Contacts_Screen.java:396)
04-10 19:35:48.768: E/AndroidRuntime(9716): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
04-10 19:35:48.768: E/AndroidRuntime(9716): at android.os.AsyncTask.execute(AsyncTask.java:535)
04-10 19:35:48.768: E/AndroidRuntime(9716): at com.example.com.Fragment_Class.onOptionsItemSelected(Fragment_Class.java:256)
04-10 19:35:48.768: E/AndroidRuntime(9716): at android.app.Activity.onMenuItemSelected(Activity.java:2617)
04-10 19:35:48.768: E/AndroidRuntime(9716): at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:372)
04-10 19:35:48.768: E/AndroidRuntime(9716): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1012)
04-10 19:35:48.768: E/AndroidRuntime(9716): at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:741)
04-10 19:35:48.768: E/AndroidRuntime(9716): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
04-10 19:35:48.768: E/AndroidRuntime(9716): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:884)
04-10 19:35:48.768: E/AndroidRuntime(9716): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
04-10 19:35:48.768: E/AndroidRuntime(9716): at com.android.internal.view.menu.MenuPopupHelper.onItemClick(MenuPopupHelper.java:177)
04-10 19:35:48.768: E/AndroidRuntime(9716): at android.widget.AdapterView.performItemClick(AdapterView.java:299)
04-10 19:35:48.768: E/AndroidRuntime(9716): at android.widget.AbsListView.performItemClick(AbsListView.java:1129)
04-10 19:35:48.768: E/AndroidRuntime(9716): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2927)
04-10 19:35:48.768: E/AndroidRuntime(9716): at android.widget.AbsListView$3.run(AbsListView.java:3676)
04-10 19:35:48.768: E/AndroidRuntime(9716): at android.os.Handler.handleCallback(Handler.java:733)
04-10 19:35:48.768: E/AndroidRuntime(9716): at android.os.Handler.dispatchMessage(Handler.java:95)
04-10 19:35:48.768: E/AndroidRuntime(9716): at android.os.Looper.loop(Looper.java:136)
04-10 19:35:48.768: E/AndroidRuntime(9716): at android.app.ActivityThread.main(ActivityThread.java:5050)
04-10 19:35:48.768: E/AndroidRuntime(9716): at java.lang.reflect.Method.invokeNative(Native Method)
04-10 19:35:48.768: E/AndroidRuntime(9716): at java.lang.reflect.Method.invoke(Method.java:515)
04-10 19:35:48.768: E/AndroidRuntime(9716): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:806)
04-10 19:35:48.768: E/AndroidRuntime(9716): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
04-10 19:35:48.768: E/AndroidRuntime(9716): at dalvik.system.NativeStart.main(Native Method)
感谢您的帮助
答案 0 :(得分:2)
您必须在父活动中使用过类似的内容,以便用特定的子片段替换容器:
Fragment fragmentChild = new FragmentChild();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.container, fragmentChild, "fragChild" ).commit();
现在你只需要编写类似这样的代码来调用子类方法:
Fragment fragment (FragmentChild)
getChildFragmentManager().findFragmentByTag("fragChild");
fragment.<your_async_method>();