首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法解析符号(字符串)

无法解析符号(字符串)
EN

Stack Overflow用户
提问于 2017-04-11 09:51:01
回答 3查看 2K关注 0票数 0

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

代码语言:javascript
运行
复制
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按钮。错误发生在

代码语言:javascript
运行
复制
(R.id.play_ahhh);

特别是在play_ahhh已经定义好的情况下。

代码语言:javascript
运行
复制
<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>
EN

回答 3

Stack Overflow用户

发布于 2017-04-11 09:57:52

更改

代码语言:javascript
运行
复制
<string name="play_ahhh">Ahhh"</string>

代码语言:javascript
运行
复制
<string name="play_ahhh">Ahhh</string>

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

代码语言:javascript
运行
复制
Button play_aaah = (Button) rootView.findViewById(R.id.play_ahhh);
return rootView;
票数 0
EN

Stack Overflow用户

发布于 2017-04-11 09:59:33

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

代码语言:javascript
运行
复制
<string name="play_ahhh">Ahhh"</string

更改为,

代码语言:javascript
运行
复制
<string name="play_ahhh">Ahhh</string
票数 0
EN

Stack Overflow用户

发布于 2017-04-11 10:00:50

代码语言:javascript
运行
复制
<string name="play_ahhh">Ahhh</string>

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

代码语言:javascript
运行
复制
// Inflate the layout for this fragment
return rootView;

将此行移动到onCreateView()函数的底部。它不会在return语句之后执行代码。我还建议您阅读一些与java相关的书籍,比如“用Java思考”。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43335481

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档