为什么findFragmentById()返回null?

时间:2012-07-19 10:55:43

标签: android android-fragments android-fragmentactivity

在下面的方法中,NasaDailyFragment在调试器中始终为null。该方法调用NasaDailyFragment.onRefresh();由于这个原因失败了。我无法弄清楚为什么它为null。

public class NasaAppActivity extends FragmentActivity {
//other methods
   public void onRefreshClicked(View view){
       Log.d("debug", "in run");
       FragmentManager fragmentManager=getSupportFragmentManager();
       NasaDailyImage NasaDailyFragment=(NasaDailyImage)fragmentManager.findFragmentById(R.id.fragment_iotd);

       if(NasaDailyFragment!=null){
       Log.d("debug", "fragment has been casted and is NOT null!!!");
       }

       NasaDailyFragment.onRefresh();//always crashes here because NasaDailyFragment is null, this is "line 40" of the code

   }
   }

这是由R.id.fragment_iotd

定义的片段的xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <fragment android:name="com.wajumbie.nasadailyimage.NasaDailyImage"
        android:id="@+id/fragment_iotd"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        />
</LinearLayout>

logcat输出

07-19 11:21:25.916: D/debug(534): in run
07-19 11:21:25.916: D/AndroidRuntime(534): Shutting down VM
07-19 11:21:25.916: W/dalvikvm(534): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
07-19 11:21:25.936: E/AndroidRuntime(534): FATAL EXCEPTION: main
07-19 11:21:25.936: E/AndroidRuntime(534): java.lang.IllegalStateException: Could not execute method of the activity
07-19 11:21:25.936: E/AndroidRuntime(534):  at android.view.View$1.onClick(View.java:3044)
07-19 11:21:25.936: E/AndroidRuntime(534):  at android.view.View.performClick(View.java:3511)
07-19 11:21:25.936: E/AndroidRuntime(534):  at android.view.View$PerformClick.run(View.java:14105)
07-19 11:21:25.936: E/AndroidRuntime(534):  at android.os.Handler.handleCallback(Handler.java:605)
07-19 11:21:25.936: E/AndroidRuntime(534):  at android.os.Handler.dispatchMessage(Handler.java:92)
07-19 11:21:25.936: E/AndroidRuntime(534):  at android.os.Looper.loop(Looper.java:137)
07-19 11:21:25.936: E/AndroidRuntime(534):  at android.app.ActivityThread.main(ActivityThread.java:4424)
07-19 11:21:25.936: E/AndroidRuntime(534):  at java.lang.reflect.Method.invokeNative(Native Method)
07-19 11:21:25.936: E/AndroidRuntime(534):  at java.lang.reflect.Method.invoke(Method.java:511)
07-19 11:21:25.936: E/AndroidRuntime(534):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-19 11:21:25.936: E/AndroidRuntime(534):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-19 11:21:25.936: E/AndroidRuntime(534):  at dalvik.system.NativeStart.main(Native Method)
07-19 11:21:25.936: E/AndroidRuntime(534): Caused by: java.lang.reflect.InvocationTargetException
07-19 11:21:25.936: E/AndroidRuntime(534):  at java.lang.reflect.Method.invokeNative(Native Method)
07-19 11:21:25.936: E/AndroidRuntime(534):  at java.lang.reflect.Method.invoke(Method.java:511)
07-19 11:21:25.936: E/AndroidRuntime(534):  at android.view.View$1.onClick(View.java:3039)
07-19 11:21:25.936: E/AndroidRuntime(534):  ... 11 more
07-19 11:21:25.936: E/AndroidRuntime(534): Caused by: java.lang.NullPointerException
07-19 11:21:25.936: E/AndroidRuntime(534):  at com.wajumbie.nasadailyimage.NasaAppActivity.onRefreshClicked(NasaAppActivity.java:40)
07-19 11:21:25.936: E/AndroidRuntime(534):  ... 14 more
07-19 11:21:26.556: I/dalvikvm(534): threadid=3: reacting to signal 3
07-19 11:21:26.586: I/dalvikvm(534): Wrote stack traces to '/data/anr/traces.txt'

0 个答案:

没有答案