rootView上的Android片段空指针异常

时间:2015-05-26 22:05:53

标签: android android-fragments fragment return-value

任何人都可以帮我这个我正在使用android导航片段模板而且我是android的新手,我刚刚开始向片段rootView和TTS引擎添加一些视图,但我来了将onClickListener添加到我的rootView中的按钮时出现错误已经尝试了很多,但我只是不能把它固定下来我可以请别人请看看并让我知道我哪里出错了, 这是我的代码

 public static class PlaceholderFragment extends Fragment implements    
 TextToSpeech.OnInitListener {

    private TextToSpeech myTTS;
    private int MY_DATA_CHECK_CODE = 0;
    private EditText Scr;
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_main, container,  
false);
        Scr = (EditText) rootView.findViewById(R.id.editText);





        return rootView;
    }

    public void onActivityResult(int requestCode, int resultCode, Intent  
 data) {
        if (requestCode == MY_DATA_CHECK_CODE) {
            if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
                myTTS = new TextToSpeech(getActivity(), this);
            }
            else {Intent installTTSIntent = new Intent();

installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(installTTSIntent);
            }
        }
    }
    public void onInit(int initStatus) {
        //check for successful instantiation
        if (initStatus == TextToSpeech.SUCCESS) {
            if (myTTS.isLanguageAvailable(Locale.UK) == 
TextToSpeech.LANG_AVAILABLE)
                myTTS.setLanguage(Locale.UK);
        } else if (initStatus == TextToSpeech.ERROR) {
            Toast.makeText(getActivity(), "Sorry! Text To Speech failed...", 
Toast.LENGTH_LONG).show();
        }







    }

    public void onViewCreated(View rootView, Bundle savedInstanceState) {
        super.onViewCreated(rootView, savedInstanceState);
        Scr.setEnabled(false);
        Intent checkTTSIntent = new Intent();
        checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE);
        Button speakButton = (Button) rootView.findViewById(R.id.speak);
        speakButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {


            }
        });
    }

感谢任何和所有帮助 在我甚至可以眨眼之前,我的问题已经得到了-3,有人想建议为什么吗?

1 个答案:

答案 0 :(得分:0)

你介意搬家吗?

Intent checkTTSIntent = new Intent(); checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA); startActivityForResult(checkTTSIntent, MY_DATA_CHECK_CODE); 

到onViewCreated方法?

你将setOnInitListener放到片段实现的部分在哪里?