简体   繁体   English

导航抽屉无法设置文本和图像

[英]Navigation drawer cannot set text and image

Case_history_review.java Case_history_review.java

This is one of my java to use the nav bar 这是我使用导航栏的Java之一

  Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle("E-care");
    setSupportActionBar(toolbar);

    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

    NavigationView view = (NavigationView) findViewById(R.id.navigation_view);
    view.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            Toast.makeText(Case_history_review.this, menuItem.getItemId() + " pressed", Toast.LENGTH_LONG).show();
            Log.d(R.id.nav_1+"", menuItem.getItemId() + " ");
            Intent intent = new Intent();
            switch (menuItem.getItemId())
            {

                case R.id.nav_1:
                    break;
                case R.id.nav_2:
                    intent.setClass(Case_history_review.this,queueshow.class);
                    //intent .putExtra("name", "Hello B Activity");
                    startActivity(intent);
                    break;
                case R.id.nav_3:
                    intent.setClass(Case_history_review.this,Appointmentcreate.class);
                    //intent .putExtra("name", "Hello B Activity");
                    startActivity(intent);
                    break;
                case R.id.nav_4:
                    intent.setClass(Case_history_review.this, AlarmActivity.class);
                    startActivity(intent);
                    break;
                case R.id.nav_5:

                    intent.setClass(Case_history_review.this, PatientReport.class);
                    startActivity(intent);
                    break;
                case R.id.nav_6:
                    intent.setClass(Case_history_review.this, TimeList.class);
                    startActivity(intent);
                    //logout
                    break;

            }
            menuItem.setChecked(true);
            drawerLayout.closeDrawers();
            return true;
        }
    });

    ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle( this, drawerLayout, toolbar, R.string.drawer_open , R.string.drawer_close){
        @Override
        public void onDrawerClosed(View drawerView) {
            super .onDrawerClosed(drawerView);
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            super .onDrawerOpened(drawerView);
        }
    };
    db = new SQLiteHandler(getApplicationContext());
    HashMap<String, String> dbuser = db.getUserDetails();
    TextView name = (TextView) view.findViewById(R.id.drawer_name);
    String username = dbuser.get("name");
    Log.d("naem",username);
    name.setText(username);
    ImageLoader imageLoader = AppController.getInstance().getImageLoader();
    CirculaireNetworkImageView photo = (CirculaireNetworkImageView) view.findViewById(R.id.drawer_thumbnail);
    photo.setImageUrl("http://192.168.43.216/test/" + dbuser.get("image"), imageLoader);
    drawerLayout.setDrawerListener(actionBarDrawerToggle);
    actionBarDrawerToggle.syncState();

These two lines get errors 这两行出现错误

 name.setText(username);
 photo.setImageUrl("http://192.168.43.216/test/" + dbuser.get("image"), imageLoader);

Error message: 错误信息:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                 at com.example.yuen.e_carei.Case_history_review.onCreate(Case_history_review.java:116)

layout/drawer_header.xml layout / drawer_header.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#0097a7"
>

       <com.example.yuen.CirculaireNetworkImageView
    android:id="@+id/drawer_thumbnail"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:layout_marginRight="8dp"
    android:elevation="8dp"
    android:layout_above="@+id/drawer_name"
    android:layout_alignStart="@+id/drawer_name" />

<TextView
    android:id="@+id/drawer_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="24dp"
    android:layout_marginStart="24dp"
    android:layout_alignParentBottom="true"
    android:text="Eddard Stark"
    android:textSize="14sp"
    android:textColor="#fff"
    android:textStyle="bold"
    android:paddingBottom="8dp"
    />
 </RelativeLayout>

