从Activity转换为片段

时间:2012-11-14 17:12:50

标签: android

嗨,我正试图从一个活动变成一个片段。

以下是我在活动中的代码

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TableLayout;
import android.widget.TextView;


public class SummaryActivity extends Activity{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SimpleBookManager testBook = new SimpleBookManager();
        setContentView(R.layout.summary);


        TableLayout tableSummary = (TableLayout) this.findViewById(R.id.tableSummary);
        tableSummary.setBackgroundResource(R.drawable.book2);

        TextView nrOfBooksfield = (TextView) this.findViewById(R.id.nrOfBooksInCollection);   
        String text = Integer.toString(testBook.count());
        nrOfBooksfield.setText(text);


    }
}

她的片段

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class BFragmentTab extends Fragment{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        return inflater.inflate(R.layout.summary, container, false);

    }

SimpleBookManager testBook = new SimpleBookManager();
TableLayout tableSummary = (TableLayout) getView().findViewById(R.id.tableSummary);
tableSummary.setBackgroundResource(R.drawable.book2);
TextView nrOfBooksfield = (TextView) getView().findViewById(R.id.nrOfBooksInCollection);
String text = Integer.toString(testBook.count());
nrOfBooksfield.setText(text);
}

现在它抱怨tableSummary.setBackgroundResource(R.drawable.book2);说它是“setBackgroundResource”和“。”的语法错误。预期的标识符。

nrOfBooksfield.setText(text);“错误的构造”和“。”上的语法错误以及此令牌后期望的“文本”VariableDeclarationId上存在另一个错误。

错误是什么,它在Activity中运行良好,现在它在片段中抱怨(是的,我是Android上的新手)。

提前致谢。

2 个答案:

答案 0 :(得分:1)

夸大视图并将代码置于onCreateView()

   public class BFragmentTab extends Fragment{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    View view = inflater.inflate(R.layout.summary, 
    SimpleBookManager testBook = new SimpleBookManager();
    TableLayout tableSummary = (TableLayout) view.findViewById(R.id.tableSummary);
    tableSummary.setBackgroundResource(R.drawable.book2);
    TextView nrOfBooksfield = (TextView) view.findViewById(R.id.nrOfBooksInCollection);
    String text = Integer.toString(testBook.count());
    nrOfBooksfield.setText(text);

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

答案 1 :(得分:0)

例如,将代码放在onActivityCreated方法中。

public void onActivityCreated(Bundle savedInstanceState) {
    SimpleBookManager testBook = new SimpleBookManager();
    TableLayout tableSummary = (TableLayout) getView().findViewById(R.id.tableSummary);
    tableSummary.setBackgroundResource(R.drawable.book2);
    TextView nrOfBooksfield = (TextView) getView().findViewById(R.id.nrOfBooksInCollection);
    String text = Integer.toString(testBook.count());
    nrOfBooksfield.setText(text);
}