设置内容视图在我的HomeFragments上没有用

时间:2016-01-18 01:32:21

标签: java android android-fragments

我有setContentView(R.Id.fragment_home)但它在我的Fragment类上没有用,它说Cannot resolved method setContentView 'setContentView(int)'有人知道这个。这是我的代码:

package com.thesis.artificialintelligence;

import android.app.Activity;          
import android.os.Bundle;         
import android.speech.tts.TextToSpeech;        
import android.support.v4.app.Fragment;       
import android.support.v7.app.ActionBarActivity;   
import android.view.LayoutInflater;   
import android.view.View;   
import android.view.ViewGroup;   
import android.widget.TextView;

public class HomeFragment extends Fragment {

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_home);

}

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


    // Inflate the layout for this fragment
    return rootView;
}

@Override
public void onAttach(Activity activity) {    
    super.onAttach(activity);    
}

@Override    
public void onDetach() {    
    super.onDetach();    
}

1 个答案:

答案 0 :(得分:1)

您不在碎片中使用setContentView

您使用inflater在onCreateView加载它并将其返回。

如果您需要对任何视图元素的引用,您也可以在onCreateView中通过rootView.findViewById(...)来查找它们。

如果删除setContentView

,您的代码可能会有效
相关问题