user3117785
user3117785

Reputation: 247

Passing data back from DialogFragment to Activity

I have a search field set up in a dialogfragment, then when users click to open initially, allows them to enter a title and hit search. I want this to be able to communicate back to the Activity's listview.

Unfortunately I keep getting a crash when attempting to search. I have it set up using a listener, but I believe it has to do with attempting to read the textView from the MainActivity's layout, even though it's in the DialogFragment'a layout xml?

I attempt to call it by R.id.searchField in the MainActivity which onCreate's setContent's layout XML is different from the one the DialogFragment implements. I get no error when trying to do so , just the fatal crash. Not sure why else I would receive an error, Here is the code, and stack trace below it:

MainActivity:

public class MainActivity extends Activity implements searchListener{
public void searchBooks() 
    {
// Error is in this function, believe it has to do with this textField as it resides in // the DialogFragment's xml? Unless findViewById calls from all XMLS and just not one defined in 
//setContentView for the activity? Not sure though, initially I feel I should have this searchField 
//defined in the fragment class, but then I won't have access to it in this function in MainActivity, 
//or the mResults which is where the data is being used

        EditText searchField = (EditText) findViewById(R.id.searchField);

        if (mResults != null && !searchField.getText().equals(""))
        {

            String inputedText = searchField.getText().toString();
            JSONArray results = new JSONArray();

            for (int i = 0; i < mResults.length(); i++)
            {
                try {
                    JSONObject bookObject = mResults.getJSONObject(i);
                    if (bookObject.get("name").toString().equalsIgnoreCase(inputedText))
                    {
                        results.put(bookObject);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }
    }
public void showDialog() {
    DialogFrag newFragment = DialogFrag.newInstance();
    newFragment.show(getFragmentManager(), "dialog");
}
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Setting the main view
        setContentView(R.layout.activity_main_fragment);
}
}

DialogFragment:

public class DialogFrag extends DialogFragment {
    public static DialogFrag newInstance() {
        return new DialogFrag();
    }
    private searchListener listener;
    public interface searchListener
    {
        public void searchBooks();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            listener = (searchListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + "attaching d fragment failed!");
        }
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.search_dialog_fragment, container, false);

        searchButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                listener.searchBooks();
            }
        });
        return v;
    }
}

StackTrace

01-29 20:10:21.570: E/AndroidRuntime(11260): FATAL EXCEPTION: main
01-29 20:10:21.570: E/AndroidRuntime(11260): Process: com.brentadams.bookList, PID: 11260
01-29 20:10:21.570: E/AndroidRuntime(11260): java.lang.NullPointerException
01-29 20:10:21.570: E/AndroidRuntime(11260):    at com.brentadams.bookList.MainActivity.searchBooks(MainActivity.java:327)
01-29 20:10:21.570: E/AndroidRuntime(11260):    at com.brentadams.bookList.fragments.SearchFragment$1.onClick(SearchFragment.java:48)
01-29 20:10:21.570: E/AndroidRuntime(11260):    at android.view.View.performClick(View.java:4438)
01-29 20:10:21.570: E/AndroidRuntime(11260):    at android.view.View$PerformClick.run(View.java:18422)
01-29 20:10:21.570: E/AndroidRuntime(11260):    at android.os.Handler.handleCallback(Handler.java:733)
01-29 20:10:21.570: E/AndroidRuntime(11260):    at android.os.Handler.dispatchMessage(Handler.java:95)
01-29 20:10:21.570: E/AndroidRuntime(11260):    at android.os.Looper.loop(Looper.java:136)
01-29 20:10:21.570: E/AndroidRuntime(11260):    at android.app.ActivityThread.main(ActivityThread.java:5017)
01-29 20:10:21.570: E/AndroidRuntime(11260):    at java.lang.reflect.Method.invokeNative(Native Method)
01-29 20:10:21.570: E/AndroidRuntime(11260):    at java.lang.reflect.Method.invoke(Method.java:515)
01-29 20:10:21.570: E/AndroidRuntime(11260):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-29 20:10:21.570: E/AndroidRuntime(11260):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-29 20:10:21.570: E/AndroidRuntime(11260):    at dalvik.system.NativeStart.main(Native Method)

