簡體   English   中英

將元素綁定到代碼后,為什么我的代碼總是收到空指針異常? 我在Android Studio中使用Java

[英]Why is my code always receiving a null pointer exception when I have binded the element in to it? I am using Java in Android Studio

盡管我已經正確綁定了元素,但是我正在接收java.lang.NullPointerException。

試圖檢查元素的ID,它匹配,但仍然收到相同的異常。

MainActivity.java
@Override
protected 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.java
public class TranslatorFragment extends Fragment {

public Button speakButton;

public TranslatorFragment(){
    //Empty Constructor
}

@Override
public 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);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    speakButton = view.findViewById(R.id.speakButton);
}

}

它應該運行正常,但是我收到了java.lang.NullPointerException

按鈕代碼應在片段中而不是在活動中聲明,因為按鈕在fragment.xml中。 當您訪問活動的onCreate方法中的按鈕時,它將給出nullpointer異常,因為您的片段的視圖尚未呈現。最佳實踐是在片段的onViewCreated函數中訪問該視圖。

MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
loadFragment(new HomeFragment());

    BottomNavigationView navigation = findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}

TranslatorFragment.java
public class TranslatorFragment extends Fragment {

public Button speakButton;

public TranslatorFragment(){
    //Empty Constructor
}

@Override
public 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);
}

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

     //Text to Speech
    tts = new TextToSpeech(getActivity(), this);

    speakButton = findViewById(R.id.speakButton);

    speechText = findViewById(R.id.speechText);

    speakButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            speakOut();
        }
    });
    // End
}
void speakOut(){
}

解決方案是在項目中實現TextToSpeech.OnInitListener並將公共變量設為私有。

public class TranslatorFragment extends Fragment implements TextToSpeech.OnInitListener{

private Button speakButton; // public Button speakButton;
private TextToSpeech tts;
private EditText speechText; // public EditText speechText;

public TranslatorFragment(){
    //Empty Constructor
}

@Override
public 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);
}

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

    tts = new TextToSpeech(getActivity(), this);
    speakButton = view.findViewById(R.id.speakButton);
    speechText = view.findViewById(R.id.speechText);
    speakButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            speakOut();
        }
    });
}

private void speakOut(){
    String text = speechText.getText().toString();
    tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}

public void onInit(int status){
    if(status == TextToSpeech.SUCCESS){
        int result = tts.setLanguage(Locale.getDefault());

        if(result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED){
            Toast.makeText(getActivity(), "Language not supported!", Toast.LENGTH_SHORT).show();
        } else {
            speakButton.setEnabled(true);
            speakOut();
        }
    }
    else
    {
        Toast.makeText(getActivity(), "Initilization failed!", Toast.LENGTH_SHORT).show();
    }
}

public void onDestroy() {
    if(tts != null){
        tts.stop();
        tts.shutdown();
    }
    super.onDestroy();
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM