1

I want to make a simple menu (exactly the way I do it). When I click on a menu item, I open a new activity. The problem is that I can move from the main activity to any other activity without errors, but other activities get the following error when I try to click on the menu. I also added all the activities to the manifest file

What am I doing wrong? Please advise me. I am a beginner.

MainActivity:

public class MainActivity extends AppCompatActivity{

    DrawerLayout drawerLayout;
    RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        drawerLayout = findViewById(R.id.drawer_layout);
        recyclerView = findViewById(R.id.rv2);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));

        TextView next = (TextView) findViewById(R.id.cards);
        next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(view.getContext(), Cards.class);
                startActivityForResult(myIntent, 0);
            }
        });

        ImageView hamburger = (ImageView) findViewById(R.id.menu_ham);
        hamburger.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                drawerLayout.openDrawer(Gravity.START);
            }
        });
    }
}

Cards:

public class Cards extends AppCompatActivity {

    DrawerLayout drawerLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cards);
        drawerLayout = findViewById(R.id.drawer_layout);

        ImageView hamburger = (ImageView) findViewById(R.id.menu_ham);
        hamburger.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                drawerLayout.openDrawer(Gravity.START);
            }
        });

        TextView home = (TextView) findViewById(R.id.home);
        home.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(view.getContext(), MainActivity.class);
                startActivityForResult(myIntent, 0);
            }
        });
    }
}

Activity_cards.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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=".Cards">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <include layout="@layout/main_toolbar" />
    </LinearLayout>

    <androidx.constraintlayout.widget.ConstraintLayout 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:layout_marginTop="50dp"
        android:background="@color/white" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Cards" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.drawerlayout.widget.DrawerLayout>

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <include
            layout="@layout/main_toolbar" />

        <androidx.recyclerview.widget.RecyclerView
            android:padding="15dp"
            android:id="@+id/rv2"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>

    <RelativeLayout
        android:layout_width="300dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/white">

        <include layout="@layout/main_nav_drawer" />
    </RelativeLayout>
</androidx.drawerlayout.widget.DrawerLayout>

main_nav_drawer:

<?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="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_vertical">

        <ImageView
            android:layout_width="80px"
            android:layout_height="80px"
            android:layout_marginLeft="15dp"/>

        <TextView
            android:id="@+id/home"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="27dp"
            android:layout_weight="1"
            android:padding="12dp"
            android:text="Home"
            android:textSize="18dp"
            android:textStyle="bold"
            android:textColor="#000"/>


    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_vertical">

        <ImageView
            android:layout_width="80px"
            android:layout_height="80px"
            android:layout_marginLeft="15dp"/>

        <TextView
            android:id="@+id/cards"
            android:textColor="#000"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Cards"
            android:padding="12dp"
            android:textStyle="bold"
            android:textSize="18dp"
            android:layout_marginLeft="27dp"/>

    </LinearLayout>
</LinearLayout>

main_toolbar.xml:

<?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="wrap_content"
    android:orientation="horizontal"
    android:padding="12dp"
    android:gravity="center_horizontal">

    <ImageView
        android:id="@+id/menu_ham"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_baseline_menu_24"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="android coding"
        android:textSize="20sp"
        android:textStyle="bold"
        android:textAlignment="center"
        android:textColor="#636363"
        android:gravity="center_horizontal" />
</LinearLayout>

errors looks like:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication0000, PID: 24658
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication0000/com.example.myapplication0000.Cards}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7656)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
    at com.example.myapplication0000.Cards.onCreate(Cards.java:31)
    at android.app.Activity.performCreate(Activity.java:8000)
    at android.app.Activity.performCreate(Activity.java:7984)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:223) 
    at android.app.ActivityThread.main(ActivityThread.java:7656) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
ADM
  • 20,406
  • 11
  • 52
  • 83
Fathpath
  • 45
  • 6

1 Answers1

0

Add SlideMain id in your main_nav_drawer parent linear layout.

and Call this method onclick evenet from where you want to toggle drawer.

fun toggleDrawer() {
    if (drawer_layout.isDrawerOpen(SlideMain)) {
        drawer_layout.closeDrawer(SlideMain)
    } else {
        drawer_layout.openDrawer(SlideMain)
    }
}

Java :

private void toggleDrawer() {
    if (drawer_layout.isDrawerOpen(SlideMain)) {
        drawer_layout.closeDrawer(SlideMain)
    } else {
        drawer_layout.openDrawer(SlideMain)
    }
}

Call Method like this :

btn_toggle_drawer.onClicklistener{
toggleDrawer();
}

listener syntax can be wrong edited answer from mobile

Nadim Ansari
  • 574
  • 5
  • 20