Android从给定视图中找不到合适的父级

时间:2021-02-23 18:54:57

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.

我试图通过在Utils.java文件中将其创建为函数来为我的应用程序实现通用SnackBar代码。一切都很好,但每当我尝试打开这个SnackBar时,我的应用程序都会继续崩溃。 logcat中记录的错误是java.lang.IllegalArgumentException:从给定视图中找不到合适的父级。请提供有效的观点。

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,Utils.java中的示例代码和完整的logcat错误块如下所示。将不胜感激任何帮助......在此先感谢。

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)

07-05 20:17:52.125 18456-18456 / com.movi​​eduka.wp.wordpressvlog E / MultiWindowProxy:getServiceInstance失败! 07-05 20:18:37.595 18456-18456 / com.movi​​eduka.wp.wordpressvlog E / AndroidRuntime:FATAL EXCEPTION:main进程:com.movi​​eduka.wp.wordpressvlog,PID:18456 java.lang.IllegalArgumentException:找不到合适的父对象从给定的角度来看。请提供有效的观点。在android.support.design.widget.Snackbar.make(Snackbar.java:174)的com.movi​​eduka.wp.wordpressvlog.utils.Utils.snackBar(Utils.java:29)com.movi​​eduka.wp.wordpressvlog.MainActivity在Android.os.Handler.handleCallback的android.view.View $ PerformClick.run(View.java:21542)上的android.view.View.performClick(View.java:5273)$ 1.onClick(MainActivity.java:40) (Handler.java:815)android.app.Handler.dispatchMessage(Handler.java:104)android.app.Looper.loop(Looper.java:207)android.app.ActivityThread.main(ActivityThread.java: 5728)位于com.android.internal.os.ZygoteInit.main的com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:789)的java.lang.reflect.Method.invoke(Native Method)( ZygoteInit.java:679)

2 个解决方案

#1


2  

change in utils.java

utils.java中的更改

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

change in MainActivity.java

MainActivity.java中的更改

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

#2


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();
}

#1


2  

change in utils.java

utils.java中的更改

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

change in MainActivity.java

MainActivity.java中的更改

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

#2


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();
}