Hi Im trying to go from an activity to a fragment.
Here’s the code that I have in my Activity
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);
}
}
And hers my fragment
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);
}
Now it complains on tableSummary.setBackgroundResource(R.drawable.book2); saying that it is syntax error on “setBackgroundResource” and the “.” Identifier expected.
And there is another error on nrOfBooksfield.setText(text); “misplaced construct on “.” and syntaxerror on token “text” VariableDeclarationId expected after this token.
Whats the error, it worked fine in the Activity and now it complains in the fragment (and yes Im a newbie on Android).
Thanks in advance.
Inflate a View and place your code inside
onCreateView()