getSupportFragmentManager()。getFragments()有效,但不是findFragmentByTag(“name”);

时间:2017-05-25 17:05:32

标签: android user-interface null fragment

我只是试图在主要活动的片段中运行一个方法。 getFragments()正确列出了我的应用程序中的所有片段。但是,当调用findFragmentByTag()时,结果始终为null!我环顾四周,没有任何解决方案似乎有所帮助。

这是我调用片段管理器的地方

fm.executePendingTransactions();
Log.d("Fragmento", fm.getFragments().toString());
Log.d("Fragmento", fm.findFragmentByTag("frag3").toString());

其中fm是

`FragmentManager fm = getSupportFragmentManager();`

我的片段代码是

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;



public class Fragment3 extends Fragment {
    TextView  status;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment3, container, false);
    Log.d("InFragment", this.getClass().toString());
    return v;
}

public static Fragment3 newInstance() {
    Fragment3 f = new Fragment3();
    return f;
}

    public void setStatus(String msg){
        status = (TextView)getView().findViewById(R.id.canLog);
        status.setText(status.getText()+"\n"+msg);
    }

}

我的布局非常简单,我已经在片段的布局中定义了ID和标签。

`<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:tag="frag3"
    android:id="@+id/fragment3">
</LinearLayout>`

最后我的错误日志

05-25 16:56:54.949 13263-13263/com.archronix.infotainment E/AndroidRuntime: 
FATAL EXCEPTION: main

Process: com.archronix.infotainment, PID: 13263

java.lang.IllegalStateException: Could not execute method for 
android:onClick

at android.view.View$DeclaredOnClickListener.onClick(View.java:4458)

at android.view.View.performClick(View.java:5204)

at android.view.View$PerformClick.run(View.java:21155)

at android.os.Handler.handleCallback(Handler.java:739)

at android.os.Handler.dispatchMessage(Handler.java:95)

at android.os.Looper.loop(Looper.java:148)

at android.app.ActivityThread.main(ActivityThread.java:5422)

at java.lang.reflect.Method.invoke(Native Method)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:726)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Caused by: java.lang.reflect.InvocationTargetException

at java.lang.reflect.Method.invoke(Native Method)

at android.view.View$DeclaredOnClickListener.onClick(View.java:4453)

at android.view.View.performClick(View.java:5204) 

at android.view.View$PerformClick.run(View.java:21155) 

at android.os.Handler.handleCallback(Handler.java:739) 

at android.os.Handler.dispatchMessage(Handler.java:95) 

at android.os.Looper.loop(Looper.java:148) 

at android.app.ActivityThread.main(ActivityThread.java:5422) 

at java.lang.reflect.Method.invoke(Native Method) 

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:726) 

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 
'java.lang.String android.support.v4.app.Fragment.toString()' on a null 
object reference

at 
com.archronix.infotainment.MainActivity.buttonClicked(MainActivity.java:445)

at java.lang.reflect.Method.invoke(Native Method) 

at android.view.View$DeclaredOnClickListener.onClick(View.java:4453) 

at android.view.View.performClick(View.java:5204) 

at android.view.View$PerformClick.run(View.java:21155) 

at android.os.Handler.handleCallback(Handler.java:739) 

at android.os.Handler.dispatchMessage(Handler.java:95) 

at android.os.Looper.loop(Looper.java:148) 

at android.app.ActivityThread.main(ActivityThread.java:5422) 

at java.lang.reflect.Method.invoke(Native Method) 

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:726) 

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

这可能是一个非常愚蠢的问题,但片段似乎完全让我感到困惑。

1 个答案:

答案 0 :(得分:0)

<强>问题: 在LinearLayout中设置标签不起作用,Fragmentmangager不会找到它

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:tag="frag3"           <-- not good
    android:id="@+id/fragment3">

`

<强>解决方案:

如果你在xml中定义片段:

 <fragment android:name="com.example.YourFragment"
        android:id="@+id/frag3Id"
        android:layout_width="match_parent"
        android:tag="frag3"
        android:layout_height="match_parent" />

你可以找到它:

fm.findFragmentByTag("frag3")

 fm.findFragmentById(R.id.frag3Id)

或者您可以使用FragmentTransaction的addreplace函数设置标记