2

我已经实现了以下代码,但是当我运行应用程序并单击抽屉中的一个项目时,什么也没发生。我想这是与侦听器有关的问题,但我不知道如何解决它。或者可能是其他问题!

一旦我单击一个项目,唯一发生的就是 logcat 中的以下两行:

01-12 03:14:26.606 1565-1565/? I/LatinIME: Starting input. Cursor position = -1,-1
01-12 03:14:29.601 1565-1565/? I/LatinIME: Starting input. Cursor position = 0,0

这是代码:

public class Welcome extends AppCompatActivity {
    private DrawerLayout mDrawer,dlDrawer;
    private NavigationView nvDrawer;
    private ActionBarDrawerToggle drawerToggle;

    private Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome);

        // Set a Toolbar to replace the ActionBar.
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        // Find our drawer view
        dlDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawerToggle = setupDrawerToggle();

        // Tie DrawerLayout events to the ActionBarToggle
        dlDrawer.setDrawerListener(drawerToggle);

       nvDrawer = (NavigationView) findViewById(R.id.nvView);
        // Setup drawer view
        setupDrawerContent(nvDrawer);

        FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
        tx.replace(R.id.drawer_layout, new EditPersonal());
        tx.commit();
    }

    private void setupDrawerContent(NavigationView navigationView) {
        navigationView.setNavigationItemSelectedListener(
                new NavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(MenuItem menuItem) {
                        selectDrawerItem(menuItem);
                        return true;
                    }
                });
    }
    private ActionBarDrawerToggle setupDrawerToggle() {
        return new ActionBarDrawerToggle(this, dlDrawer, toolbar, R.string.drawer_open, R.string.drawer_close);
    }

    public void selectDrawerItem(MenuItem menuItem) {
        // Create a new fragment and specify the planet to show based on
        // position
        Fragment fragment = null;

        Class fragmentClass;
        switch(menuItem.getItemId()) {
            case R.id.nav_first_fragment:
                fragmentClass = EditPersonal.class;
                break;
            case R.id.nav_second_fragment:
                fragmentClass = Chronogram.class;
                break;
            case R.id.nav_third_fragment:
                fragmentClass = BloodTest.class;
                break;
            case R.id.nav_forth_fragment:
                fragmentClass = GlucoseTest.class;
                break;
            case R.id.nav_fifth_fragment:
                fragmentClass = SendRecord.class;
                break;
            case R.id.nav_sixth_fragment:
                fragmentClass = ReviewLogin.class;
                break;
            case R.id.nav_seventh_fragment:
                fragmentClass = SignOut.class;
                break;
            default:
                fragmentClass = EditPersonal.class;
        }

        try {
            fragment = (Fragment) fragmentClass.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Insert the fragment by replacing any existing fragment
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.flContent,fragment).commit();

        // Highlight the selected item, update the title, and close the drawer
        menuItem.setChecked(true);
        setTitle(menuItem.getTitle());
        mDrawer.closeDrawers();
    }
    // ...
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (drawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    // Make sure this is the method with just `Bundle` as the signature
    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        drawerToggle.syncState();
    }
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggles
        drawerToggle.onConfigurationChanged(newConfig);
    }
}

应用程序屏幕截图:

在此处输入图像描述

4

3 回答 3

1

在你的开关盒上,尝试这样的事情:

FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();

case R.id.nav_first_fragment:

     EditPersonal editPersonal= new EditPersonal();
     fragmentTransaction.replace(R.id.flContent, editPersonal);
     fragmentTransaction.commit();
     break;

在其他情况下也这样做。

于 2016-01-12T02:29:12.523 回答
0

基本上我已经使用 Android Studio 本身的 Activity(导航抽屉活动)更改了代码。所以这里是代码:

    public class Welcome extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.drawer_open, R.string.drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.review_login_listview, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.menu_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();

        if (id == R.id.nav_first_fragment) {
            // Handle the camera action
        }
        else {

        }
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}
于 2016-01-12T14:26:48.423 回答
0

您是否尝试过将日志放入代码中以查看逻辑流向或无法流向的位置?

例如,在您的 switch 语句中,尝试查看 menuItem.getItemId() 是否确实与您的 case 语句匹配。看看你的听众是否真的是问题所在,或者它是否与案例不匹配会有所帮助。或者它可能是您的片段管理器的问题。

查看http://developer.android.com/reference/android/util/Log.html。您应该能够在 logcat 中看到日志。祝你好运!

于 2016-01-12T02:25:21.757 回答