我正在写一本食谱书,我遇到了一个问题,当我从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。这是我的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
答案 0 :(得分: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);
当你传递价值时,你使用 T 和 V 小
和 t 以及 v 是您获得价值的首都
答案 1 :(得分:1)
案例敏感性对于此类数据传递至关重要
String Method = i.getStringExtra("TextView2");
其中“T”开始字符串额外名称vs:
i.putExtra("textView2", inputMethod.getText().toString());
其中“t”开始字符串额外名称。
切换字符串以匹配,你会没事的。
答案 2 :(得分:1)
变化:
String Ingredients = i.getStringExtra("TextView1");
String Method = i.getStringExtra("TextView2");
使用:
String Ingredients = i.getStringExtra("textView1");
String Method = i.getStringExtra("textView2");
答案 3 :(得分:1)
您在当前代码中使用的密钥在放置时为小写(例如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
)。