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;

    @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)

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 {

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_website);

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

        //Open website
        webView = findViewById(R.id.webViewId);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient());
        webView.loadUrl(website);

    }
}

Intent, android.app.admin 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(R.id.add) 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,