menu/drawer.xml menu / drawer.xml

 <?xml version="1.0" encoding="utf-8"?>
 <menu xmlns:android="http://schemas.android.com/apk/res/android">
 <group android:checkableBehavior="single">
    <item
        android:id="@+id/nav_1"
        android:checked="true"
        android:icon="@drawable/hi"
        android:title="Case Review"/>
    <item
        android:id="@+id/nav_2"
        android:icon="@drawable/hi"
        android:title="Queue state"/>
    <item
        android:id="@+id/nav_3"
        android:icon="@drawable/hi"
        android:title="Create appointemnt"/>
    <item
        android:id="@+id/nav_4"
        android:icon="@drawable/hi"
        android:title="Alarm"/>
    <item
        android:id="@+id/nav_5"
        android:icon="@drawable/hi"
        android:title="Report"/>
    <item
        android:id="@+id/nav_6"
        android:icon="@drawable/hi"
        android:title="Logout"/>
 </group>
 </menu>

activity_case_history_solo.xml activity_case_history_solo.xml

<?xml version="1.0" encoding="utf-8"?>
 <android.support.design.widget.CoordinatorLayout       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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.yuen.e_carei.Case_history_review">

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <!-- your content layout -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

        </android.support.design.widget.AppBarLayout>

        <RelativeLayout 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:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            tools:context="com.example.yuen.e_carei.Case_history_review">

            <ImageView
                android:src="@drawable/hi"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:id="@+id/imageView2"
                android:layout_alignParentTop="true"
                android:layout_alignParentStart="true" />

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_alignParentTop="true"
                android:layout_toEndOf="@+id/imageView2"
                android:layout_alignBottom="@+id/imageView2"
                android:id="@+id/relativeLayout">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Name : "
                    android:textSize="25sp"
                    android:id="@+id/textView2"
                    android:paddingTop="10dp"
                    android:paddingBottom="10dp"
                    android:paddingLeft="10dp"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentStart="true" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="ID : "
                    android:textSize="25sp"
                    android:id="@+id/idTag"
                    android:paddingLeft="11dp"
                    android:layout_below="@+id/textView2"
                    android:layout_alignParentStart="true" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Chan Tai Man"
                    android:textSize="25sp"
                    android:id="@+id/nameResult"
                    android:paddingBottom="10dp"
                    android:layout_above="@+id/idResult"
                    android:layout_toEndOf="@+id/textView2" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="N001"
                    android:textSize="25sp"
                    android:id="@+id/idResult"
                    android:layout_below="@+id/textView2"
                    android:layout_toEndOf="@+id/idTag" />
            </RelativeLayout>

            <ListView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/listView_case_history"
                android:layout_alignEnd="@+id/relativeLayout"
                android:layout_below="@+id/imageView2" />
        </RelativeLayout>
    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/drawer" />

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


<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />

  </android.support.design.widget.CoordinatorLayout>

I want to change the text in the drawer_header to become other string. 我想更改抽屉标题中的文本以成为其他字符串。 However, it can get the id but crash in the runtime. 但是,它可以获取ID但在运行时崩溃。 Please give me some helps to solve this. 请给我一些帮助来解决这个问题。 Thank You. 谢谢。

Error depicts that the TextView object is null, means there is no such textview exists in the layout that is currently running. 错误描述TextView对象为null,表示当前正在运行的布局中不存在此类textview。

Did the textview present in the XML layout that you are calling? 文本视图是否出现在您要调用的XML布局中? Which layout did you put the TextView? 您将TextView放在哪种布局上? Kindly check whether your activity is correct!! 请检查您的活动是否正确!

The id that you use inside findViewById(R.id....) may be wrong. 您在findViewById(R.id....)内部使用的ID可能是错误的。 Double check that. 仔细检查一下。

You have called the 您已致电

TextView name = (TextView) view.findViewById(R.id.drawer_name);

in the Case_history_review.java but it seems that the above textView doesn't exists in your layout that you are calling. Case_history_review.java中,但似乎上面的textView在您正在调用的布局中不存在。 Probably you will be calling the below activity_case_history_solo.XML in the Activity and it doesn't has it. 也许你会被要求在活动下面activity_case_history_solo.XML,它不拥有它。 And the above TextView lies in the drawer_header.xml 上面的TextView放在抽屉里

Hope this helps you..!! 希望这对您有帮助。

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

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