Arin Yaldizciyan
Arin Yaldizciyan

Reputation: 35

getSupportFragmentManager().getFragments() works, but not findFragmentByTag("name");

I'm simply trying to run a method within a fragment from my main activity. getFragments() lists all the fragments in my application correctly. However when calling findFragmentByTag() the result is always null! I've looked around and no solution seems to help.

Here's where I call my fragment manager

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

where fm is

`FragmentManager fm = getSupportFragmentManager();`

My fragment Code is

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);
    }

}

My layout is very simple, and i've defined an ID and a tag within the layout for the fragment.

`<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>`

finally my error log

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)

This is probably a very stupid question however fragments seem to baffle me completely.

Upvotes: 2

Views: 921

Answers (1)

daemmie
daemmie

Reputation: 6460

Problem: Setting the tag in the LinearLayout doesn't work, the Fragmentmangager wont find it

<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">

`

Solution:

If your define the fragment in the xml:

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

you can find it with:

fm.findFragmentByTag("frag3")

or

 fm.findFragmentById(R.id.frag3Id)

Or you can set the tag withe the add and replace functions of the FragmentTransaction

Upvotes: 0

Related Questions