0

I'm writing a recipe book, and I've encountered a problem, my screen2 recieves null.null when I send information from screen1 ...

This is my code for recipe_button_list:

public class Recipe_Button_List extends Activity {

    TextView inputMethod;

    TextView inputIngredients;

    @Override
    public void onCreate( Bundle savedInstanceState ) {

        super.onCreate( savedInstanceState );
        setContentView( R.layout.recipe_button_list );

        inputMethod = (TextView) findViewById( R.id.textView2 );
        inputIngredients = (TextView) findViewById( R.id.textView1 );
        //remember when adding more intents to make sure that they are classed as TextView not EditText

        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled( true );

        Button mainNext = (Button) findViewById( R.id.Recipe1 );
        mainNext.setOnClickListener( new OnClickListener() {

            public void onClick( View v ) {

                final TextView mTextView = (TextView) findViewById( R.id.textView1 );
                mTextView.setText( R.string.Test2 );
                final TextView mTextView2 = (TextView) findViewById( R.id.textView2 );
                mTextView2.setText( R.string.Test );
                Intent i = new Intent( getBaseContext(), recipedisplayscreen.class );
                //Sending data to the next screen
                i.putExtra( "textView1", inputIngredients.getText().toString() );
                i.putExtra( "textView2", inputMethod.getText().toString() );

                Log.e( "n", inputMethod.getText() + "." + inputIngredients.getText() );
                startActivity( i );
            }
        } );

        Button mainNext2 = (Button) findViewById( R.id.Recipe2 );
        mainNext2.setOnClickListener( new OnClickListener() {

            public void onClick( View v ) {

                final TextView mTextView = (TextView) findViewById( R.id.textView1 );
                mTextView.setText( R.string.Test );
                Intent i = new Intent( getBaseContext(), recipedisplayscreen.class );
                //Sending data to the next screen
                i.putExtra( "textView1", inputIngredients.getText().toString() );
                i.putExtra( "textView2", inputMethod.getText().toString() );

                Log.e( "n", inputMethod.getText() + "." + inputIngredients.getText() );
                startActivity( i );
            }
        } );
    }

    @Override
    public boolean onCreateOptionsMenu( Menu menu ) {

        MenuInflater inflater = getMenuInflater();
        inflater.inflate( R.menu.recipe_menu1, menu );
        return true;

    }
}

This is my recipe_display_screen:

public class recipedisplayscreen extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recipedisplayscreen);

        TextView MethodDisplay = (TextView) findViewById(R.id.textView3);
        TextView IngredientsDisplay = (TextView) findViewById(R.id.textView5);

        Intent i = getIntent();
        String Ingredients = i.getStringExtra("TextView1");
        String Method = i.getStringExtra("TextView2");
        Log.e("recipedisplayscreen", Ingredients + "." + Method);

        MethodDisplay.setText(Method);
        IngredientsDisplay.setText(Ingredients);


        ActionBar actionBar = getActionBar();
        setTitle(R.string.title);
        actionBar.setDisplayHomeAsUpEnabled(true);}

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
                case android.R.id.home:
                    // App icon in action bar clicked; go home
                    Intent intent = new Intent(this, MainScreen.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                    return true;
                default:
                    return super.onOptionsItemSelected(item);
            }
        }




     @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.recipe_menu1, menu);
            return true;

    }

}

Any ideas, solutions ??? Thanks in advance :)

P.S. This is my logcat:

05-03 18:55:34.734: E/n(21132): if this is displaying then Intent activity is working correctly.bla bla bla working correctly
05-03 18:55:34.883: D/dalvikvm(21132): GC_FOR_ALLOC freed 101K, 2% free 12815K/12999K, paused 23ms
05-03 18:55:34.883: I/dalvikvm-heap(21132): Grow heap (frag case) to 13.885MB for 1401676-byte allocation
05-03 18:55:34.922: D/dalvikvm(21132): GC_CONCURRENT freed 7K, 2% free 14177K/14407K, paused 4ms+2ms
05-03 18:55:34.945: E/recipedisplayscreen(21132): null.null
4

4 回答 4

1
    Intent i = getIntent();
    String Ingredients = i.getStringExtra("textview1"); <--- see diff TextView1
    String Method = i.getStringExtra("textview2"); <--- see diff TextView2
    Log.e("recipedisplayscreen", Ingredients + "." + Method);

when you are passing value you are useig T and V small

and t and v are in capital where you are getting values

于 2012-05-03T17:56:04.007 回答
1

Case sensativity is critical for this type of data passing

String Method = i.getStringExtra("TextView2");

where "T" starts the string extra name vs:

i.putExtra("textView2", inputMethod.getText().toString());

Where "t' starts the string extra name.

Switch the strings to match and you'll be fine.

于 2012-05-03T17:59:10.637 回答
1

just minor mistake. here

Change:

String Ingredients = i.getStringExtra("TextView1");
String Method = i.getStringExtra("TextView2");

With:

String Ingredients = i.getStringExtra("textView1");
String Method = i.getStringExtra("textView2");
于 2012-05-03T18:07:56.187 回答
1

The keys you are using in your current code are lowercase (e.g. textView1) when you put, but upper case when you get (e.g. TextView1). That means you are not finding the extra when you read. Make sure to use the same key. You could just change the case to match, but I find an easy way to avoid this problem entirely is to use a common static variable:

public static final String EXTRA_INGREDIENTS = "com.your.namespace.Ingredients";

Then you can use it:

Intent i = new Intent(getBaseContext(), recipedisplayscreen.class);
//Sending data to the next screen
i.putExtra(EXTRA_INGREDIENTS, inputIngredients.getText().toString());

and:

i.putExtra(EXTRA_INGREDIENTS, inputMethod.getText().toString());

You might have to figure out a specific class to put it in, etc. but it's a good strategy. The Android platform itself uses this too. (See Intent, e.g. EXTRA_ALARM_COUNT).

于 2012-05-03T18:40:20.113 回答