Android 实现导航抽屉时出错

Android 实现导航抽屉时出错,android,navigation-drawer,Android,Navigation Drawer,我有一个登录活动,若用户点击登录按钮,而不是自动进入导航抽屉,但我得到了一个错误。请帮我摆脱这个错误 MainActivity.java import android.content.Intent; import android.os.Bundle; import android.support.design.widget.NavigationView; import android.support.v4.app.FragmentManager; import android.support.v

我有一个登录活动,若用户点击登录按钮,而不是自动进入导航抽屉,但我得到了一个错误。请帮我摆脱这个错误

MainActivity.java

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener, AdapterView.OnItemSelectedListener, AdapterView.OnItemClickListener {


    private Toolbar toolbar;
    private DrawerLayout drawer;
    public String emailRegistration;





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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);


        //toolbar.setTitle("toolbar");
        setSupportActionBar(toolbar);



        //Set adapter to AutoCompleteTextView

      //  FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

//        if (!emailRegistration.matches("[a-zA-Z0-9._-]+@[a-z]+.[a-z]+")) {
//
//            email.setError("Invalid Email Address");
//
//        }

//        fab.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Snackbar.make(v, "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.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(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.main, 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.action_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();

        if (id == R.id.nav_camera) {
        setTitle("Home");
           Intent i = new Intent(MainActivity.this, HomeFragment.class);
            startActivity(i);
        } else if (id == R.id.nav_gallery) {
            setTitle("Edit Profile");
            EditFragment editFragment = new EditFragment();
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.fragment,editFragment).commit();

        } else if (id == R.id.nav_slideshow) {
            setTitle("Business");
            BusinessFragment businessFragment = new BusinessFragment();
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.fragment,businessFragment).commit();

        } else if (id == R.id.nav_manage) {
            setTitle("Message");
            MessageFragment messageFragment = new MessageFragment();
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.fragment,messageFragment).commit();

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        // TODO Auto-generated method stub
        //Log.d("AutocompleteContacts", "onItemSelected() position " + position);

    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {
        InputMethodManager imm = (InputMethodManager) getSystemService(
                INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);

    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        // TODO Auto-generated method stub

        // Show Alert
        Toast.makeText(getBaseContext(), "Position:"+i+" Country:"+adapterView.getItemAtPosition(i),
                Toast.LENGTH_LONG).show();

        Log.d("AutocompleteContacts", "Position:"+i+" Country:"+adapterView.getItemAtPosition(i));

    }
}
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;



public class LoginActivity extends AppCompatActivity {
    public EditText email, password;
    public TextView textView;
    public Button login;


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

        email = (EditText) findViewById(R.id.email);
//        if( email.getText().toString().length() == 0 )
//            email.setError("Email is required!" );
        password = (EditText) findViewById(R.id.text_input_password_toggle);
//        if( password.getText().toString().length() == 0 )
//            password.setError("Password is required!" );
        textView = (TextView) findViewById(R.id.text2);
        login = (Button) findViewById(R.id.login);

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });

        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(LoginActivity.this,signupactivity.class);
                startActivity(i);
            }
        });
    }



    }
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>
注册活动。爪哇

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;

import java.util.ArrayList;
import java.util.List;


public class signupactivity extends AppCompatActivity {
    public EditText firstname, lastname, email;
    private AutoCompleteTextView actv;
    public Button signup;
    String[] languages={"Indore","Ujjain","Bhopal","Delhi","Bangalore","Chennai"};
    List<CharSequence> list = new ArrayList<CharSequence>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_main);
        for (int i=0;i<20;i++){
            list.add("test " + i);  // Add the item in the list
        }
        firstname = (EditText)findViewById(R.id.firstname);
        lastname = (EditText)findViewById(R.id.lastname);
        actv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
        email = (EditText) findViewById(R.id.email);
        signup = (Button)findViewById(R.id.signup);
        ArrayAdapter adapter = new
                ArrayAdapter(this,android.R.layout.simple_list_item_1,languages);
        actv.setAdapter(adapter);
        actv.setThreshold(1);

        signup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(signupactivity.this, LoginActivity.class);
                startActivity(i);
            }
        });
    }

}
导入android.content.Intent;
导入android.os.Bundle;
导入android.support.v7.app.AppActivity;
导入android.view.view;
导入android.widget.ArrayAdapter;
导入android.widget.AutoCompleteTextView;
导入android.widget.Button;
导入android.widget.EditText;
导入java.util.ArrayList;
导入java.util.List;
公共类注册活动扩展了AppCompatActivity{
公共编辑文本名、姓氏、电子邮件;
私有自动完成文本视图actv;
公共按钮注册;
字符串[]语言={“印多尔”、“乌贾因”、“博帕尔”、“德里”、“班加罗尔”、“钦奈”};
列表=新的ArrayList();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.content\u main);

对于您正在使用的MainActivity.java中的(inti=0;i)

setContentView(R.layout.fragment_business);
但您的抽屉在activity_main.xml中

因此,您的抽屉是空的,您不能向空对象添加侦听器

将setContentView更改为

setContentView(R.layout.activity_main);

嘿,在MainActivity.java中,您正在使用setContentView(R.layout.fragment\u business)你的抽屉是在Actuviy.Maun.xml中,所以你的抽屉是空的,你不能把一个侦听器添加到一个空的对象。是的,现在它工作了,但是在后台它显示了我的注册页面。我也有一个信号功能。我如何从那里删除注册页面?因为第一个用户注册而不是点击登录,而不是进入空白的导航。igation drawer Page为了更好地理解,最好发布代码。我认为这是一个小问题,你所面临的是什么,谷歌搜索它会为你找到解决方案。如果你正在使用片段。为片段布局设置背景颜色,这样就可以了。否则,你可以直接替换片段。
setContentView(R.layout.activity_main);