{"id":1423,"date":"2022-08-30T15:16:24","date_gmt":"2022-08-30T15:16:24","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/16\/how-to-send-text-from-screen1-to-screen2-using-i-putextra-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:16:24","modified_gmt":"2022-08-30T15:16:24","slug":"how-to-send-text-from-screen1-to-screen2-using-i-putextra-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/how-to-send-text-from-screen1-to-screen2-using-i-putextra-collection-of-common-programming-errors\/","title":{"rendered":"How to send text from screen1 to screen2, using i.putExtra?-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m writing a recipe book, and I&#8217;ve encountered a problem &#8211; I want to send text from my recipe list to recipe display screen, and I must be doing something wrong because I keep getting force closes:<\/p>\n<p>This is code for my Recipe_Button_List<\/p>\n<p>public class Recipe_Button_List extends Activity {<\/p>\n<p>EditText inputMethod;<\/p>\n<p>EditText inputIngredients;<\/p>\n<pre><code>@Override\npublic void onCreate(Bundle savedInstanceState) {\n    super.onCreate(savedInstanceState);\n    setContentView(R.layout.recipe_button_list);\n\n    inputMethod = (EditText) findViewById(R.id.textView2);\n    inputIngredients = (EditText) findViewById(R.id.textView1);\n\n    ActionBar actionBar = getActionBar();\n    actionBar.setDisplayHomeAsUpEnabled(true);\n\n\n\n    Button mainNext = (Button) findViewById(R.id.Recipe1);\n    mainNext.setOnClickListener(new OnClickListener() {\n        public void onClick(View v) {\n            final TextView mTextView = (TextView) findViewById(R.id.textView1);\n            mTextView.setText(R.string.Test);\n            Intent i= new Intent(getBaseContext(),recipedisplayscreen.class);\n            \/\/Sending data to the next screen\n            i.putExtra(\"textView1\", inputIngredients.getText().toString());\n            i.putExtra(\"textView2\", inputMethod.getText().toString());\n\n            Log.e(\"n\", inputMethod.getText()+\".\"+ inputIngredients.getText());\n\n            startActivity(i);\n        }\n    });\n}\n @Override\n public boolean onCreateOptionsMenu(Menu menu) {\n     MenuInflater inflater = getMenuInflater();\n     inflater.inflate(R.menu.recipe_menu1, menu);\n     return true;\n\n}\n<\/code><\/pre>\n<p>}<\/p>\n<p>And this is my recipe_display_screen:<\/p>\n<p>public class recipedisplayscreen extends Activity {<\/p>\n<pre><code>@Override\npublic void onCreate(Bundle savedInstanceState) {\n    super.onCreate(savedInstanceState);\n    setContentView(R.layout.recipedisplayscreen);\n\n    TextView MethodDisplay = (TextView) findViewById(R.id.textView2);\n    TextView IngredientsDisplay = (TextView) findViewById(R.id.textView4);\n\n    Intent i = getIntent();\n    String Ingredients = i.getStringExtra(\"TextView1\");\n    String Method = i.getStringExtra(\"TextView2\");\n    Log.e(\"recipedisplayscreen\", Ingredients + \".\" + Method);\n\n    MethodDisplay.setText(Method);\n    IngredientsDisplay.setText(Ingredients);\n\n\n    ActionBar actionBar = getActionBar();\n    setTitle(R.string.title);\n    actionBar.setDisplayHomeAsUpEnabled(true);}\n\n    @Override\n    public boolean onOptionsItemSelected(MenuItem item) {\n        switch (item.getItemId()) {\n            case android.R.id.home:\n                \/\/ App icon in action bar clicked; go home\n                Intent intent = new Intent(this, MainScreen.class);\n                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);\n                startActivity(intent);\n                return true;\n            default:\n                return super.onOptionsItemSelected(item);\n        }\n    }\n\n\n\n\n @Override\n    public boolean onCreateOptionsMenu(Menu menu) {\n        MenuInflater inflater = getMenuInflater();\n        inflater.inflate(R.menu.recipe_menu1, menu);\n        return true;\n\n}\n<\/code><\/pre>\n<p>}<\/p>\n<p>Here are records from logCat:<\/p>\n<p>05-01 21:54:54.638: D\/AndroidRuntime(10717): Shutting down VM 05-01 21:54:54.638: W\/dalvikvm(10717): threadid=1: thread exiting with uncaught exception (group=0x40a301f8) 05-01 21:54:54.638: E\/AndroidRuntime(10717): FATAL EXCEPTION: main 05-01 21:54:54.638: E\/AndroidRuntime(10717): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bluStudios.Recipes4U.ics\/com.bluStudios.Recipes4U.ics.Recipe_Button_List}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText 05-01 21:54:54.638: E\/AndroidRuntime(10717): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956) 05-01 21:54:54.638: E\/AndroidRuntime(10717): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 05-01 21:54:54.638: E\/AndroidRuntime(10717): at android.app.ActivityThread.access$600(ActivityThread.java:123) 05-01 21:54:54.638: E\/AndroidRuntime(10717): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 05-01 21:54:54.638: E\/AndroidRuntime(10717): at android.os.Handler.dispatchMessage(Handler.java:99) 05-01 21:54:54.638: E\/AndroidRuntime(10717): at android.os.Looper.loop(Looper.java:137) 05-01 21:54:54.638: E\/AndroidRuntime(10717): at android.app.ActivityThread.main(ActivityThread.java:4424) 05-01 21:54:54.638: E\/AndroidRuntime(10717): at java.lang.reflect.Method.invokeNative(Native Method) 05-01 21:54:54.638: E\/AndroidRuntime(10717): at java.lang.reflect.Method.invoke(Method.java:511) 05-01 21:54:54.638: E\/AndroidRuntime(10717): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 05-01 21:54:54.638: E\/AndroidRuntime(10717): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 05-01 21:54:54.638: E\/AndroidRuntime(10717): at dalvik.system.NativeStart.main(Native Method) 05-01 21:54:54.638: E\/AndroidRuntime(10717): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText 05-01 21:54:54.638: E\/AndroidRuntime(10717): at com.bluStudios.Recipes4U.ics.Recipe_Button_List.onCreate(Recipe_Button_List.java:25) 05-01 21:54:54.638: E\/AndroidRuntime(10717): at android.app.Activity.performCreate(Activity.java:4465) 05-01 21:54:54.638: E\/AndroidRuntime(10717): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 05-01 21:54:54.638: E\/AndroidRuntime(10717): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 05-01 21:54:54.638: E\/AndroidRuntime(10717): &#8230; 11 more<\/p>\n<p>PS Force close happens when I press the button that takes you to the recipe_button_list<\/p>\n<p><strong>EDIT:<\/strong> Hi again \ud83d\ude42 I&#8217;ve got another small problem &#8211; my recipe_display_screen recieves null.null form my previous screen any idea why?<\/p>\n<p>LogCat:<\/p>\n<p>05-02 11:28:29.764: D\/dalvikvm(28455): GC_CONCURRENT freed 113K, 2% free 14526K\/14727K, paused 2ms+14ms 05-02 11:28:30.022: E\/n(28455): FromStringMethod.if this is displaying then Intent activity is working correctly 05-02 11:28:30.163: D\/dalvikvm(28455): GC_FOR_ALLOC freed 1769K, 13% free 12813K\/14727K, paused 23ms 05-02 11:28:30.163: I\/dalvikvm-heap(28455): Grow heap (frag case) to 13.883MB for 1401676-byte allocation 05-02 11:28:30.202: D\/dalvikvm(28455): GC_CONCURRENT freed 3K, 4% free 14179K\/14727K, paused 2ms+2ms 05-02 11:28:30.218: E\/recipedisplayscreen(28455): null.null<\/p>\n<p><strong>EDIT END<\/strong><\/p>\n<ol>\n<li>\n<p>Ah! you&#8217;re casting textviews to edit boxes. See the lines<\/p>\n<pre><code>inputMethod = (EditText) findViewById(R.id.textView2);\ninputIngredients = (EditText) findViewById(R.id.textView1);\n<\/code><\/pre>\n<p>change them to<\/p>\n<pre><code>inputMethod = (TextView) findViewById(R.id.textView2);\ninputIngredients = (TextView) findViewById(R.id.textView1);\n<\/code><\/pre>\n<p>and change the declarations of inputMethod and inputIngredients.<\/p>\n<p>Unless you meant them to be EditTexts, in which case you need to change that in your layout recipe_button_list<\/p>\n<\/li>\n<li>\n<p><strong>EDIT:<\/strong> In your Logcat you cast a TextView to an EditText. Set the view in your XML-layout-file to an EditText and it has to work \ud83d\ude42<\/p>\n<pre><code>inputMethod = (EditText) findViewById(R.id.textView2);\ninputIngredients = (EditText) findViewById(R.id.textView1);\n<\/code><\/pre>\n<p><strong>EDIT-END<\/strong><\/p>\n<p>you put your data in your first activity:<\/p>\n<pre><code>Intent i= new Intent(this, recipedisplayscreen.class);\n\/\/Sending data to the next screen\ni.putExtra(\"textView1\", inputIngredients.getText().toString());\ni.putExtra(\"textView2\", inputMethod.getText().toString());\n<\/code><\/pre>\n<p>in RecipeDisplayScreen you can fetch data in your <code>onCreate()<\/code><\/p>\n<pre><code>Bundle extras = getIntent().getExtras();\n\nString myText1;\nString myText2;\n\n\nif(extras != null) {\n   if (extras.containsKey(\"textView1\") {\n      myText1 = extras.getString(\"textView1\");\n   }\n   if (extras.containsKey(\"textView2\") {\n      myText2 = extras.getString(\"textView2\");\n   }\n}\n<\/code><\/pre>\n<p>I hope it works for you! If not, please post your Log-Output \ud83d\ude42<\/p>\n<p>Some tips: think for good key names and set them as constans:<\/p>\n<pre><code>public static final String INGREDIENTS_KEY = \"INGREDIENTS_TEXT_KEY\";\npublic static final String METHOD_KEY = \"METHOD_TEXT_KEY\";\n<\/code><\/pre>\n<p>Use for class name ALWAYS capitals <code>recipedisplayscreen<\/code> =&gt; <code>RecipeDisplayScreen<\/code> For fields use small letters: String Method =&gt; String txtMethod; <code>TextView MethodDisplay<\/code> =&gt; <code>TextView methodView;<\/code><\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-16 20:50:13. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;m writing a recipe book, and I&#8217;ve encountered a problem &#8211; I want to send text from my recipe list to recipe display screen, and I must be doing something wrong because I keep getting force closes: This is code for my Recipe_Button_List public class Recipe_Button_List extends Activity { EditText inputMethod; EditText inputIngredients; @Override public [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1423","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1423","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1423"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1423\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1423"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1423"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}