MainActivity puts valid String into Extras for an Intent but the Intent finds null String. What to do?

intent in android tutorial point
intent android
intent in android javatpoint
intent syntax in android
android intent putextra() and getextra example
getintent android
intent-filter in android manifest example
startactivityforresult

I am baffled by what's happening.

I put the file name "Help" into the Extras of the Intent that I then immediately start. (There can't be any problems with this first code block. With breakpoint after putExtra, Watch expression helpIntent.getStringExtra(PACKAGE_NAME + ".filename") returned "Help".)

import static com.dslomer64.sqhell.BuildConfig.APPLICATION_ID;
...
public class MainActivity extends Activity
{
  public final static String PACKAGE_NAME       = APPLICATION_ID;
...
  @Override public boolean onOptionsItemSelected(MenuItem item)
  {
    if(item.toString().equals("Help") || item.toString().equals("Acknowledgements"))
    {
      Intent
          helpIntent;
      helpIntent = new Intent(this, HelpActivity.class);

      helpIntent.putExtra(PACKAGE_NAME + ".filename", item.getTitle().toString());

      startActivity(helpIntent);
      return true;
    }
...
}

The fun begins in the spawned Intent:

public class HelpActivity extends Activity
{
...
  public void showHelp(AssetManager assets, String filename)
  {
    try
    {
    ////////////////////////////////////////////
    //                      filename = null ********* #1
       stream = assets.open(filename); 
    ////////////////////////////////////////////    }

    catch (IOException e) { e.printStackTrace(); }
...
  }

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

    Log.w("`````Help", "onCreate--bundle info: " + bundle.toString()) ;
    Log.w("`````Help","onCreate--bundle key: " + bundle.getString("filename") );

     Bundle bundle = getIntent().getExtras();

    setContentView(R.layout.activity_help);

    ////////////////////////////////////////////
    String fn = bundle.getString((PACKAGE_NAME + ".filename"));
    //     fn = null  ***************************** #2
    ////////////////////////////////////////////
    showHelp(getAssets(), fn ); 
  } 

The first sign of trouble (#1) was "filename is empty". Tracing back was easy. Now we're at #2, where--just compare--I use the same basic syntax to try to get the filename "Help" out of the Extras of the Intent. But it's null.

I'm not an expert with Intent but I've successfully used one a few times in the past.

I guess if I supplied a compilable program that shows the error it would be easier. But ...

If nobody spots anything right off, I'll get on that.

But it's straightforward!

But not.

================

EDIT

I added 2 Log statements (see above) and got this output:

    W/`````Help: onCreate--bundle info: 

Bundle[{com.dslomer64.sqhell.filename=Help}]
//////////////////////////////////////^^^^
    W/`````Help: onCreate--bundle key: null

Not sure how this helps since it looks like "Help" is where it should be. And I assume key is null is equivalent to the original problem about filename being null.

============================

EDIT #2: Here's the WHOLE HelpActivity class. If this doesn't help, I'll get on the compilable thing:

package com.dslomer64.sqhell;
import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;
import android.view.WindowManager;
import android.widget.EditText;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;
import static android.provider.ContactsContract.Directory.PACKAGE_NAME;

public class HelpActivity extends Activity
{
  Scanner     scDict;
  InputStream stream;
  EditText    help;

  public void showHelp(AssetManager assets, String filename)
  {
    help = /*(EditText)*/ findViewById(R.id.txaHelp);
    try
    {
      stream = assets.open(filename);
    }
    catch (IOException e) { e.printStackTrace(); }

    scDict = new Scanner(stream).useDelimiter("\n\r\t ,.:;-( )"); // added (SPACE) recently

    help.getText().clear();
    while(scDict.hasNext())
      help.append(scDict.next());

    hideKeyboard();

    help.setSelection(0);
  }

  @Override
  public void onCreate(Bundle savedInstanceState)
  {

    super.onCreate(savedInstanceState);

    Bundle bundle = getIntent().getExtras();

    setContentView(R.layout.activity_help);

    Log.w("`````Help", "onCreate--bundle info: "  + bundle.toString()) ;
    Log.w("`````Help", "onCreate--bundle key: "   + bundle.getString(PACKAGE_NAME + ".filename"));
    String fn =                                     bundle.getString(PACKAGE_NAME + ".filename");
    showHelp(getAssets(), fn );

  }

}

=========================

In following cricket's advice, I noticed a long line in the importarea:

    import static android.provider.ContactsContract.Directory.PACKAGE_NAME;

I don't remember ever seeing such before. So I took it out and got a few errors, wherever PACKAGE_NAME was used. Since each line had the "red underline" indicating use keypress 'Alt-Enter' for an easy import construction, I found that I HAD TWO CHOICES: the one above and MainActivity.PACKAGE_NAME.

I assume the previous time I did this, I chose the wrong item to import.

All is well.

Spent... too long on this.

Thanks to all who helped and Cricket for steering me in the right direction.

=====================

FINAL EDIT:

All I needed to do all along was delete the long import statement. And also pay attention to what I selected after doing it.

Criminey.

I thought I was doing what Cricket suggested all along. Didn't get the funny import in other classes. Why here????


it looks like "Help" is where it should be.

It does

... key is null

Its actually the value, but you're getting the value of the key "filename", not PACKAGE_NAME + ".filename", which is what you used in the putExtra.

The keys don't match, so the value is null

Suggestion: define your own constant like

public static final String FILENAME_EXTRA = APPLICATION_ID + ".filename";

and use that in all classes

Additionally your Scanner and everything requiring the file to be opened should be inside the try block.

Android Intents - Tutorial, How do you pass data from one activity to another activity using intent? 2 How to make ImageView reappear after making it null Jul 17 '17 1 Changing the color of different intent in an application Dec 24 '17 1 MainActivity puts valid String into Extras for an Intent but the Intent finds null String.


you put data in the Intent, not the Bundle.

So, try String fn = getIntent().getStringExtra(PACKAGE_NAME + ".filename").Then you will find what you want.

How to Pass Data from One Activity to Another in Android, is passed in key value pair. The value can be of types like int, float, long, string, etc. In order for the next activity to query the extra data, you should define the key for your intent's extra using a public constant. So add the EXTRA_MESSAGE definition to the top of the MainActivity class: public class MainActivity extends Activity { public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";


you are using a wrong key when retrieving the value.

  Log.w("`````Help","onCreate--bundle key: " + bundle.getString(PACKAGE_NAME + ".filename") );

Start a new Activity from Fragment, If only one component is found, Android starts this component directly. getExtras(); if (extras == null) { return; } // get data via the key String value1 The sub-activity uses the finish() method to create a new intent and to put data into it. But how does the Android system identify the components which can  I am baffled by what's happening. I put the file name "Help" into the Extras of the Intent that I then immediately start. (There can't be any problems with this first code block. With breakpoint a


Intent, Put together, the set of actions, data types, categories, and extra data defines a language More details on this can be found in the documentation on the IntentFilter class. NotesList" android:label="@string/title_notes_list"> <intent-​filter> <action Make an Intent for the main activity of an application, without specifying a  I'm trying to send intent from fragment to MainActivity. But while doing startActivity(intent) But while doing startActivity(intent) at this point app is working good but every time i click on listview fragment is refreshing and listview starts from 0 index again. What is the best way to send the intent from fragment.


Every time getting null data in extra intent · Issue #205 · firebase , I handle the notification strings in MainActivity's onResume() and in this way the "​handleNotification" function will called every time MainActivity  We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand


Sdk 691/init session forced · Issue #783 · BranchMetrics/android , getActivity(MainActivity.this, 0, intent, PendingIntent. valid URI format. In this case the app behaves like a cold start but still Branch can do probabilistic match. for (String key : EXTERNAL_INTENT_EXTRA_KEY_WHITE_LIST) { Put the raw JSON params as extra in case need to get the deep link  To do that we copy the file into the external storage directory and send an ACTION_MEDIA_SCANNER_SCAN_FILE intent so that it is shown on the user's Gallery. If the file does not have the correct extension or the Intent does not have the correct mime type set, the media will not be added to the MediaStore and will not be shown in the gallery.