无法解析符号(字符串)

时间:2017-04-11 01:51:01

标签: java android

简单的问题,为什么会发生这种情况?

import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.maxiesnax.gavinisms2.gavinisms2.R;



public class GavinFragment extends Fragment {
;



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_gavin, container, 
false);



    // Inflate the layout for this fragment
    return rootView;

    final MediaPlayer aahMP = MediaPlayer.create(getActivity(), R.raw.aaah);

    Button play_aaah = (Button) getActivity().findViewById(R.id.play_ahhh);

    play_aaah.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            aahMP.start();
        }
    });

}


}

这是我的片段代码,包含要播放的aah.mp3按钮。错误发生在

(R.id.play_ahhh);

具体是play_ahhh,当它已经在XML中定义时。

<resources>
<string name="app_name">Gavinisms</string>

<string name="navigation_drawer_open">Open navigation drawer</string>
<string name="navigation_drawer_close">Close navigation drawer</string>

<string name="action_settings">Settings</string>

<string name="play_ahhh">Ahhh"</string>
<!-- TODO: Remove or change this placeholder text -->
<string name="hello_blank_fragment">Hello blank fragment</string>
</resources>

3 个答案:

答案 0 :(得分:0)

更改

<string name="play_ahhh">Ahhh"</string>

<string name="play_ahhh">Ahhh</string>

此外,您应该使用片段中的rootView

Button play_aaah = (Button) rootView.findViewById(R.id.play_ahhh);
return rootView;

答案 1 :(得分:0)

在下面的行中删除文字内的双引号,

<string name="play_ahhh">Ahhh"</string

更改为,

<string name="play_ahhh">Ahhh</string

答案 2 :(得分:0)

<string name="play_ahhh">Ahhh</string>

我还在你的代码中发现了一个可能的错误......

// Inflate the layout for this fragment
return rootView;

将此行移动到onCreateView()函数的底部。它在返回语句后不会执行代码。我还建议你阅读一些与java相关的书籍,比如&#34; Think in Java。&#34;