无法在导航标题

时间:2017-08-25 08:50:40

标签: android

public class Drawer extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    private TextView textView1, textView2;
    private ImageView imageView;
    private FirebaseAuth mAuth;
    private FirebaseAuth.AuthStateListener authListener;

    @Override
    protected void onStart() {
        super.onStart();
        mAuth.addAuthStateListener(authListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drawer);
        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.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();


        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        View headerView = navigationView.getHeaderView(0);
        //navigationView.addHeaderView(headerView);

        textView1 = (TextView) headerView.findViewById(R.id.textview);
        textView2 = (TextView) headerView.findViewById(R.id.textview1);
        imageView = (ImageView)headerView. findViewById(R.id.imageview);
        Bundle bundle = getIntent().getExtras();
        String Name = bundle.getString("name");
        String Email = bundle.getString("email");
        String photo = bundle.getString("photo");
        Log.d("TAG", "name" + Name);
        Log.d("TAG1", "email" + Email);
        Log.d("TAG2", "photo" + photo);

        Glide.with(this).load(photo).into(imageView);

        textView1.setText(Name);
        textView2.setText(Email);


        mAuth = FirebaseAuth.getInstance();
        authListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                if (firebaseAuth.getCurrentUser() == null) {
                    startActivity(new Intent(Drawer.this, MainActivity.class));
                }
            }
        };
    }

这是我的错误 - 我该如何解决?

  

08-25 14:13:42.154 31382-31382 / com.example.arun.logintest   E / AndroidRuntime:致命异常:主要                                                                               处理:com.example.arun.logintest,PID:31382                                                                               java.lang.RuntimeException:无法启动活动   ComponentInfo {com.example.arun.logintest / com.example.arun.logintest.Drawer}:   java.lang.IllegalArgumentException:您必须传入非null视图                                                                                   在   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2560)                                                                                   在   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2626)                                                                                   在android.app.ActivityThread.-wrap11(ActivityThread.java)                                                                                   在   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1475)                                                                                   在android.os.Handler.dispatchMessage(Handler.java:111)                                                                                   在android.os.Looper.loop(Looper.java:207)                                                                                   在android.app.ActivityThread.main(ActivityThread.java:5740)                                                                                   at java.lang.reflect.Method.invoke(Native Method)                                                                                   在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:905)                                                                                   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:766)                                                                                引起:java.lang.IllegalArgumentException:您必须传入非   null查看                                                                                   在   com.bumptech.glide.GenericRequestBuilder.into(GenericRequestBuilder.java:678)                                                                                   在   com.bumptech.glide.DrawableRequestBuilder.into(DrawableRequestBuilder.java:448)                                                                                   在com.example.arun.logintest.Drawer.onCreate(Drawer.java:81)                                                                                   在android.app.Activity.performCreate(Activity.java:6543)                                                                                   在   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)                                                                                   在   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2513)                                                                                   在   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2626)                                                                                   在android.app.ActivityThread.-wrap11(ActivityThread.java)                                                                                   在   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1475)                                                                                   在android.os.Handler.dispatchMessage(Handler.java:111)                                                                                   在android.os.Looper.loop(Looper.java:207)                                                                                   在android.app.ActivityThread.main(ActivityThread.java:5740)                                                                                   at java.lang.reflect.Method.invoke(Native Method)                                                                                   在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:905)                                                                                   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:766)08-25   14:13:43.809 31382-31382 / com.example.arun.logintest I / Process:发送   信号。 PID:31382 SIG:9

1 个答案:

答案 0 :(得分:0)

您可以通过inflateHeaderView方法查找标题布局来执行另一项操作。

NavigationView navigationView = (NavigationView) findViewById(R.id.navigationViewId);
View headerView = navigationView.inflateHeaderView(R.layout.nav_header);
textView1 = (TextView) headerView.findViewById(R.id.textview);
textView2 = (TextView) headerView.findViewById(R.id.textview1);
imageView = (ImageView)headerView. findViewById(R.id.imageview);

您现在可以从布局文件中删除app:headerLayout="@layout/nav_header"