导航活动中的Android错误

时间:2015-12-29 13:53:36

标签: java android

我在Android应用程序中有一个导航活动。我使用Android工作室的默认模板来创建导航抽屉。 以下是Nav_headder_main.xml

中的代码
unordered_map

我正在尝试动态地从Java文件设置TextView的值。以下是java代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="@dimen/nav_header_height"
    android:background="@drawable/side_nav_bar"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:theme="@style/ThemeOverlay.AppCompat.Dark" android:orientation="vertical"
    android:gravity="bottom">

    <TextView android:layout_width="match_parent" android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:id="@+id/Name"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"
        />

    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
         android:id="@+id/mailId" />

</LinearLayout>

}

以下是activity_home.xml的代码

package example.android.webdroid.loginapplication;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.support.design.widget.NavigationView;
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.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import example.android.webdroid.loginapplication.Util.User;
import example.android.webdroid.loginapplication.Util.UserDataStore;
import example.android.webdroid.loginapplication.fragments.UpdateProfile;

public class Home extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

TextView tvUserName,tvMailId;
UserDataStore userData;
String auth="no";
Toolbar toolbar;
NavigationView navigationView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
   // Intent i=getIntent();
    /*if(i!=null) {
        auth = i.getExtras().getString("auth");
    }*/
    //To set up MainFragment - Uncomment this
    /*MainFragment fragment =new MainFragment();
    FragmentTransaction fragTran=getSupportFragmentManager().beginTransaction();
    fragTran.replace(R.id.displayPage,fragment);
    fragTran.commit();*/
     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.setDrawerListener(toggle);
    toggle.syncState();
    navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
    tvUserName=(TextView)navigationView.findViewById(R.id.Name);
    tvMailId=(TextView)navigationView.findViewById(R.id.mailId);
    userData=new UserDataStore(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.home, 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.

    switch (item.getItemId()){
    case R.id.nav_contact:
        Intent intent = new Intent(Intent.ACTION_DIAL);
        intent.setData(Uri.parse("tel:000000"));
        startActivity(intent);
        break;
    case R.id.nav_updateProfile:
        UpdateProfile fragment =new UpdateProfile();
        FragmentTransaction fragTran=getSupportFragmentManager().beginTransaction();
        fragTran.replace(R.id.displayPage, fragment);
        fragTran.commit();
        break;
     case R.id.nav_logout:
         userData.ClearUserData();
         userData.setUserLoggedIn(false);
         startActivity(new Intent(this,Login.class));
         break;
      default:
            break;
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}
@Override
protected void onStart() {
    super.onStart();
   if(authentiateUser()){
        displayUserDetails();
    }
    else {
        startActivity(new Intent(this, Login.class));
    }

}
public void displayUserDetails(){
    User user=userData.getLoggedInUserData();
    if(user==null){
        System.out.println("LoginAppln_Nag User null=========");
    }
    else {
        System.out.println("LoginAppln_Nag User not null=========" + user.mailId + user.customerId);
       tvUserName.setText("Welcome " + user.mailId);
        tvMailId.setText(user.name);
    }
}

public boolean authentiateUser(){
    return userData.getUserLoggedIn();
}

我在设置TextView字段的值时得到NullPointerException。

<?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_home" 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="false"
        app:headerLayout="@layout/nav_header_home" app:menu="@menu/activity_home_drawer" />

</android.support.v4.widget.DrawerLayout>

我正在尝试在导航抽屉的Headder部分中设置名称。导航抽屉中的文本视图仅在拉出时才可见。我是Android编程的新手,我不确定,是否有任何不同的方法在导航抽屉中设置文本视图

2 个答案:

答案 0 :(得分:1)

如果您的观点位于导航视图中。请尝试从导航视图中查找视图

tvUserName=(TextView) navigationView.findViewById(R.id.Name);
tvMailId=(TextView) navigationView.findViewById(R.id.mailId);

答案 1 :(得分:0)

您正在将activity_home.xml作为Activity主要布局:

setContentView(R.layout.activity_home);

你可以像这样分配TextView:

tvUserName=(TextView)findViewById(R.id.Name);
tvMailId=(TextView)findViewById(R.id.mailId);

但是那些TextView包含在名为Nav_headder_main.xml的布局中,您在布局中寻找它们(activity_home.xml

您需要在正确的布局中查找这些TextView,否则它们将显示为null,请尝试以下操作:

tvUserName=(TextView) navigationView.findViewById(R.id.Name);
tvMailId=(TextView) navigationView.findViewById(R.id.mailId);

编辑:以上假设您至少在Nav_header_main.xml主布局中某处<{1}} 虚高,例如Activity

include