无法访问的声明

时间:2015-07-08 19:22:57

标签: android

我目前正在创建一个meme生成器,我得到了一条错误信息 无法说明这是什么意思,我该如何解决这个问题

这是代码

package com.example.curtis.memegenerator;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class BottomPictureFragment
    extends Fragment {

    private static TextView topMemeText;
    private static TextView bottomMemeText;

    // Variables topmemetext and bottommemetext
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.bottom_picture_fragment, container, false);
        return view;
        topMemeText = (TextView) view.findViewById(R.id.topMemeText);
        bottomMemeText = (TextView) view.findViewById(R.id.bottomMemeText);
    }

    //method public void , two parameters
    public void setTopMemeText(String top, String bottom) {
        topMemeText.setText(top);
        bottomMemeText.setText(bottom);
    }
}

2 个答案:

答案 0 :(得分:6)

将onCreateView重新排序为这样;

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.bottom_picture_fragment,container,false);
    topMemeText = (TextView) view.findViewById(R.id.topMemeText);
    bottomMemeText = (TextView) view.findViewById(R.id.bottomMemeText);
    return view;
}

任何代码都不能在返回语句后执行。记住它应该在最后一行功能中。

答案 1 :(得分:1)

您在onCreateView中的return语句之后放置了代码语句。他们永远不会到达。

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.bottom_picture_fragment,container,false);
    return view; //<- code returns from method here. It will never proceed
    topMemeText = (TextView) view.findViewById(R.id.topMemeText);
    bottomMemeText = (TextView) view.findViewById(R.id.bottomMemeText);
}