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>