简体   繁体   English

导航抽屉-片段未显示

[英]Navigation drawer - Fragment is not showing

EDITED -2 编辑-2

Its like the same one earlier, I tried to do what you did on the first one that I gave and searched some Instructions on converting an Activity to Fragment but all I get is a bunch of errors. 就像之前的一样,我试图按照您给出的第一个方法进行操作,并搜索了一些有关将Activity转换为Fragment的说明,但是我得到的只是一堆错误。 Also, do you have a good source tutorial or information on converting this kinds of files? 另外,您是否有很好的源教程或有关转换此类文件的信息? I would really much appreciate it. 我真的很感激。

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

            }

        });

    }
}

Here is the main activity 这是主要活动

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;

this is not the whole code. 这不是完整的代码。 But i believe the reason why the fragment is not showing is because of this case statement. 但我相信未显示该片段的原因是由于此case语句。 Did i missed something? 我错过了什么吗?

There is typo error in your code , use Inflater instead of inflater 您的代码中有拼写错误,请使用Inflater而不是inflater

View rootView = Inflater.inflate(R.layout.twopointthree, null);

replace with this in your code. 在您的代码中将其替换。

EDIT - 2 , Lesson111 as Fragment 编辑-2,第111课为片段

Try this 尝试这个

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

In your main activity replace 在您的主要活动中,替换

  case 2:
  Activity activity = new Lesson111();

with

  case 2:
  fragment = new Lesson111();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM