Android Studio无法访问的声明

时间:2016-01-19 15:41:20

标签: android android-studio

我在编译项目时遇到问题,在事件日志中说明:

Error:(110, 9) error: unreachable statement
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint: deprecation for details.
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    return inflater.inflate(R.layout.activity_analysis, container, false);
    InitializeAnalysisView(); // <-- [UNREACHABLE STATEMENT]
    return mRootView;
}

 private void InitializeAnalysisView(){
    mNameEditText = (EditText)mRootView.findViewById(R.id.editText_name2);
    mDescriptionEditText = (EditText)mRootView.findViewById(R.id.editText_descripion2);
    mGenderEditText = (EditText)mRootView.findViewById(R.id.editText_gender2);
    mProfileImageButton = (ImageButton)mRootView.findViewById(R.id.capture_image_button2);
    mProfileImageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            chooseanalysisimage();
        }
    });
}

@Override
public void onAttach(Activity activity){
    super.onAttach(activity);
    ((Gallery)activity).onSectionAttached(getArguments().getInt(Constants.ARG_SECTION_NUMBER));
} 

不推荐使用方法onAttach(Activity activity)。我在Android版本4.4.2的三星Galaxy Note 2上运行。 我尝试过在互联网上找到的解决方案但没有一个能够正常工作。

2 个答案:

答案 0 :(得分:4)

您将立即返回,而不会将膨胀的视图分配给mRootView,从而无法访问以下语句

return inflater.inflate(R.layout.activity_analysis, container, false);

应该是

mRootView =  inflater.inflate(R.layout.activity_analysis, container, false);

答案 1 :(得分:0)

您将立即返回而不将膨胀的视图分配给 mRootView,从而使以下语句无法访问

return inflater.inflate(R.layout.activity_analysis, container, false); 应该

mRootView = inflater.inflate(R.layout.activity_analysis, container, false); 复制!