0

Hello can anyone help me solving this problem I don't understand what exactly is wrong and Hello can anyone help me solving this problem I don't understand what exactly is wrong Hello can anyone help me solving this problem I don't understand what exactly is wrong and Hello can anyone help me solving this problem I don't understand what exactly is wrong

MainActivity.java

    public class MainActivity extends AppCompatActivity {

     Button btnSearch,btnDailyEntry,btnNewCust;

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

        btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btnSearch = (Button)findViewById(R.id.btn_search) ;
                Intent intent = new Intent(v.getContext(),Search.class);
                startActivity(intent);
            }
        });
        btnNewCust.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btnDailyEntry = (Button)findViewById(R.id.btn_daily_entry) ;
                Intent intent = new Intent(v.getContext(),NewCust.class);
                startActivity(intent);
            }
        });
        btnDailyEntry.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btnNewCust = (Button)findViewById(R.id.btn_new_cust);
                Intent intent = new Intent(v.getContext(),DailyEntry.class);
                startActivity(intent);
            }
        });

    }


}

activity_main.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="match_parent"
android:fitsSystemWindows="true"
    android:orientation="vertical"
    android:gravity="center">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Daily Entry System"
        android:textColor="@android:color/holo_blue_dark"
        android:textSize="20dp"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Search"
        android:id="@+id/btn_search" />


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Daily Entry"
        android:id="@+id/btn_daily_entry" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Cust"
        android:id="@+id/btn_new_cust" />
</LinearLayout>
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
bipin
  • 1,091
  • 4
  • 12
  • 30

3 Answers3

3

You have to find the button first, and after that set the onClick()

btnSearch = (Button) findViewById(R.id.buttonId);
Spirrow
  • 1,120
  • 6
  • 18
1

You never initialized your buttons.

Add

btnSearch = (Button)findViewById(R.id.btn_search);
btnNewCust = (Button)findViewById(R.id.btn_new_cust);
btnDailyEntry = (Button)findViewById(R.id.btn_daily_entry);

after

 setContentView(R.layout.activity_main);

Note: Default value in btnSearch etc would be null after normal declaration. To do anything on it you need to initialize it with a proper reference, otherwise you will get NullPointerException. Sorry to say but very basic exception of Java.

Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
0

Initialize your buttons with respect to their xml id's before setting their click listeners :

btnSearch = (Button) findViewById(R.id.btn_search);
btnDailyEntry = (Button) findViewById(R.id.btn_daily_entry);
btnNewCast = (Button) findViewById(R.id.btn_new_cust);


//Now set their onClickListeners..
Tafveez Mehdi
  • 456
  • 3
  • 12