EDITED -2
就像之前的那个一样,我试着做你在我给的第一个上所做的事情,并搜索了一些关于将一个Activity转换为Fragment的指令,但我得到的只是一堆错误。另外,你有一个很好的源教程或有关转换这类文件的信息吗?我真的很感激。
public class Lesson111 extends Activity{
private RadioGroup radioAnswerGroup;
private RadioButton radioAnswerButton;
private Button btnSubmit;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.onepointthree);
addListenerOnButton();
}
public void addListenerOnButton() {
radioAnswerGroup = (RadioGroup) findViewById(R.id.radioAnswer);
btnSubmit = (Button) findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// get selected radio button from radioGroup
int selectedId = radioAnswerGroup.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioAnswerButton = (RadioButton) findViewById(selectedId);
Toast.makeText(Lesson111.this,
radioAnswerButton.getText(), Toast.LENGTH_SHORT).show();
}
});
}
}
这是主要活动
public class NavigationActivity extends FragmentActivity {
private DrawerLayout mDrawerLayout;
ImageView home;
Fragment fragment = null;
TextView appname;
ExpandableListView expListView;
HashMap<String, List<String>> listDataChild;
ExpandableListAdapter listAdapter;
List<String> listDataHeader;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation);
home = (ImageView)findViewById(R.id.home);
home.setOnClickListener(homeOnclickListener);
appname = (TextView)findViewById(R.id.appname);
setUpDrawer();
}
/**
*
* Get the names and icons references to build the drawer menu...
*/
private void setUpDrawer() {
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerLayout.setScrimColor(getResources().getColor(android.R.color.transparent));
mDrawerLayout.setDrawerListener(mDrawerListener);
expListView = (ExpandableListView) findViewById(R.id.lvExp);
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
fragment = new Lesson1();
getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerLayout.closeDrawer(expListView);
expListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
switch (groupPosition) {
case 0:
switch (childPosition) {
case 0:
fragment = new Lesson1();
break;
case 1:
fragment = new Lesson11();
break;
case 2:
Activity activity = new Lesson111();
break;
default:
break;
}
break;
这不是整个代码。但我相信片段没有显示的原因是因为这个案例陈述。我错过了什么吗?
答案 0 :(得分:0)
您的代码中存在拼写错误,请使用Inflater
代替inflater
View rootView = Inflater.inflate(R.layout.twopointthree, null);
在您的代码中替换为此。
EDIT - 2,Lesson111 as Fragment
试试这个
public class Lesson111 extends Fragment{
private RadioGroup radioGroup;
public View onCreateView(LayoutInflater Inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = Inflater.inflate(R.layout.twopointthree, null);
/* Initialize Radio Group and attach click handler */
radioGroup = (RadioGroup) rootView.findViewById(R.id.radioGroup);
radioGroup.clearCheck();
/* Attach CheckedChangeListener to radio group */
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb = (RadioButton) group.findViewById(checkedId);
if(null!=rb && checkedId > -1){
Toast.makeText(Lesson111.this, rb.getText(), Toast.LENGTH_SHORT).show();
}
}
});
return rootView;
}
public void onClear(View v) {
/* Clears all selected radio buttons to default */
radioGroup.clearCheck();
}
public void onSubmit(View v) {
RadioButton rb = (RadioButton) radioGroup.findViewById(radioGroup.getCheckedRadioButtonId());
Toast.makeText(Lesson111.this, rb.getText(), Toast.LENGTH_SHORT).show();
}
}
在您的主要活动中替换
case 2:
Activity activity = new Lesson111();
与
case 2:
fragment = new Lesson111();