我需要在onResume活动中调用片段内的方法,但始终显示FC并出现此错误:
06-15 14:59:58.496: E/AndroidRuntime(28904): FATAL EXCEPTION: main
06-15 14:59:58.496: E/AndroidRuntime(28904): Process: com.carlosdiaz.tfm_usuarios, PID: 28904
06-15 14:59:58.496: E/AndroidRuntime(28904): java.lang.RuntimeException: Unable to resume activity {com.carlosdiaz.tfm_usuarios/com.carlosdiaz.tfm_usuarios.Grupos}: java.lang.NullPointerException
06-15 14:59:58.496: E/AndroidRuntime(28904): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2774)
06-15 14:59:58.496: E/AndroidRuntime(28904): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2803)
06-15 14:59:58.496: E/AndroidRuntime(28904): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2238)
06-15 14:59:58.496: E/AndroidRuntime(28904): at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-15 14:59:58.496: E/AndroidRuntime(28904): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-15 14:59:58.496: E/AndroidRuntime(28904): at android.os.Handler.dispatchMessage(Handler.java:102)
06-15 14:59:58.496: E/AndroidRuntime(28904): at android.os.Looper.loop(Looper.java:136)
06-15 14:59:58.496: E/AndroidRuntime(28904): at android.app.ActivityThread.main(ActivityThread.java:5001)
06-15 14:59:58.496: E/AndroidRuntime(28904): at java.lang.reflect.Method.invokeNative(Native Method)
06-15 14:59:58.496: E/AndroidRuntime(28904): at java.lang.reflect.Method.invoke(Method.java:515)
06-15 14:59:58.496: E/AndroidRuntime(28904): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
06-15 14:59:58.496: E/AndroidRuntime(28904): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
06-15 14:59:58.496: E/AndroidRuntime(28904): at dalvik.system.NativeStart.main(Native Method)
06-15 14:59:58.496: E/AndroidRuntime(28904): Caused by: java.lang.NullPointerException
06-15 14:59:58.496: E/AndroidRuntime(28904): at com.carlosdiaz.tfm_usuarios.Grupos.onResume(Grupos.java:52)
06-15 14:59:58.496: E/AndroidRuntime(28904): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1192)
06-15 14:59:58.496: E/AndroidRuntime(28904): at android.app.Activity.performResume(Activity.java:5310)
06-15 14:59:58.496: E/AndroidRuntime(28904): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2764)
06-15 14:59:58.496: E/AndroidRuntime(28904): ... 12 more
我不知道我能做些什么。
这是mi OnCreate& onResume方法:
public class Grupos extends Activity {
PlaceholderFragment fragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grupos);
fragment = (PlaceholderFragment) getFragmentManager().findFragmentById(R.layout.fragment_grupos);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
protected void onResume()
{
super.onResume();
Log.i("Test","Test");
fragment.myOnResume();
}
...
我的方法在Fragment类中:
public static class PlaceholderFragment extends Fragment {
...
public void myOnResume() {
GruposTask getGrupos = new GruposTask(PlaceholderFragment.this, getActivity());
getGrupos.execute(id);
}
...
提前谢谢你!
修改
现在我试图在活动xml中创建片段,以便稍后从oncreate()调用它:
PlaceholderFragment mainFragment = (PlaceholderFragment)getFragmentManager().findFragmentById(R.id.listGroup);
这是活动布局:
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.carlosdiaz.tfm_usuarios.Grupos"
tools:ignore="MergeRootFrame">
<fragment class="com.carlosdiaz.tfm_usuarios.Grupos.PlaceholderFragment"
android:id="@+id/listGroup"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
</FrameLayout>
但是现在我有这个错误:
06-15 19:20:15.956: E/AndroidRuntime(7424): FATAL EXCEPTION: main
06-15 19:20:15.956: E/AndroidRuntime(7424): Process: com.carlosdiaz.tfm_usuarios, PID: 7424
06-15 19:20:15.956: E/AndroidRuntime(7424): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.carlosdiaz.tfm_usuarios/com.carlosdiaz.tfm_usuarios.Grupos}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
06-15 19:20:15.956: E/AndroidRuntime(7424): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
06-15 19:20:15.956: E/AndroidRuntime(7424): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
06-15 19:20:15.956: E/AndroidRuntime(7424): at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-15 19:20:15.956: E/AndroidRuntime(7424): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-15 19:20:15.956: E/AndroidRuntime(7424): at android.os.Handler.dispatchMessage(Handler.java:102)
06-15 19:20:15.956: E/AndroidRuntime(7424): at android.os.Looper.loop(Looper.java:136)
06-15 19:20:15.956: E/AndroidRuntime(7424): at android.app.ActivityThread.main(ActivityThread.java:5001)
06-15 19:20:15.956: E/AndroidRuntime(7424): at java.lang.reflect.Method.invokeNative(Native Method)
06-15 19:20:15.956: E/AndroidRuntime(7424): at java.lang.reflect.Method.invoke(Method.java:515)
06-15 19:20:15.956: E/AndroidRuntime(7424): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
06-15 19:20:15.956: E/AndroidRuntime(7424): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
06-15 19:20:15.956: E/AndroidRuntime(7424): at dalvik.system.NativeStart.main(Native Method)
06-15 19:20:15.956: E/AndroidRuntime(7424): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
06-15 19:20:15.956: E/AndroidRuntime(7424): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
06-15 19:20:15.956: E/AndroidRuntime(7424): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
06-15 19:20:15.956: E/AndroidRuntime(7424): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
06-15 19:20:15.956: E/AndroidRuntime(7424): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
06-15 19:20:15.956: E/AndroidRuntime(7424): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
06-15 19:20:15.956: E/AndroidRuntime(7424): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
06-15 19:20:15.956: E/AndroidRuntime(7424): at android.app.Activity.setContentView(Activity.java:1929)
06-15 19:20:15.956: E/AndroidRuntime(7424): at com.carlosdiaz.tfm_usuarios.Grupos.onCreate(Grupos.java:39)
06-15 19:20:15.956: E/AndroidRuntime(7424): at android.app.Activity.performCreate(Activity.java:5231)
06-15 19:20:15.956: E/AndroidRuntime(7424): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-15 19:20:15.956: E/AndroidRuntime(7424): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
06-15 19:20:15.956: E/AndroidRuntime(7424): ... 11 more
06-15 19:20:15.956: E/AndroidRuntime(7424): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.carlosdiaz.tfm_usuarios.Grupos.PlaceholderFragment: make sure class name exists, is public, and has an empty constructor that is public
06-15 19:20:15.956: E/AndroidRuntime(7424): at android.app.Fragment.instantiate(Fragment.java:597)
06-15 19:20:15.956: E/AndroidRuntime(7424): at android.app.Fragment.instantiate(Fragment.java:561)
06-15 19:20:15.956: E/AndroidRuntime(7424): at android.app.Activity.onCreateView(Activity.java:4778)
06-15 19:20:15.956: E/AndroidRuntime(7424): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
06-15 19:20:15.956: E/AndroidRuntime(7424): ... 21 more
06-15 19:20:15.956: E/AndroidRuntime(7424): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.carlosdiaz.tfm_usuarios.Grupos.PlaceholderFragment" on path: DexPathList[[zip file "/data/app/com.carlosdiaz.tfm_usuarios-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.carlosdiaz.tfm_usuarios-2, /vendor/lib, /system/lib]]
06-15 19:20:15.956: E/AndroidRuntime(7424): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
06-15 19:20:15.956: E/AndroidRuntime(7424): at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
06-15 19:20:15.956: E/AndroidRuntime(7424): at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
06-15 19:20:15.956: E/AndroidRuntime(7424): at android.app.Fragment.instantiate(Fragment.java:583)
06-15 19:20:15.956: E/AndroidRuntime(7424): ... 24 more
答案 0 :(得分:3)
那是因为你的订单错了。
PlaceholderFragment fragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grupos);
if(savedInstanceState == null)
{
fragment = new PlaceHolderFragment();
fragment.setTag(R.id.myfragmentId);
getFragmentManager().beginTransaction()
.add(R.id.container, fragment).commit();
}
else
{
if(fragment == null)
{
fragment = (PlaceholderFragment) getFragmentManager().findFragmentByTag(R.id.myfragmentId);
}
}
}
答案 1 :(得分:2)
如果您的 父 的活动可以使用此代码:
final PlaceholderFragment pf = new PlaceholderFragment();
pf.your_method_name();
这段代码对我有用
答案 2 :(得分:1)
我认为您可以尝试像FragmentActivity一样 - 公共类Grupos扩展FragmentActivity 因为要使用基于支持的Fragment和Loader API的活动的Base类。 (android.support.v4.app.FragmentActivity)
答案 3 :(得分:1)
尝试使用FragmentActivity而不是Activity。
然后你应该调用getSupportFragmentManager而不是getFragmentManager
此外,您无法将布局传递给findFragmentById方法。你应该给它一个片段ID(R.id.FRAGMENT_ID)。
尝试像这样:
public class Grupos extends FragmentActivity {
PlaceholderFragment fragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grupos);
fragment = new PlaceholderFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.container, fragment).commit();
}
@Override
protected void onResume()
{
super.onResume();
Log.i("Test","Test");
fragment.myOnResume();
}