-2

i am building the login screen which takes the details. i Am trying to put those details on the navigation View where your name-email can be seen. i saved the details using shared preference. The problem is when i am updating those views its throwing NULL POINT EXCEPTION. SO, how to intialize a view of another layout in MAin Activity

My LOG

07-05 12:02:39.549 24377-24377/? E/AndroidRuntime: FATAL EXCEPTION: main
                                               Process: rishabh.example.com.navigationdrawer, PID: 24377
                                               java.lang.RuntimeException: Unable to start activity ComponentInfo{rishabh.example.com.navigationdrawer/rishabh.example.com.navigationdrawer.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.addView(android.view.View)' on a null object reference
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
                                                   at android.app.ActivityThread.access$800(ActivityThread.java:155)
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                   at android.os.Looper.loop(Looper.java:135)
                                                   at android.app.ActivityThread.main(ActivityThread.java:5343)
                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                   at java.lang.reflect.Method.invoke(Method.java:372)
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
                                                Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.addView(android.view.View)' on a null object reference
                                                   at rishabh.example.com.navigationdrawer.MainActivity.onCreate(MainActivity.java:58)
                                                   at android.app.Activity.performCreate(Activity.java:6010)
                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413) 
                                                   at android.app.ActivityThread.access$800(ActivityThread.java:155) 
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317) 
                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                   at android.os.Looper.loop(Looper.java:135) 
                                                   at android.app.ActivityThread.main(ActivityThread.java:5343) 
                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                   at java.lang.reflect.Method.invoke(Method.java:372) 

