1

I have been looking around for the answer but no luck on this. to resume

the id of my button is correct Activity names are correct, but for some reason the Bundle is returning null and crashing my App, This is my Main Activity, please help.

Check the OnClick Listener I think I am using the correct form

Main Activity

package com.example.carlos.application1;


        import android.widget.Button;
        import android.content.Context;
        import android.content.Intent;
        import android.support.v7.app.ActionBarActivity;
        import android.os.Bundle;

        import android.view.KeyEvent;
        import android.view.Menu;
        import android.view.MenuItem;
        import android.view.View;

        import android.widget.AdapterView;
        import android.widget.ArrayAdapter;
        import android.widget.AdapterView.OnItemClickListener;
        import android.widget.ListView;

        import java.util.ArrayList;
        import java.util.List;

public class MainActivity extends ActionBarActivity {
    public final static String EXTRA_MESSAGE = "MESSAGE";
    private ListView obj;
    DBHelper mydb;
    int status = 0;
    Button hp_1, HP2, fo, previous, home, next,MZ,Control,show;

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

        mydb = new DBHelper(this);
        ArrayList array_list = mydb.getAllCotacts();
        ArrayAdapter arrayAdapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1, array_list);

        obj = (ListView)findViewById(R.id.listView1);
        obj.setAdapter(arrayAdapter);
        obj.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                // TODO Auto-generated method stub
                int id_To_Search = arg2 + 1;

                Bundle dataBundle = new Bundle();
                dataBundle.putInt("id", id_To_Search);

                Intent intent = new Intent(getApplicationContext(), DisplayValues.class);

                intent.putExtras(dataBundle);
                startActivity(intent);
            }
        });
        hp_1.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {
                status = 1;
                Bundle bundle = new Bundle();
                bundle.putInt("status", status);

                Intent intent = new Intent(MainActivity.this, DisplayValues.class);
                intent.putExtras(bundle);

                startActivity(intent);


            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        super.onOptionsItemSelected(item);

        switch(item.getItemId())
        {
            case R.id.item1:Bundle dataBundle = new Bundle();
                dataBundle.putInt("id", 0);

                Intent intent = new Intent(getApplicationContext(),DisplayValues.class);
                intent.putExtras(dataBundle);

                startActivity(intent);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    public boolean onKeyDown(int keycode, KeyEvent event) {
        if (keycode == KeyEvent.KEYCODE_BACK) {
            moveTaskToBack(true);
        }
        return super.onKeyDown(keycode, event);
    }
}
  • There are actually many cases like this but, everyone of them suggest the form I am using so I suspect is my Index parameter – Carlos Siverio Apr 23 '16 at 10:55
  • Welcome to Stack Overflow. Unfortunately you've posted a lot of code but only left a vague description of the problem, which makes it hard to help you. What *eaxctly* do you mean by "but for some reason the Bundle is returning null and crashing my App"? Please post a stack trace and more details - and ideally cut the code down to *just* what is required to demonstrate the problem. (It's not even clear what you mean by "my index parameter" here - nor what this has to do with sqlite.) – Jon Skeet Apr 23 '16 at 10:55
  • first to check your Arraylist is null or not if null then fetch error. – Patel Vicky Apr 23 '16 at 10:58
  • Sorry I forgot to put another details like my Log cat: – Carlos Siverio Apr 23 '16 at 10:59
  • java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.carlos.application1/com.example.carlos.application1.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference – Carlos Siverio Apr 23 '16 at 10:59
  • hp_1 = (Button) findViewById(R.id.?) declare in oncreate method – Patel Vicky Apr 23 '16 at 11:11

1 Answers1

1

you haven't assigned anything to hp_1 so its value is NULL and that is the problem. You need to do the following:

hp_1 = (Button) findViewById(...);
hp_1.setOnClickListener(new View.OnClickListener() {
...
Pooya
  • 6,083
  • 3
  • 23
  • 43