为了账号安全,请及时绑定邮箱和手机立即绑定

将元素绑定到代码后,为什么我的代码总是收到空指针异常?我在Android Studio中使用Java

将元素绑定到代码后,为什么我的代码总是收到空指针异常?我在Android Studio中使用Java

牛魔王的故事 2021-04-09 14:14:09
尽管我已经正确绑定了元素,但是我正在接收java.lang.NullPointerException。试图检查元素的ID,它匹配,但是我仍然收到相同的异常。MainActivity.java@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    //Text to Speech    tts = new TextToSpeech(this, this);    speakButton = findViewById(R.id.speakButton);    speechText = findViewById(R.id.speechText);    speakButton.setOnClickListener(new View.OnClickListener() {        @Override        public void onClick(View v) {            speakOut();        }    });    // End    loadFragment(new HomeFragment());    BottomNavigationView navigation = findViewById(R.id.navigation);    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);}TranslatorFragment.javapublic class TranslatorFragment extends Fragment {public Button speakButton;public TranslatorFragment(){    //Empty Constructor}@Overridepublic View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {    return inflater.inflate(R.layout.translator_fragment, container, false);}public void onActivityCreated(Bundle savedInstanceState) {    super.onActivityCreated(savedInstanceState);}@Overridepublic void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {    super.onViewCreated(view, savedInstanceState);    speakButton = view.findViewById(R.id.speakButton);}}它应该运行正常,但是我收到了java.lang.NullPointerException
查看完整描述

2 回答

  • 2 回答
  • 0 关注
  • 202 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信