James
James

Reputation: 45

NullPointerException using fragments with textview

I'm getting a NullPointerException on tv.setMovementMethod(new ScrollingMovementMethod()); but i have absolutely no idea why! I have declared the textView in the XML file and initialized it inside onCreateView. Any help would be appreciated.

Here is the relevant section for the java code:

public class TestConnection extends Fragment {
    public TestConnection() {

    }


    JSONParser jParser = new JSONParser();

    //button pressed to get the items
    Button getAllItems;

    //declare list view
    TextView tv;

    //Progress dialog
    private ProgressDialog pDialog;

    //Make an arrayList containing all items
    ArrayList<HashMap<String, String>> itemsList;

    //url to get items
    private static String urlAllItems = "http://jamesalfei.netne.net/php/get_all_items.php";

    //JSON node/tag names
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_ITEMS = "items";
    private static final String TAG_PID = "pid";
    private static final String TAG_NAME = "name";

    //items array
    JSONArray items = null;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.sql_layout,
                container, false);
        getAllItems = (Button) getActivity().findViewById(R.id.SQLGetItems);

        //listview
        tv = (TextView) getActivity().findViewById(R.id.sqltv); 

        tv.setMovementMethod(new ScrollingMovementMethod());

        getAllItems.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // Launching All products Activity
                retreiveProducts();
            }
        });
        return view;
    }

and here is the xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <Button
            android:id="@+id/SQLGetItems"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:text="@string/getSQL" />

        <TextView
            android:id="@+id/sqltv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/SQLGetItems"
            android:text="@string/sqlLabel" />

    </RelativeLayout>

Apologies if i have used any bad coding practices, but i'm VERY new to android programming!

Upvotes: 2

Views: 1386

Answers (2)

Marcin Orlowski
Marcin Orlowski

Reputation: 75629

You are looking for your TextView in wrong view. You should be searching inflated view, not parent's view, mostly because you are not added there in your onCreateView(). So replace:

tv = (TextView) getActivity().findViewById(R.id.sqltv); 

with this

tv = (TextView) view.findViewById(R.id.sqltv); 

Upvotes: 5

Sam
Sam

Reputation: 86948

The View isn't attached to anything because you use false as the third parameter in inflate(). So you need to scope findViewById() to the layout that has your TextView. Use:

tv = (TextView) view.findViewById(R.id.sqltv); 

Upvotes: 3

Related Questions