Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8584889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:52:44+00:00 2026-06-11T21:52:44+00:00

In a class i am adding a Fragment programatically. This Fragment contains a Button,

  • 0

In a class i am adding a Fragment programatically.

This Fragment contains a Button, which i want to have an onClick implementation.

Here is the class i have:

public class ShareProduct extends SherlockFragmentActivity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.share_product);

        FragmentManager fragmentManager = getSupportFragmentManager();
        final FragmentTransaction fragmentTransaction = fragmentManager
                .beginTransaction();

        ProductFragment1 fragment = new ProductFragment1();
        fragmentTransaction.add(R.id.share_product_parent, fragment);
        fragmentTransaction.commit();

        Button done_button = (Button) fragment.getView().findViewById(
                R.id.done_product_1);
        done_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {

                ProductFragment2 fragment = new ProductFragment2();
                fragmentTransaction
                        .replace(R.id.share_product_parent, fragment);
                fragmentTransaction.commit();
            }
        });
    }
}

and my ProductFragment1 class is:

public class ProductFragment1 extends SherlockFragment {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater
                .inflate(R.layout.share_product_1, container, false);
        return view;
    }
}

and the layout my main class uses is:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/share_product_parent"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:gravity="right|center_vertical"
    android:orientation="horizontal" >

</RelativeLayout>

and the layout my ProductFragment1 uses is:

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

    <TextView
        android:id="@+id/tv1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="25dp" />

    <EditText
        android:id="@+id/et1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv1"
        android:textSize="25dp" />

    <Button
        android:id="@+id/done_product_1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/et1"
        android:text="Done" />

</RelativeLayout>

The problem is:

I am getting a NullPointerException at this line in my main class:

Button done_button = (Button) fragment.getView().findViewById(R.id.done_product_1);
done_button.setOnClickListener ....

when i searched for what getView() on a fragment returns. I found out that it returns the view from onCreateView();

I checked it doesn’t return null.

Thank You

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T21:52:46+00:00Added an answer on June 11, 2026 at 9:52 pm

    You are on the wrong track here. You get the NPE because the Fragments views is not created yet. Fragments also have a lifecycle just like Activities.

    What you should be doing is assigning the OnClickListener inside your Fragments onViewCreated() method. And from there… you should create a callback and notify your hosting Activity of the event.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

in my C#-project, I have a class which contains a List public class MyClass
I am adding a Map to a fragment with this code: public class MapFragment
Does making the below class final (adding public final class) have any real impact
Let's say we have a base class: public abstract class BaseFragment extends Fragment {
I have created a class for adding numbers: public class Add { private int
I have problem with adding class to every list element. This is example html:
I want to remove a css class before adding a new class to an
I'm have some issues adding a class to 2 navigation links on a theme.
I have the following JQuery statement and it is adding the class 'current' but
I have extended the standard FileSystemWatcher class by adding a string property (fileToTest), now

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.