0

This is my layout file.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ImageDownloadActivity" >

<EditText 
    android:id="@+id/url_text"
    android:layout_width = "match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/url_field_hint"
    android:inputType="textUri"
    android:layout_alignParentTop="true"/>

<Button 
    android:id="@+id/download_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/download_url"
    android:layout_below="@+id/url_text"
    android:layout_centerHorizontal="true"
    android:onClick="downloadImage"
    />

</RelativeLayout>  

And this is my Fragment class

public class ImageDownloadFragment extends Fragment {

    public ImageDownloadFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        return (RelativeLayout)inflater.inflate(R.layout.image_download, null);
    }
}

In an activity I am doing this.

imageDownloadFragment = new ImageDownloadFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();

        fragmentTransaction.replace(R.id.image_download_layout, imageDownloadFragment);
        fragmentTransaction.commit();


EditText urlText = (EditText)findViewById(R.id.url_text);
    if(urlText!=null)
    urlText.setText(urlEntered);

So I have an xml layout. I am inflating that in a Fragment and then I am replacing the fragment in a container. Everything works fine till this point. But after committing the FragmentTransaction, when I try to access the EditText View in the fragment, it gives me the null pointer exception. All the ids are correct in the R file. Please suggest

Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
MobileAppDeveloper
  • 1,048
  • 2
  • 16
  • 27

2 Answers2

0

You should try:

public class ImageDownloadFragment extends Fragment {

    private EditText ed;

    public ImageDownloadFragment() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.image_download, container, false);
        ed = (EditText)v.findViewById(R.id.url_text);
        return v;
    }
}

Since the EditText you want to retrieve is located (I suppose) inside your Fragment's layout.

Laurent Dezitter
  • 710
  • 1
  • 8
  • 16
0

Check findViewById returns NULL when using Fragment

In your onCreateView try to use

return (RelativeLayout)inflater.inflate(R.layout.image_download, container, false);

Hope it helps else please comment.

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124