0

this is the class where i have done my login code and after login it fetches name and email of user to shoe. The fetching process is working well but when i try to set text of different layout xml file it gives me null pointer exception.

this is the NavigationMenu class here is my login function

   FragmentTransaction fragmentTransaction;
    NavigationView navigationView;
    RoundImage roundedImage;
    ImageView profileImage;
    TextView uname,mail;

    private SQLiteHandler db;
    private SessionManager session;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navigation_menu);

        toolbar = (Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        db = new SQLiteHandler(getApplicationContext());

        HashMap<String,String> user = db.getUserDetails();

        String name = user.get("name");
        String email = user.get("email");

        Log.d("Soulsystem", name);
        Log.d("Soulsystem", email);

        uname = (TextView)findViewById(R.id.alluser);
        mail = (TextView) findViewById(R.id.allemail);

        try {
            uname.setText(name);
            mail.setText(email);
        }catch (NullPointerException NPE)
        {
            Log.d("Soul system","Here is the error we are setting null values");
        }
       /* profileImage = (ImageView)view.findViewById(R.id.navigation_view_Image);
        profileImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("NavigationMenu", "In profile Image view");
                Intent i = new Intent(NavigationMenu.this, ClientProfile.class);
            }
        });*/

        drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
        actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,R.string.drawer_open,R.string.drawer_close);

        drawerLayout.setDrawerListener(actionBarDrawerToggle);
        fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.main_container,new HomeFragment());
        fragmentTransaction.commit();

        // session manager
        session = new SessionManager(getApplicationContext());

        if (!session.isLoggedIn()) {
            logoutUser();
        }

        navigationView = (NavigationView)findViewById(R.id.navigation_view);

        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem item) {

                switch (item.getItemId()){

                    case R.id.profile:

                        fragmentTransaction = getSupportFragmentManager().beginTransaction();
                        fragmentTransaction.replace(R.id.main_container, new ClientProfile());
                        fragmentTransaction.commit();
                        item.setChecked(true);
                        drawerLayout.closeDrawers();
                        break;

                    case R.id.logout:
                        logoutUser();
                       /* item.setChecked(true);
                        drawerLayout.closeDrawers();*/
                        break;
                }
                return true;
            }
        });


    }
    protected void onPostCreate(@Nullable Bundle savedInstanceState) {
        actionBarDrawerToggle.syncState();
        super.onPostCreate(savedInstanceState);
    }

    private void logoutUser() {
        session.setLogin(false);

        db.deleteUsers();
        Intent intent = new Intent(NavigationMenu.this, MainActivity.class);
        startActivity(intent);
        finish();
    }


}

And this one the Navigation menu xml file here I tried to set text and its working file

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/drawer_layout"
    tools:context="com.example.user.soulsystem.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/main_container">
            <TextView
                android:id="@+id/alluser"
                android:gravity="center"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <TextView
                android:gravity="center_horizontal"
                android:id="@+id/allemail"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </FrameLayout>

    </LinearLayout>
    <android.support.design.widget.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/navigation_view"
        android:layout_gravity="start"
        app:headerLayout="@layout/navigation_drawer_header"
        app:menu="@menu/drawe_menu">
    </android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>

This one the xml where i want to show the text how can i do this i tried using Layout Interline but when i tried to set text it is giving me null pointer exception on setText()

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:background="@color/bg_register">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:orientation="vertical"
        android:paddingLeft="20dp"
        android:paddingRight="20dp" >
    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/navigation_view_Image"
        android:background="@drawable/image"
        android:src="@drawable/profile_image"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="10dp"/>

    <TextView
        android:layout_below="@id/navigation_view_Image"
        android:layout_width="300dp"
        android:layout_height="50dp"
        android:id="@+id/username"
        android:textColor="@color/Black"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="10dp"
        android:layout_alignLeft="@+id/navigation_view_Image"
        android:layout_alignStart="@+id/navigation_view_Image" />
        </LinearLayout>
    <TextView
        android:layout_below="@id/username"
        android:layout_width="300dp"
        android:layout_height="50dp"
        android:id="@+id/email"
        android:textColor="@color/Black"
        android:layout_alignLeft="@+id/navigation_view_Image"
        android:layout_alignStart="@+id/navigation_view_Image" />
</LinearLayout>
Manish
  • 234
  • 2
  • 13
  • 1
    *"it gives me null pointer exception"* -- Okay, then show the logcat. What about the error is not clear where the problem exists? – OneCricketeer Sep 09 '16 at 05:38
  • 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.user.soulsystem.NavigationMenu.onCreate(Navigati‌​onMenu.java:57) – Nitesh Jadhav Sep 09 '16 at 05:40
  • error occurs on when i call username text view from 2nd xml file – Nitesh Jadhav Sep 09 '16 at 05:41
  • You can't use findViewById to find a Fragment layout value from the Activity class. Why can't you query the database from the Fragment and update the TextView there? – OneCricketeer Sep 09 '16 at 05:41
  • i tried the also. I created class and extended with fragment and called the 2nd xml in the and tried to set text in onCreate view but the was also not worked – Nitesh Jadhav Sep 09 '16 at 05:48
  • Which view is this second one? The headerLayout of the NavigationView? If so, you may see this post. http://stackoverflow.com/questions/33194594/navigationview-get-find-header-layout – OneCricketeer Sep 09 '16 at 05:50
  • The view which has 1 Image view and 2 textviews and i added it in navigation as header – Nitesh Jadhav Sep 09 '16 at 06:08
  • Okay, then does that linked post answer your question on how to find views from the NavigationView Header? – OneCricketeer Sep 09 '16 at 06:09
  • What does the toolbar_layout file (`layout="@layout/toolbar_layout"`) contain? BTW: If you request for help it is extremely helpful to reduce your code example to a minimum. You have database access code in your sample which is totally irrelevant to the problem. – gus27 Sep 09 '16 at 06:09
  • thanks cricket_007 its working now using this NavigationView navigationView = (NavigationView)findViewById(R.id.navigation_view); View header = navigationView.getHeaderView(0); uname = (TextView)header.findViewById(R.id.rahul); – Nitesh Jadhav Sep 09 '16 at 06:12
  • yes i want to reduce code gus42 can u give me some examples how to do and Singleton class can i use it to create instance in my application – Nitesh Jadhav Sep 09 '16 at 06:14

1 Answers1

0

Replace this line to

db = new SQLiteHandler(getApplicationContext());

to

db = new SQLiteHandler(this);

It will work fine as getApplicationContext() returns the context of the whole application, but 'this' returns the context of the Activity that is running.

Chandan kushwaha
  • 941
  • 6
  • 26