EDIT NEW CODE / ERRORS:

Took your advice and have the string passed back into method searchBooks, but now recieving a stupid nullpointerexception on what I think doesn't make any sense. The textview resides in my activity_main_fragment.xml:

public class MainActivity extends Activity implements searchListener{
public void searchBooks(String searchString) 
    { 


        if (mResults != null && searchString != null && !searchString.equals(""))
        {


            JSONArray results = new JSONArray();

            for (int i = 0; i < mResults.length(); i++)
            {
                try {
                    JSONObject bookObject = mResults.getJSONObject(i);
                    if (bookObject.get("name").toString().equalsIgnoreCase(searchString))
                    {
                        results.put(bookObject);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }
    }
public void showDialog() {
    DialogFrag newFragment = DialogFrag.newInstance();
    newFragment.show(getFragmentManager(), "dialog");
}
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Setting the main view
        setContentView(R.layout.activity_main_fragment);
}
}

DialogFragment

public class DialogFrag extends DialogFragment {
    public static DialogFrag newInstance() {
        return new DialogFrag();
    }
    private searchListener listener;
    public interface searchListener
    {
        public void searchBooks(String text);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            listener = (searchListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + "attaching d fragment failed!");
        }
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.search_dialog_fragment, container, false);

        searchButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                EditText searchField = (EditText) v.findViewById(R.id.searchField);
                String input = searchField.getText().toString();
                listener.searchBooks(input);
            }
        });
        return v;
    }
}

Stacktrace

01-29 15:30:31.402: E/AndroidRuntime(777): FATAL EXCEPTION: main
01-29 15:30:31.402: E/AndroidRuntime(777): java.lang.NullPointerException
01-29 15:30:31.402: E/AndroidRuntime(777):  at com.brentadams.bookList.fragments.SearchFragment$1.onClick(SearchFragment.java:51)
01-29 15:30:31.402: E/AndroidRuntime(777):  at android.view.View.performClick(View.java:4084)
01-29 15:30:31.402: E/AndroidRuntime(777):  at android.view.View$PerformClick.run(View.java:16966)
01-29 15:30:31.402: E/AndroidRuntime(777):  at android.os.Handler.handleCallback(Handler.java:615)
01-29 15:30:31.402: E/AndroidRuntime(777):  at android.os.Handler.dispatchMessage(Handler.java:92)
01-29 15:30:31.402: E/AndroidRuntime(777):  at android.os.Looper.loop(Looper.java:137)
01-29 15:30:31.402: E/AndroidRuntime(777):  at android.app.ActivityThread.main(ActivityThread.java:4745)
01-29 15:30:31.402: E/AndroidRuntime(777):  at java.lang.reflect.Method.invokeNative(Native Method)
01-29 15:30:31.402: E/AndroidRuntime(777):  at java.lang.reflect.Method.invoke(Method.java:511)
01-29 15:30:31.402: E/AndroidRuntime(777):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-29 15:30:31.402: E/AndroidRuntime(777):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-29 15:30:31.402: E/AndroidRuntime(777):  at dalvik.system.NativeStart.main(Native Method)

Line 51:

        String input = searchField.getText().toString();

Upvotes: 0

Views: 2844

Answers (2)

NameSpace
NameSpace

Reputation: 10177

So yeah, the problem is the R.id.searchfield. It's not a view that is a child of your MainActivity's rootview, its a child of the the dialog fragment.

The null pointer is occurring because it doesn't find R.id.searchfield, and searchfield is null as a result, which crashes you on the next line or so, when you call searchField.getText().

Just have your dialogfrag search the text field and pass back a string to your main activity.

Upvotes: 1

NasaGeek
NasaGeek

Reputation: 2198

findViewById() will only find views located in the layout file you specified in setContentView(). You need to either move the TextView into your Activity's layout or move this logic into your Fragment.

Upvotes: 1

Related Questions