我只是尝试在我的主要活动的片段中运行一个方法。 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)
这可能是一个非常愚蠢的问题,但碎片似乎完全让我感到困惑。
问题:在线性布局中设置标签不起作用,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)
或者您可以使用片段事务的添加和替换功能设置标签