getExtras causing app to crash

Related searches

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;

    public void onCreate(Bundle savedInstanceState) {

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

        // // get detail controls
        tvTitle = (TextView) findViewById(;
        tvDate = (TextView) findViewById(;
        tvTime = (TextView) findViewById(;

        Bundle b = this.getIntent().getExtras();

            add = (ImageButton) findViewById(;


    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            Intent intent = new Intent(this, CreateActivity.class);



 public class CreateActivity extends Activity implements OnClickListener {

    EditText etTitle;
    Button btDate;
    Button btTime;

    public void onCreate(Bundle savedInstanceState) {

        // /onclicklistener

        // edittexts and buttons
        btDate = (Button) findViewById(;
        etTitle = (EditText) findViewById(;
        btTime = (Button) findViewById(;

    // Will be called via the onClick attribute
    // of the buttons in main.xml
    public void onClick(View view) {
        switch (view.getId()) {
             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);


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

    public void showDatePickerDialog(View v) {
        DialogFragment newFragment = new DatePickerFragment();, "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
03-03 15:44:07.117: E/AndroidRuntime(15202):    at
03-03 15:44:07.117: E/AndroidRuntime(15202):    at$600(
03-03 15:44:07.117: E/AndroidRuntime(15202):    at$H.handleMessage(
03-03 15:44:07.117: E/AndroidRuntime(15202):    at android.os.Handler.dispatchMessage(
03-03 15:44:07.117: E/AndroidRuntime(15202):    at android.os.Looper.loop(
03-03 15:44:07.117: E/AndroidRuntime(15202):    at
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(
03-03 15:44:07.117: E/AndroidRuntime(15202):    at$
03-03 15:44:07.117: E/AndroidRuntime(15202):    at
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(
03-03 15:44:07.117: E/AndroidRuntime(15202):    at
03-03 15:44:07.117: E/AndroidRuntime(15202):    at
03-03 15:44:07.117: E/AndroidRuntime(15202):    at

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");

My app crashes when switch back and forth through activitys (Intent , Here is the intent sending me to Activity 3. Bundle extras = getIntent().getExtras(); Intent intent = new Intent(Results1.this, UsersAnswers1.class); intent.putExtra("� According to a report by WABetaInfo, a message with long character strings are causing the app to crash. Similarly, contact information like vcards has been stuffed full of characters which may cause the app to crash. If you are using a more mid-range or an affordable phone with lesser powered chipsets - then you are in for multiple restarts.

I think the problem is you didn't initialize lstEvents. Let's try lstEvents = new ArrayList<Event>() in onCreate() methods

Passing value to another activity via intent putString is crashing app , Passing value to another activity via intent putString is crashing app with nullPointerException - android. extras = getIntent().getExtras(); assert extras != null; String moduleId = extras. This causes a NullPointerException startActivity( hi1);� Why do apps freeze or crash? There are several reasons why apps keep crashing or freezing. The app can become unresponsive or it may crash if it is not up to date. Many apps work a lot better if you have a strong Internet connection. Another reason is the fact the app is poorly coded.

You should declare all your variables inside onCreate() method!!!

public class WebsiteActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {

        WebView webView;
        Intent intent = getIntent();
        String website = intent.getStringExtra("key");

        //Open website
        webView = findViewById(;
        webView.setWebViewClient(new WebViewClient());


Intent, Activity Action: The user pressed the "Report" button in the crash/ANR dialog. If set in an Intent passed to Context#startActivity , this flag will cause any existing task that would be associated public Bundle getExtras () . A flaw in WhatsApp allows users to send “Scary Messages” that can cause the recipient’s app to crash, or even worse, result in the loss of their chat history/media etc. According to a report

My guess would be that in this line:

   Integer id = eventItem.get_Id();

get_Id() is returning null, based on the observation that you aren't initializing that field in the new Event() calls and that you haven't gotten into the habit of initializing variable instances yet.

Given that, you'd be passing in null to the view.setId(id) call. When you do initialiaze it, don't initialize it to 0 as that's not a valid Id for views. you can use NO_ID or a positive number.

Also: in onCreate() You can check that findViewById( is returning a View and not null. That would cause a NullPointerException at about the right line of code.

Detect and diagnose crashes, An Android app crashes whenever there's an unexpected exit caused by an unhandled exception or signal. An app that is written using Java or� A new ‘crash code’ is circulating on chat app WhatsApp, according to several user reports. The code causes the app to completely crash, giving the user no way to reopen the app, without fully

Android Intents Tutorial with Kotlin, If there are no apps to perform that action, then Android will return nothing, leaving you with a null value that will cause your app to crash! :[. For example, the history will show when you installed software, so you might be able to see whether crashes started occuring after the installation of a particular app. If you see an interesting event listed, double-click it to open a details window with more information.

Important: Safe mode temporarily turns off all downloaded apps. Restarting in safe mode varies by phone. To learn how to restart your phone in safe mode, visit your manufacturer’s support site. Step 2: Check if the problem goes away. Check if the problem goes away. If it does, an app is likely causing your problem. Go to the next step.

Facebook SDK Issue Again Causing Numerous iOS Apps to Crash or Freeze. Friday July 10, 2020 5:49 AM PDT by Mitchel Broussard. Popular iOS apps including Spotify, SoundCloud, Waze,

  • check bundle for null before accessing it, its non null when you fill data in bundle in saveInstancestate function. otherwise its null
  • @PulkitSethi - the bundle he is using is from the intent extras. However, the null check should still apply since the extras bundle can be null
  • please upload your logs output
  • When you get exceptions, post the stacktrace/logcat output as part of your question. It should tell you the exact line where you problem is.
  • Can you find which line is line 48 in your onCreate() and make a comment in your code? That will indicate what the problem is.
  • I added that piece, but I still get the same error. My guess is that it might have something to do with the SwipeAdapter that I have. I am going to post that code and let me know what you think. The SwipeAdapter was associated with another project that used a fragment and different layouts/views.
  • Can you post logcat output? It's better to find where is the error :)
  • That line is within the adapter, which isn't getting called until the end of onCreate(). If this was the error, then getView() would show as the top line in the stack trace for the NPE, rather than say it's caused at MainActivity.onCreate() line 48
  • from ItzHoudini: .."my guess is that it might have something to do with the SwipeAdapter..."
  • Then either he has multiple issues and his logcat is from another issue, or he's guessed incorrectly, which is why he asked the question.