Search code examples
javaandroidlistviewbundle

getExtras causing app to crash


I am working on an Android App that lets me create an Event in CreateActivity, and than passes the data from CreateActivity to the MainActivity through Bundle and putExtras().

It appears as if I am using .getExtras() or getString() wrong. The app crashes once the b.getString("TITLE") function is implemented.

public class MainActivity extends FragmentActivity implements OnClickListener {

    ListView listView;
    int lastIndex = -1;
    ArrayList<Event> lstEvents;

    // detail view
    TextView tvTitle, tvTime, tvDate;
    ImageView img;
    View vw_master;

    boolean _isBack = true;

    ImageButton add;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        // get list view
        listView = (ListView) findViewById(R.id.listViewFragment);
        lstEvents = new ArrayList<Event>();

        // // get detail controls
        tvTitle = (TextView) findViewById(R.id.textViewTitle);
        tvDate = (TextView) findViewById(R.id.textViewDate);
        tvTime = (TextView) findViewById(R.id.textViewTime);

        Bundle b = this.getIntent().getExtras();
        b.getString("TITLE");



            add = (ImageButton) findViewById(R.id.add);
        add.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.add:
            Intent intent = new Intent(this, CreateActivity.class);
            startActivity(intent);
            break;
        }
    }  

}

CREATEACTIVITY

 public class CreateActivity extends Activity implements OnClickListener {

    EditText etTitle;
    Button btDate;
    Button btTime;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        // /onclicklistener
        findViewById(R.id.btn_confirm).setOnClickListener(this);

        // edittexts and buttons
        btDate = (Button) findViewById(R.id.btn_date);
        etTitle = (EditText) findViewById(R.id.editTextTitle);
        btTime = (Button) findViewById(R.id.btn_time);
    }

    // Will be called via the onClick attribute
    // of the buttons in main.xml
    public void onClick(View view) {
        switch (view.getId()) {
        case R.id.btn_confirm:
             String title = etTitle.getText().toString();
             String time = btTime.getText().toString();
             String date = btDate.getText().toString();

            Log.e("LOG", title);
            Log.e("LOG", time);
            Log.e("LOG", date);



            Bundle newBundle = new Bundle();
            newBundle.putString("TITLE", title);
            newBundle.putString("TIME", time);
            newBundle.putString("DATE", date);



            Intent intent = new Intent(this, MainActivity.class);
            intent.putExtras(newBundle);
            startActivity(intent);
            break;
        }

    }

    public void showTimePickerDialog(View v) {
        DialogFragment newFragment = new TimePickerFragment();
        newFragment.show(getFragmentManager(), "timePicker");
    }

    public void showDatePickerDialog(View v) {
        DialogFragment newFragment = new DatePickerFragment();
        newFragment.show(getFragmentManager(), "datePicker");
    }

}

Error Log :

03-03 15:44:07.117: E/AndroidRuntime(15202): FATAL EXCEPTION: main
03-03 15:44:07.117: E/AndroidRuntime(15202): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.datetracker/com.example.datetracker.MainActivity}: java.lang.NullPointerException
03-03 15:44:07.117: E/AndroidRuntime(15202):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2202)
03-03 15:44:07.117: E/AndroidRuntime(15202):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2252)
03-03 15:44:07.117: E/AndroidRuntime(15202):    at android.app.ActivityThread.access$600(ActivityThread.java:146)
03-03 15:44:07.117: E/AndroidRuntime(15202):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1240)
03-03 15:44:07.117: E/AndroidRuntime(15202):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-03 15:44:07.117: E/AndroidRuntime(15202):    at android.os.Looper.loop(Looper.java:137)
03-03 15:44:07.117: E/AndroidRuntime(15202):    at android.app.ActivityThread.main(ActivityThread.java:5168)
03-03 15:44:07.117: E/AndroidRuntime(15202):    at java.lang.reflect.Method.invokeNative(Native Method)
03-03 15:44:07.117: E/AndroidRuntime(15202):    at java.lang.reflect.Method.invoke(Method.java:511)
03-03 15:44:07.117: E/AndroidRuntime(15202):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
03-03 15:44:07.117: E/AndroidRuntime(15202):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:564)
03-03 15:44:07.117: E/AndroidRuntime(15202):    at dalvik.system.NativeStart.main(Native Method)
03-03 15:44:07.117: E/AndroidRuntime(15202): Caused by: java.lang.NullPointerException
03-03 15:44:07.117: E/AndroidRuntime(15202):    at com.example.datetracker.MainActivity.onCreate(MainActivity.java:47)
03-03 15:44:07.117: E/AndroidRuntime(15202):    at android.app.Activity.performCreate(Activity.java:5200)
03-03 15:44:07.117: E/AndroidRuntime(15202):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-03 15:44:07.117: E/AndroidRuntime(15202):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2166)

Solution

  • I figured out that the problem was being caused from the fact that when the app is initially opened it is trying to getExtras() on values that haven't been placed yet.

    I used this as a simple fix to check if there is any extras at first launch.

      Intent intent = getIntent();
        if (intent.getExtras() == null) {
            // Do first time stuff here
        } else {
            // Do stuff with intent data here
            Bundle b = getIntent().getExtras();
            title = b.getString("TITLE");
            time = b.getString("TIME");
            date = b.getString("DATE");
    
        }