Android

[스낵바 오류 처리]java.lang.IllegalArgumentException: No suitable parent found from the given view. Please provide a valid view.

커스텀 스낵바를 구현하는 과정에 오류가 발생되었다. ACTIVITY 에서 사용할 때는 아무 문제 없던 코드인데

FRAGMENT에서 사용하니 오류가 발생된다.

2022-06-26 03:11:10.338 23036-23036/com.test.code E/RecyclerView: No adapter attached; skipping layout
2022-06-26 03:11:11.801 23036-23036/com.test.code E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.test.code, PID: 23036
    java.lang.IllegalArgumentException: No suitable parent found from the given view. Please provide a valid view.
        at com.google.android.material.snackbar.Snackbar.makeInternal(Snackbar.java:198)
        at com.google.android.material.snackbar.Snackbar.make(Snackbar.java:157)
        at com.test.code.ui.SearchHistoryFragment.showInputSnackbar(SearchHistoryFragment.java:491)
        at com.test.code.ui.SearchHistoryFragment.initUI(SearchHistoryFragment.java:225)
        at com.test.code.ui.SearchHistoryFragment.onCreateView(SearchHistoryFragment.java:178)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2698)
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:320)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1187)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1356)
        at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1434)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1497)
        at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:447)
        at androidx.fragment.app.FragmentManager.executeOps(FragmentManager.java:2169)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1992)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1947)
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849)
        at androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:413)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7615)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)
2022-06-26 03:11:11.803 23036-23036/com.test.code E/UncaughtException: java.lang.IllegalArgumentException: No suitable parent found from the given view. Please provide a valid view.
        at com.google.android.material.snackbar.Snackbar.makeInternal(Snackbar.java:198)
        at com.google.android.material.snackbar.Snackbar.make(Snackbar.java:157)
        at com.test.code.ui.SearchHistoryFragment.showInputSnackbar(SearchHistoryFragment.java:491)
        at com.test.code.ui.SearchHistoryFragment.initUI(SearchHistoryFragment.java:225)
        at com.test.code.ui.SearchHistoryFragment.onCreateView(SearchHistoryFragment.java:178)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2698)
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:320)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1187)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1356)
        at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1434)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1497)
        at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:447)
        at androidx.fragment.app.FragmentManager.executeOps(FragmentManager.java:2169)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1992)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1947)
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849)
        at androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:413)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7615)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)
2022-06-26 03:11:12.498 23036-23281/com.test.code E/FirebaseCrash: Unable to parse Json response string to get message: No value for crashes

 

[스낵바 구현을 위한 main_fragment.xml 레이아웃]

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/generalLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="top"
    android:orientation="vertical">
      
    .............생략
</RelativeLayout>

 

[오류가 발생한 코드]

private Snackbar globalSnackbar;

@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                         @Nullable Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.matching_fragment, container, false);
    ...........생략
    globalSnackbar = Snackbar.make(snackBarLayout, "", Snackbar.LENGTH_INDEFINITE);
    
    
}

전체 XML파일이 로드되기전에 스낵바를 호출하게 되어 발생된 오류이다.

 

[오류 해결방법] 

프레그먼트를 사용중인경우라면 onViewCreated()에서 처리하면 오류는 없어진다.

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

        View rootView = inflater.inflate(R.layout.matching_fragment, container, false);
        ...........생략
        globalSnackbar = Snackbar.make(snackBarLayout, "", Snackbar.LENGTH_INDEFINITE);
        Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout) globalSnackbar.getView();
        //layout.findViewById(android.support.design.R.id.snackbar_text).setVisibility(View.INVISIBLE);
        layout.setPadding(0,0,0,0);  //마진값 없애기
        layout.setBackgroundResource(R.drawable.bg_round_shape);      
        
        //inflate view
        View snackView = getLayoutInflater().inflate(R.layout.input_snackbar, null);
        checkbox1 = snackView.findViewById(R.id.checkbox1);
        
        ...
        layout.addView(snackView, 0);
        globalSnackbar.show();        
    }

 

[REFERENCE]

 

Leave a Reply

error: Content is protected !!