3

I am trying to implement a universal SnackBar code for my app by creating it as a function in the Utils.java file. All's well but my app keeps on crashing whenever I try to open this SnackBar. The error thet is logged in the logcat is java.lang.IllegalArgumentException: No suitable parent found from the given view. Please provide a valid view.

The sample code in the MainActivity.java, Utils.java and the full logcat error block are as below. Will appreciate any assistance...thanks in advance.

MainActivity.java

public class MainActivity extends AppCompatActivity {
public Utils util;
public Constants k;

@Override
protected void onCreate(Bundle savedInstanceState) {

    util = new Utils(this);
    k = new Constants();

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            util.snackBar("A Simple SnackBar!");
        }
    });

Utils.Java

import android.content.Context;
import android.content.Intent;
import android.support.design.widget.Snackbar;
import android.view.View;

import com.example.wp.wordpressvlog.MainActivity;
import com.example.wp.wordpressvlog.NewProductActivity;
import com.example.wp.wordpressvlog.R;

public class Utils {
    private Context _con;
    public View v;

    public Utils (Context _con) {
        this._con = _con;
    }
    public void snackBar(String msg) {
        Snackbar.make(v, msg, Snackbar.LENGTH_LONG).setAction("Action", null).show();
    }
}

logcat error

07-05 20:17:52.125 18456-18456/com.movieduka.wp.wordpressvlog E/MultiWindowProxy: getServiceInstance failed! 07-05 20:18:37.595 18456-18456/com.movieduka.wp.wordpressvlog E/AndroidRuntime: FATAL EXCEPTION: main Process: com.movieduka.wp.wordpressvlog, PID: 18456 java.lang.IllegalArgumentException: No suitable parent found from the given view. Please provide a valid view. at android.support.design.widget.Snackbar.make(Snackbar.java:174) at com.movieduka.wp.wordpressvlog.utils.Utils.snackBar(Utils.java:29) at com.movieduka.wp.wordpressvlog.MainActivity$1.onClick(MainActivity.java:40) at android.view.View.performClick(View.java:5273) at android.view.View$PerformClick.run(View.java:21542) at android.os.Handler.handleCallback(Handler.java:815) at android.os.Handler.dispatchMessage(Handler.java:104) at android.os.Looper.loop(Looper.java:207) at android.app.ActivityThread.main(ActivityThread.java:5728) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)

Biko
  • 332
  • 3
  • 15

2 Answers2

2

change in utils.java

public void snackBar(String msg, View v) {
    Snackbar.make(v, msg, Snackbar.LENGTH_LONG).setAction("Action", null).show();
}

change in MainActivity.java

fab.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        util.snackBar("A Simple SnackBar!", view);
    }
});
nishant
  • 2,526
  • 1
  • 13
  • 19
0

change your method this way

util.snackBar(view,"A Simple SnackBar!");
 public void snackBar(View view,String msg) {
    Snackbar.make(view, msg, Snackbar.LENGTH_LONG).setAction("Action", null).show();
}
Milan Pansuriya
  • 2,521
  • 1
  • 19
  • 33