2012-11-14 87 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); 
}