0

I'm still new in creating interface. I refer a lot of tutorial but still not working. I want to pass the value from activity to fragment. mListener is null.

How the call the listener in correct way ?

Interface

public interface ListenerCardDetails {
    void getCardSelected(String id);
}

MainActivity.java

public class CardDetailsActivity extends AppCompatActivity {

private ListenerCardDetails mListener;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // some stuff
    mListener.getCardSelected("20");
}

public void setListener(ListenerCardDetails mListener) {
        this.mListener = mListener;
    }

MyFragment.java

public class TabPoints extends Fragment implements ListenerCardDetails {

    private CardDetailsActivity mCardDetailsActivity;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tab_points, container, false);
        configView(view);
        return view;
    }

    private void configView(View view){
        ((CardDetailsActivity) getActivity()).setListener(this);
    }

    @Override
    public void getCardSelected(String id) {
         Toast.makeText(getActivity(), id, Toast.LENGTH_LONG).show();
    }
}

Error

java.lang.RuntimeException: Unable to instantiate activity java.lang.InstantiationException: java.lang.Class has no zero argument constructor at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2548) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) Caused by: java.lang.InstantiationException: java.lang.Class has no zero argument constructor at java.lang.Class.newInstance(Native Method)

Any help is really appreciated.

Sariyanti
  • 37
  • 1
  • 5
  • You can't `instantiate` your activity. – Piyush Feb 08 '17 at 07:13
  • Check this one : [link](http://stackoverflow.com/a/37122385/5816000) This might help you. – Bishu Feb 08 '17 at 07:14
  • Please remove the constructor from the activity. Instead use a static method in activity and call it from the fragment to instantiate the listener. – Vaibhav Jain Feb 08 '17 at 07:21
  • @Piyush could you please provide an answer – Sariyanti Feb 08 '17 at 07:22
  • @VaibhavJain could you please write your answer – Sariyanti Feb 08 '17 at 07:23
  • @Bishu that one is not listener right ? – Sariyanti Feb 08 '17 at 07:30
  • Possible duplicate of [Communicating between a fragment and an activity - best practices.](http://stackoverflow.com/questions/14247954/communicating-between-a-fragment-and-an-activity-best-practices) – jakubbialkowski Feb 08 '17 at 08:04
  • Yours main problem is actually wrong usage of Activities and fragments. The `interface` is the last thing here. – jakubbialkowski Feb 08 '17 at 08:34
  • How does look like the association between the Activity and Fragment? Who hosts the the fragment? Because if the `CardDetailsActivity` then the `onCreate` method is called before `onCreateView` in fragment. So the NPE is unavoidable. – jakubbialkowski Feb 08 '17 at 09:31
  • The value will be retrieve at activity, then pass to fragment. So, onCreate method is called first then mListeners is keep null right ? What is the solution ? hmmm – Sariyanti Feb 08 '17 at 09:44
  • Yes, the `onCreate` is called first. Before doing something in Android you should first become more familiar with the framework itself. There is nice diagram about the lifecycle: https://plus.google.com/photos/photo/101826485820997153590/5914215085941005954?icm=false – jakubbialkowski Feb 08 '17 at 09:47
  • @Sariyanti Please post a code which will explain us where is the fragment actually put. Is it in layout? – jakubbialkowski Feb 08 '17 at 09:48
  • @jakubbialkowski, I'm using a viewpager for the fragment. It didnt put in any layout. – Sariyanti Feb 10 '17 at 03:52
  • @Sariyanti Then please show the layout of Activity with ViewPager and how do you set the ViewPager adapter. – jakubbialkowski Feb 10 '17 at 06:29

6 Answers6

2

doing this

new CardDetailsActivity(this);

is not the way android wants you to create activities at all, your code is not working because basiccally you are creating a TOTALLY NEW instance of the activity, not keeping in mind all the preparation the framework does for you in the background...

solution can be:

take the activity and add a new method where you set the listener interface!

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0
Unable to instantiate activity java.lang.InstantiationException: java.lang.Class has no zero argument constructor

This tells you exactly what happened. In Android you are not allowed to create any Activity or Fragment with custom constructor. It is because the Android framework uses reflection under the hood to instantiate them.

If you want to communicate between Activity and Fragment you should use the onAttach(Context context) in Fragment.

Please refer to the official docs of Android https://developer.android.com/training/basics/fragments/communicating.html

jakubbialkowski
  • 1,546
  • 16
  • 24
0

How you can do is, define Interface in your Fragment and add void getCardSelected(String id); method to that. Implement the same interface in your Activity and override the interface method and write your logic inside that method.

For more explanation follow this tutorial

If you want to send values from Activity to Fragment then follow this link: Send data from activity to fragment in android

Hope it will help you.

Community
  • 1
  • 1
0

do not use custom constructor, but put on a setListener instead.

public class MainActivity extends AppCompatActivity {

    private ListenerCardDetails mListener;

    public void setListener(ListenerCardDetails mListener) {
        this.mListener = mListener;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // some stuff
        mListener.getCardSelected("20");
    }


    public class TabPoints extends Fragment implements ListenerCardDetails {

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_tab_points, container, false);
            configView(view);
            return view;
        }

        private void configView(View view){
            ((MainActivity) getActivity()).setListener(this);
        }


        @Override
        public void getCardSelected(String id) {
            Toast.makeText(getActivity(), id, Toast.LENGTH_LONG).show();
        }
    }
0

try this `public class MainActivity extends AppCompatActivity {

    private ListenerCardDetails mListener;

    public void setListener(ListenerCardDetails mListener) {
        this.mListener = mListener;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TabPoints fragmentListingMyGrid = new TabPoints();
        getSupportFragmentManager().beginTransaction().replace(R.id.hidesContainer, fragmentListingMyGrid);

        fragmentListingMyGrid.setCardSelected("20");
    }

}

public class TabPoints extends Fragment {

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

    public void setCardSelected(String id) {
        Toast.makeText(getActivity(), id, Toast.LENGTH_LONG).show();
    }
}`
0

In your CardDetailActivity change the function setListener like this :

public static void setListener(ListenerCardDetails mListener) {
    this.mListener = mListener;
}

Now in you TabPoints fragment replace :

((CardDetailsActivity) getActivity()).setListener(this);

with :

CardDetailActivity.setListener(this);

OR

In your CardDetailActivity.java

public class CardDetailsActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TabPoints.bindListener(new ListenerCardDetails(){
            @Override
            public void getCardSelected(String id) {
                Toast.makeText(CardDetailsActivity.this, id, Toast.LENGTH_LONG).show();
            }
         }
    }
}

In your TabPoints.java

public class TabPoints extends Fragment {
ListenerCardDetails mListener;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_tab_points, container, false);
    return view;
    configView(view);

}

private void configView(View view){
    mListener.getCardSelected(20);
}

public static void bindListener(ListenerCardDetails listener) {
    mListener = listener;
}
Vaibhav Jain
  • 163
  • 13