java.lang.IllegalArgumentException: No suitable parent found from the given view. Please provide a valid view

那年仲夏 提交于 2019-12-01 03:35:26

I suggest that you to try with findViewById(android.R.id.content).

This is what did the trick for me:

Snackbar.make(findViewById(android.R.id.content), resp.getMessage(), Snackbar.LENGTH_LONG).show();

android.R.id.content will give you the root element of the view (for more information on that, please check Android: What is android.R.id.content used for?).

Ashutosh Tiwari

Replace your View with this

getActivity().getCurrentFocus().

but not in your Default Fragment

Wrap your layout with CoordinatorLayout

<android.support.design.widget.CoordinatorLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:id="@+id/chat"
     android:layout_height="match_parent">
     <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="@color/bg_register"
          android:gravity="center"
          android:orientation="vertical"
          android:padding="10dp">
               ...
               ...
               ...
    </LinearLayout>
</android.support.design.widget.CoordinatorLayout>

You are passing invalid view argument to Snackbar. so you've to give RegisterActivity BaseLayout reference to it. you should try declare id to activity_register.xml Linear / Relative or whatever you design.

  <LinearLayout 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:orientation="vertical"
              android:background="#a7b6bdd6"
    android:id="@+id/ll1"  //here 
     >

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="64dp"
        android:background="@color/colorPrimary"
        android:minHeight="?attr/actionBarSize" />

        //your design

</LinearLayout>

then initialise pass this layout to snackbar like below

 private void registerProcess(String name,String email,String password){

     final LinearLayout linearLayout = (LinearLayout)findViewById(R.id.ll1);// here change
      //Snackbar.make(linearLayout,"Hello", Snackbar.LENGTH_LONG).show();

    String tag_string_req = "req_register";
    //pDialog = new AlertDialog.Builder(getActivity());
    pDialog.setMessage("please wait");
    pDialog.show();


    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(Constants.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    RequestInterface requestInterface = retrofit.create(RequestInterface.class);

    User user = new User();
    user.setName(name);
    user.setEmail(email);
    user.setPassword(password);
    ServerRequest request = new ServerRequest();
    request.setOperation(Constants.REGISTER_OPERATION);
    request.setUser(user);
    Call<ServerResponse> response = requestInterface.operation(request);

    response.enqueue(new Callback<ServerResponse>() {
        private View view;

        public View getView() {
            return view;

        }

        @Override
        public void onResponse(Call<ServerResponse> call, retrofit2.Response<ServerResponse> response) {

            ServerResponse resp = response.body();

           if(resp !=null)//tried to check if resp is null but its not

//crash occurs here
            Snackbar.make(linearLayout,resp.getMessage(), Snackbar.LENGTH_LONG).show();
            pDialog.dismiss();
        }


        @Override
        public void onFailure(Call<ServerResponse> call, Throwable t) {

           // progress.setVisibility(View.INVISIBLE);
            Log.d(Constants.TAG, "failed");
            Toast.makeText(getApplicationContext(), t.getLocalizedMessage(), Toast.LENGTH_LONG).show();
            pDialog.dismiss();


        }
    });
}

Instead of this :

Snackbar.make(getView(),resp.getMessage(), Snackbar.LENGTH_LONG).show();

Replace getView() with an existing view component of your current activity, like this:

Snackbar.make(findViewById(R.id. chat),resp.getMessage(), Snackbar.LENGTH_LONG).show();

I found one solution that may help others who are looking for common solution.

I got this error when I have tried to show message with Snackbar before loading our whole XML file.

In fragment I have tried to call API in onCreateView(); and showing Snackbar message of Internet connection is off as below:

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    mBindingObject = DataBindingUtil.inflate(inflater, getLayoutResId(), container, false);

    getData();

    return mBindingObject.getRoot();
}

I just put that method in onViewCreated()

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    getData();
}

Its working for me now.

BONUS:

Always try to use view after its loaded successfully. Each and every position of one line is matters.

Thank you.

To solve this Exception, you can do this.

 private void CrearSnackBar(String mensaje)
{
    Snackbar snackbar = Snackbar
            .make(constraintLayout, mensaje, Snackbar.LENGTH_LONG)
            .setActionTextColor(getResources().getColor(R.color.colorWhite))
            .setAction(getResources().getString(R.string.label_settings), new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent myIntent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                    startActivity(myIntent);
                }
            });


    View sbView = snackbar.getView();
    snackbar.show();
}

Where constraintLayout is the layout parent where I want to show the message, declare the layout:

ConstraintLayout constraintLayout;

And find it by Id.

constraintLayout = findViewById(R.id.cl_layout);

The function works for me. I hope it could help you.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!