0

嗨我试图从一个活动去一个片段。

这是我的活动中的代码

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 上的新手)。

提前致谢。

4

2 回答 2

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);
                }
    }
于 2012-11-14T17:20:22.153 回答
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);
}
于 2012-11-14T17:17:59.800 回答