Part of the code from MainActivity which is initializing the view.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    linearLayout=(LinearLayout)findViewById(R.id.base_navbar_id);
    LayoutInflater inflater=   (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v=inflater.inflate(R.layout.navigation_drawer_head,null);


    name1= (TextView)v.findViewById(R.id.name_navDrawer);

    //login window which runs only one time when app is installed

    myPreferences = new MyPreferences();
    if (MyPreferences.isFirst(this)) {
        Intent intent=new Intent(this,LoginActivity.class);
        startActivity(intent);

    }

    SharedPreferences sharedPreferences=((DataProvider)getApplicationContext()).getSharedPreferences("MY_LOGIN",MODE_PRIVATE);
    if(sharedPreferences.getBoolean("IS_TRUE",false)) {
        Log.i("tag", "data:" + sharedPreferences.getString("MY_NAME", "Name"));
        name1.setText(sharedPreferences.getString("MY_NAME", "Name"));
        linearLayout.addView(name1);

    }

My navigation_drawer_head.xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="140dp"
android:id="@+id/base_navbar_id"
android:background="@android:color/holo_orange_light">


<ImageView
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="2"
    android:id="@+id/imageView"
    android:src="@drawable/my_image"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="56dp"
    android:layout_marginStart="56dp" />


    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="7"
        android:layout_marginTop="30dp"
        android:hint="Name"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:textSize="25dp"
        android:id="@+id/name_navDrawer" />
   </LinearLayout>

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:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawerLayout"
tools:context="rishabh.example.com.navigationdrawer.MainActivity">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <include android:layout_height="wrap_content"
        android:layout_width="match_parent"
        layout="@layout/toolbar_layout"
        />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/container_fragment">

    </FrameLayout>

  </LinearLayout>
  <android.support.design.widget.NavigationView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/navView"
    android:layout_gravity="start"
    app:menu="@menu/drawer_menu"
    app:headerLayout="@layout/navigation_drawer_head"
    >
  </android.support.design.widget.NavigationView>
  </android.support.v4.widget.DrawerLayout>
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – earthw0rmjim Jul 05 '16 at 06:52

2 Answers2

1

Replace your below line,

    name1= (TextView)v.findViewById(R.id.name_navDrawer);

by

    name1= (TextView) findViewById(R.id.name_navDrawer);

And then check it will work.

And main thing is it is already in main xml file so it is in linear layout, so also check if you want to add another text view from drawer xml file so also check that.

Now make changes below,

    LayoutInflater inflater=   (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v=inflater.inflate(R.layout.navigation_drawer_head,null);

    linearLayout=(LinearLayout) v.findViewById(R.id.base_navbar_id);
Vickyexpert
  • 3,066
  • 4
  • 17
  • 34
  • that view is in another layout navigation_drawer_head.xml – R.K. GUPTA Jul 05 '16 at 07:12
  • @R.K.GUPTA But is there a `name_navDrawer` inside `navigation_drawer_head`? – Joshua Jul 05 '16 at 07:13
  • @R.K.GUPTA, then your linear layout is not in main.xml file it is in navigation_drawer_head.xml file check again, – Vickyexpert Jul 05 '16 at 07:15
  • @Joshua yes it is there inside the linear layout base_navbar_id of xml file navigation_drawer_head.xml see the above code – R.K. GUPTA Jul 05 '16 at 07:19
  • @Vickyexpert yes linear layout is there it is that view i want to initialize – R.K. GUPTA Jul 05 '16 at 07:21
  • @R.K.GUPTA Wait, I think that was your `activity_main.xml` lol. But if it is `navigation_drawer_head.xml`, is there **really** `R.id.base_navbar_id` in your `activity_main.xml`? – Joshua Jul 05 '16 at 07:23
  • @R.K.GUPTA, try to understand that when you setContentView(activity_main), then you can access only those views by findViewById(), which are in activity_main.xml file not from another file, your linear layout is not in activity_main.xml it is in navigation_drawer_head.xml file so it will not accissible by findViewById – Vickyexpert Jul 05 '16 at 07:25
  • Show you activity_main.xml file – Vickyexpert Jul 05 '16 at 07:25
  • @Vickyexpert i added my activity_main.....i know that is why i am initializing a new linear layout inflating the layout xml file into a view and then v.findViewById(textviewid) – R.K. GUPTA Jul 05 '16 at 07:31
  • now see your main xml file and check where is linear layout with id base_navbar_id ? there is no any linear layout inside activity_main.xml file with the id "base_navbar_id" then how can you get it by findViewById()? – Vickyexpert Jul 05 '16 at 07:34
  • @Vickyexpert thats why i used v.findViewByID() – R.K. GUPTA Jul 05 '16 at 07:39
  • @R.K.GUPTA `linearLayout=(LinearLayout)findViewById(R.id.base_navbar_id);`. Check once more time =p It is not `v.findViewById` – Joshua Jul 05 '16 at 07:41
  • @Joshua thanks that was the problem initially ,....but this didn't solve what i am trying to do. I removed the initialization of LinearLayout and just initilise the view ...the name1.setText() isn't changing text – R.K. GUPTA Jul 05 '16 at 07:59
  • @R.K.GUPTA You should probably open another question. – Joshua Jul 05 '16 at 08:01
0

As it has mentioned in the exception,

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.addView(android.view.View)' on a null object reference

This means linearLayout is null. This is caused by findViewById cannot find any view with R.id.base_navbar_id.

Joshua
  • 4,757
  • 1
  • 23
  • 45
  • but i have a linear layout with that id (xml added) – R.K. GUPTA Jul 05 '16 at 06:54
  • @R.K.GUPTA Do you call `setContentView`? It seems it does not exists in your code. – Joshua Jul 05 '16 at 06:55
  • Are you sure that, the file of mentioned above layout is named `activity_main.xml` and placed in a proper directory? – Tomasz Dzieniak Jul 05 '16 at 07:01
  • no the above file mentioned is a different xml file with name navigation_drawer_head.xml.....that is what i wanted to do, initialize a view of different layout! – R.K. GUPTA Jul 05 '16 at 07:09
  • @R.K.GUPTA I modify some of your code and try. It runs fine. What I did is just removing `MyPreferences` (I don't have it), changing `R.layout.navigation_drawer_head` to `R.layout.activity_main` (I don't have it) and create local variables. – Joshua Jul 05 '16 at 07:09
  • @R.K.GUPTA Vickyexpert may have the correct answer. – Joshua Jul 05 '16 at 07:11