片段中的按钮无缘无故地给出空指针



我以前做过片段,我处理过nnull指针,但是在这种情况下,当我尝试使用onClickListener进行majorSecond5按钮时,我得到了一个空指针。

public class Intervals_and_chords extends Fragment {
Button majorSecond5;
public void majorSecond ()
    {
        notimportant
    }
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.rhythm, container, false);

        majorSecond5 = view.findViewById(R.id.majorSecond);

        majorSecond5.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               majorSecond();
           }
       });

return view;
}
}

这是日志猫:

01-29 14:44:46.826 2508-2508/com.stefanawesome.piano E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       Process: com.stefanawesome.piano, PID: 2508
                                                                       java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                                           at com.stefanawesome.piano.Intervals_and_chords.onCreateView(Intervals_and_chords.java:158)
                                                                           at android.support.v4.app.Fragment.performCreateView(Fragment.java:2354)
                                                                           at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419)
                                                                           at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)
                                                                           at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809)
                                                                           at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799)
                                                                           at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580)
                                                                           at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367)
                                                                           at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322)
                                                                           at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2229)
                                                                           at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:700)
                                                                           at android.os.Handler.handleCallback(Handler.java:751)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                           at android.os.Looper.loop(Looper.java:154)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

尝试将代码移动到onActivityCreated中:

public class Intervals_and_chords extends Fragment {
Button majorSecond5;
public void majorSecond ()
{
    notimportant
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.rhythm, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    if (getView() != null) {
        majorSecond5 = getView.findViewById(R.id.majorSecond);
        majorSecond5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                majorSecond();
            }
        });
    }
}

}

majorsecond按钮是否存在于您的片段xml文件中?请再次检查身份证

相关内容

  • 没有找到相关文章

最新更新