0

In a project I am currently working on, I am receiving the following error:

 java.lang.RuntimeException: Unable to start activity ComponentInfo{midamcorp.com.burgerkingapp/midamcorp.com.burgerkingapp.lunchHome}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object 

Here is my code currently

 public class lunchHome extends AppCompatActivity {
 private SimpleCursorAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
  int discriminator = 0;
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_lunch_home);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    databaseHelper dbHelper = new databaseHelper(this);
    SQLiteDatabase db = dbHelper.getReadableDatabase();
    SharedPreferences preferences = getSharedPreferences("config", MODE_PRIVATE);
    if(preferences.getInt("Location", 2) == 3) {
        discriminator = 3;
    } else if(preferences.getInt("Location", 2) == 2){
        discriminator = 2;
    }

    Button ltoButton = (Button)findViewById(R.id.lunchLTOButton);
    Button standardButton = (Button)findViewById(R.id.lunchMainButton);

    standardButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(lunchHome.this, lunchStandard.class);
            startActivity(intent);

        }
    });

    ltoButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(lunchHome.this, lunchLTOs.class);
            startActivity(intent);
        }
    });
 Cursor results = db.rawQuery("SELECT _id, name, icon, buildImage FROM " + databaseHelper.TABLE_NAME + " WHERE location = " + discriminator + " and LTO = " + 1 + " LIMIT 3", null);

 String[] from = new String [] {databaseHelper.ITEM_NAME};
 int[] to = new int[] {R.id.newName};

    adapter = new SimpleCursorAdapter(this, R.layout.item_list, results, from, to, 0);
    final ListView listContainer = (ListView) findViewById(R.id.listContainer);
    listContainer.setAdapter(adapter);

    listContainer.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Cursor cursor = (Cursor) listContainer.getItemAtPosition(position);
            int imageId = cursor.getInt(cursor.getColumnIndex("buildImage"));
 Intent intent = new Intent(lunchHome.this,        midamcorp.com.burgerkingapp.buildImage.class);
            intent.putExtra("imageID", imageId );
            startActivity(intent);

        }
    });


    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

}

The id's for the buttons are correct, so I am not sure why I am having this issue. I truly appreciate any help.

EDIT:

 <?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>



<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />

and

     <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="midamcorp.com.burgerkingapp.lunchHome">

<TextView android:text="LTO's" android:textSize="48dp" android:textAlignment="center" android:layout_marginBottom="50dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
<ListView android:id="@+id/listContainer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
   <Button android:text="View All" android:id="@+id/lunchLTOButton"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" />
   <TextView android:text="Main Items" android:textSize="48dp" android:textAlignment="center" android:layout_marginBottom="50dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

  <Button android:text="View" android:id="@+id/lunchMainButton"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />


</LinearLayout>
KellyM
  • 2,028
  • 3
  • 35
  • 72

2 Answers2

1

You haven't used second xml content_lunch_home in your main layout xml of your activity. That's why, you are unable to find the Buttons declared in content_lunch_home.

Do this something like this:

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:popupTheme="@style/AppTheme.PopupOverlay" />

<include
android:id="@+id/content_lunch_home"  
android:layout_height="wrap_content"
android:layout_width="match_parent" />

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />
Rohit Arya
  • 6,458
  • 1
  • 21
  • 37
0

You should try this:

public class MyActivity extends AppCompatActivity implements View.OnClickListener {

   Button buttona, buttonb, buttonc;

   @Override
   public void onCreate(Bundle bundle) {
       super.onCreate();

       ...

       buttona = (Button) findViewById(R.id.buttona);
       buttonb = (Button) findViewById(R.id.buttonb);
       buttonc = (Button) findViewById(R.id.buttonc);

       buttona.setOnClickListener(this);
       buttonb.setOnClickListener(this);
       buttonc.setOnClickListener(this);
  }

  @Override
  public void onClick(View v) {
      switch(v.getId()) {
          case R.id.buttona:
          // do something;
          break;
          case R.id.buttonb:
          // do something;
          break;
      ...
  }
}