2012-11-14 88 views
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);“错位结构上”。另一个错误”和的SyntaxError令牌‘文本’VariableDeclarationId预计此令牌后。

请告诉我的错误,它的工作在活动很好,现在它在抱怨该片段提前(是IM在Android新手)。

感谢。

回答

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); 
       } 
    } 
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); 
}