user3200451
user3200451

Reputation: 19

Finding a textView from XML of a Custom View for a AlertDialog Fragment

I have a custom class that extends a dialog fragment that I will later be putting in an activity, however no matter what I do I cannot find the Textview associated with the XML used to create the custom AlertDialog.

EDIT: Mike was right, the onCreateDialog(Bundle ...) is not called before I try to access the textViews, thus leading to the null pointer exception, I cannot, however for the life of me figure out how to create the dialog then try and access the fields, I have attempted to place the code that finds the views in the onAttach(...) method of a fragment because it is the earliest in the lifecycle, however the code the always calls the getter (to get the textView instance in the fragments first and not onCreateDialog or onAttach, is there anyway to ENSURE that the fragment's onCreateDialog or onAttach is called before the getters?

This is in the activity that that will host the fragment win_frag is an already instantiated instance of the custom fragment, appendStreak is the getter to attempt to get the textView instances from the fragment.

mainPanel.getThread().onPause();
            win_frag.show(fragmentManager, "winning");          

            // Set the text in the views accordingly by calling a method from
            // the fragment
            Log.d(TAG, "Attempting to append to the fragment textView");
            win_frag.appendStreak(pongApplication.getStreak());
            win_frag.appendBonus(pongApplication.getBonus());
            win_frag.appendTotal(total);





@Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder build = new AlertDialog.Builder(getActivity());
// Get the layout inflater (needed to put in custom XML)
LayoutInflater inflater = getActivity().getLayoutInflater();
View inflatedView = inflater.inflate(R.layout.win_screen, null, false);
build.setView(inflatedView);
streakView= (TextView)inflatedView.findViewById(R.id.streak_box);
bonusView= (TextView)inflatedView.findViewById(R.id.bonus_box);
totalView= (TextView)inflatedView.findViewById(R.id.total_box);
}

win_screen.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
   android:background="@drawable/metal_background">

    <TextView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:height="50dp"

        android:textColor="@android:color/white"
        android:text="@string/win_title"
        android:textSize="30sp"
        android:typeface="monospace"

        />   
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:height="50dp"
        android:gravity="left|center"
        android:text="@string/streak_title"
         android:textSize="20sp"
           android:textColor="#ffffff"
         android:id="@+id/streak_box"
         /> 

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:height="50dp"
        android:gravity="left|center"
        android:text="@string/bonus_title"
         android:textSize="20sp"
           android:textColor="#ffffff"
         android:id="@+id/bonus_box"
         /> 

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:height="50dp"
        android:gravity="left|center"
        android:text="@string/total_title"
         android:textSize="20sp"
         android:textColor="#ffffff"
         android:id="@+id/total_box"
         /> 


</LinearLayout>

Upvotes: 0

Views: 342

Answers (2)

Ved
Ved

Reputation: 1077

Try this

@Override    
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder build = new AlertDialog.Builder(getActivity()); 
// Get the layout inflater (needed to put in custom XML)
LayoutInflater inflater = getActivity().getLayoutInflater();
View inflatedView = inflater.inflate(R.layout.win_screen, null);
build.setView(inflatedView);
streakView= (TextView) inflatedView.findViewById(R.id.streak_box);
bonusView= (TextView) inflatedView.findViewById(R.id.bonus_box);
totalView= (TextView) inflatedView.findViewById(R.id.total_box);
return builder.create();
}

Upvotes: 1

Michael Shrestha
Michael Shrestha

Reputation: 2555

try changing

inflater.inflate(R.layout.win_screen, null, false);

to

inflater.inflate(R.layout.win_screen, null);

Upvotes: 0

Related Questions