I'm working on a Madlib app. The code you see below is the class that handles gathering input from edittext boxes found in madlibinput1.xml, storing that info as strings, and then sending that text to the madliboutput.xml where it replaces all "wx" with input gathered in the input xml. This class used to be an activity, but I found making it a fragment helps implement my navigation drawer better. However, I am an almost complete beginner with fragments (and programming in general) and feel like I didn't convert my code to work with fragments very well.

Can someone help me figure out why I have a null pointer exception in the gather() method (and perhaps null pointers elsewhere) when I hit the convert button. Perhaps I am doing something wrong getting information about my xml or something to do with my views. It would be very appreciated thank you.

package com.shamu11.madlibsportable; import java.io.IOException; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AutoCompleteTextView; import android.widget.Button; import android.widget.TextView; import android.app.Activity; import android.content.Context; import android.content.res.Resources; public class Madlibs extends Fragment { int inLayout; // will hold the id number for layout file // main_class_activity_in.xml. int outLayout; // will hold the id number for layout file // main_class_activity_out.xml. int outviewid; // will hold the id number for the textview found in // main_class_activity_out.xml //Activity activity = this; String test; AutoCompleteTextView autoview; StringBuffer stringbuffer = new StringBuffer(); String[] stringviews = new String[16]; Button convert; View view; public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //super.onCreate(savedInstanceState); view = inflater.inflate(R.layout.madlibinput1, container, false); //setContentView(R.layout.madlibinput1); outLayout = (R.layout.madliboutput1); inLayout = (R.layout.madlibinput1); outviewid = (R.id.outview14); convert = (Button) view.findViewById(R.id.convert); convert.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { gather(); postIt(); } }); return view; } public void gather() { ViewGroup layout = (ViewGroup) getView().findViewById(R.id.anskey_clover); View[] views = new View[layout.getChildCount()]; int[] ids = new int[layout.getChildCount()]; for (int i = 0; i < layout.getChildCount(); i++) { ids[i] = layout.getChildAt(i).getId(); } for (int i = 0; i < layout.getChildCount(); i++) { AutoCompleteTextView au = (AutoCompleteTextView) getActivity().findViewById(ids[i]); stringbuffer.append(au.getText().toString() + "

"); } //setContentView(outLayout); TextView outview = (TextView) getView().findViewById(outviewid); outview.setText(stringbuffer.toString()); } public void postIt() { String str = "let's go to the park"; String str2; String newstr = null; //setContentView(outLayout); TextView outview = (TextView) view.findViewById(outviewid); str = (String) outview.getText(); stringviews = stringbuffer.toString().split("

"); // turns the // stringbuffer from // getAllXml() into // an array and // assigns to // stringviews. for (int i = 0; i < stringviews.length; i++) { str2 = stringviews[i]; newstr = str.replaceFirst("wx", str2); // replaces all the "wx"s in // the // main_class_activity_out // textview with values from // strinvgviews. str = newstr; } outview.setText(newstr); stringbuffer.delete(0, stringbuffer.length()); Fragment newFragment = new MadlibsOutput(); FragmentTransaction transaction = getFragmentManager().beginTransaction(); transaction.replace(R.id.fragment1, newFragment); transaction.addToBackStack(null); transaction.commit(); } }

madlibinput1.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="#ffffff" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0.39" android:background="#ffffff" > <LinearLayout android:id="@+id/linearLayout1" android:layout_width="184dp" android:layout_height="match_parent" android:orientation="vertical" android:background="#ffffff" > <TextView android:id="@+id/outview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="8dp" android:text="1.) Adjective to Describe:" /> <TextView android:id="@+id/lvheader_song" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="8dp" android:text="2.) ing-verb (i.e. running):" /> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="8dp" android:text="3.) Negative Adjective:" /> <TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="8dp" android:text="4.) Negative Verb:" /> <TextView android:id="@+id/textView5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="8dp" android:text="5.) Adjective" /> <TextView android:id="@+id/textView6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="8dp" android:text="6.) Plural Noun:" /> <TextView android:id="@+id/textView7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="8dp" android:text="7.) Verb (ending in '-ed'):" /> </LinearLayout> <LinearLayout android:id="@+id/anskey_clover" android:layout_width="match_parent" android:layout_height="234dp" android:orientation="vertical" > <AutoCompleteTextView android:id="@+id/auview1" android:layout_width="141dp" android:layout_height="wrap_content" android:layout_weight="0.40" android:ems="10" android:text="la dee dah" /> <AutoCompleteTextView android:id="@+id/auview2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" /> <AutoCompleteTextView android:id="@+id/auview3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" /> <AutoCompleteTextView android:id="@+id/auview4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" /> <AutoCompleteTextView android:id="@+id/auview5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" /> <AutoCompleteTextView android:id="@+id/auview6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" /> <AutoCompleteTextView android:id="@+id/auview7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" /> </LinearLayout> </LinearLayout> <Button android:id="@+id/convert" android:layout_width="222dp" android:layout_height="93dp" android:layout_weight="0.25" android:text="Get My Mad Lib" /> </LinearLayout>

madliboutput1.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" > <TextView android:id="@+id/outview14" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="20dp" android:gravity="left" android:text="madlib text.........etc" /> </LinearLayout>

LOG CAT: