【问题标题】:Why is my screen2 recieving null.null from screen1 when I use i.putExtra?当我使用 i.putExtra 时,为什么我的 screen2 从 screen1 接收 null.null?
【发布时间】:2012-05-03 17:54:05
【问题描述】:

我正在写一本食谱,我遇到了一个问题,当我从 screen1 发送信息时,我的 screen2 收到 null.null ...

这是我的 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;

    }
}

这是我的 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;

    }

}

任何想法,解决方案???在此先感谢:)

P.S.这是我的日志:

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

【问题讨论】:

    标签: android class android-intent


    【解决方案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);
    

    当您传递价值时,您使用的是 TV

    tv 都在大写字母中,您将获得价值

    【讨论】:

    • 你明白这是什么问题吗?
    • 是的,我只是再等 5 分钟让网站让我接受您的回答:P 谢谢:D
    • @gtumca-MAC 有趣的是,您在代码中将原来的大小写问题换成了另一个问题。原始帖子的“视图”中的“v”在 put 和 get 中都大写。答案的想法当然是正确的,因为这是密钥外壳的问题。
    【解决方案2】:

    区分大小写对于这种类型的数据传递至关重要

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

    其中 "T" 开始字符串额外名称 vs:

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

    其中 "t' 开始字符串额外名称。

    切换字符串以匹配,你会没事的。

    【讨论】:

      【解决方案3】:

      您在当前代码中使用的键在输入时为小写(例如textView1),但在获取时为大写(例如TextView1)。这意味着您在阅读时找不到额外的内容。确保使用相同的密钥。你可以改变大小写来匹配,但我发现一个完全避免这个问题的简单方法是使用一个通用的静态变量:

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

      然后就可以使用了:

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

      和:

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

      您可能必须找出一个特定的类来放置它,等等,但这是一个很好的策略。 Android 平台本身也使用这个。 (参见Intent,例如EXTRA_ALARM_COUNT)。

      【讨论】:

        【解决方案4】:

        只是小错误。这里

        变化:

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

        与:

        String Ingredients = i.getStringExtra("textView1");
        String Method = i.getStringExtra("textView2");
        

        【讨论】:

          猜你喜欢
          • 2020-05-10
          • 1970-01-01
          • 1970-01-01
          • 2020-02-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-26
          相关资源
          最近更新 更多