{"id":1432,"date":"2022-08-30T15:16:29","date_gmt":"2022-08-30T15:16:29","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/16\/extremely-simple-android-app-keeps-fcing-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:16:29","modified_gmt":"2022-08-30T15:16:29","slug":"extremely-simple-android-app-keeps-fcing-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/extremely-simple-android-app-keeps-fcing-collection-of-common-programming-errors\/","title":{"rendered":"Extremely simple android app keeps FC&#39;ing-Collection of common programming errors"},"content":{"rendered":"<p>Hey I just started dabbling with android programming, and from my short period of time with it I&#8217;m a bit frustrated. I can get no errors in eclipse, but when I start my app it immediately force closes. I&#8217;m following an android tutorial book verbatim, and still I&#8217;m getting errors. My aim here is to create a list view inside a relative layout. Also, for some reason when I assign p.firstName or p.lastName it&#8217;ll force close as well, but anyways here&#8217;s my code. I&#8217;m using API level 15 (ie. Android 4.0.3) if that makes a difference.<\/p>\n<pre><code> package matthews.zack.test;\n import android.app.Activity;\n import android.os.Bundle;\n import android.view.View;\n import android.widget.Button;\n import android.widget.EditText;\n   public class _testActivity extends Activity {\n\nButton save;\nPeople p = new People();\n\/** Called when the activity is first created. *\/\n@Override\npublic void onCreate(Bundle savedInstanceState) {\n    super.onCreate(savedInstanceState);\n    setContentView(R.layout.main);\n\n    \/\/onInitialize();\n}\n\nprivate void onInitialize()\n{\n     save = (Button)findViewById(android.R.id.button1);\n     save.setOnClickListener(new View.OnClickListener() {\n\n\n        public void onClick(View v) {\n            \/\/ TODO Auto-generated method stub\n            EditText firstName = (EditText)findViewById(android.R.id.text1);\n            EditText lastName = (EditText)findViewById(android.R.id.text2);\n            p.setFirstName(\"Hai\");\/\/firstName.getText().toString());\n            p.setLastName(\"Hi\");\/\/lastName.getText().toString());\n            \/\/((EditText)findViewById(android.R.id.text1)).setText(\"This the tune bada bing bada boom\");\n        }\n    });\n}\n<\/code><\/pre>\n<p>}<\/p>\n<p>`<br \/>\nand here&#8217;s my XML code<\/p>\n<pre><code>`\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n`\n<\/code><\/pre>\n<p>Here&#8217;s my error output<\/p>\n<pre><code>06-12 00:32:44.871: D\/AndroidRuntime(30530): Shutting down VM\n06-12 00:32:44.871: W\/dalvikvm(30530): threadid=1: thread exiting with uncaught exception (group=0x40a3b1f8)\n06-12 00:32:44.879: E\/AndroidRuntime(30530): FATAL EXCEPTION: main\n06-12 00:32:44.879: E\/AndroidRuntime(30530): java.lang.RuntimeException: Unable to start activity ComponentInfo{matthews.zack.test\/matthews.zack.test._testActivity}: java.lang.NullPointerException\n06-12 00:32:44.879: E\/AndroidRuntime(30530):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)\n06-12 00:32:44.879: E\/AndroidRuntime(30530):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)\n06-12 00:32:44.879: E\/AndroidRuntime(30530):    at android.app.ActivityThread.access$600(ActivityThread.java:123)\n06-12 00:32:44.879: E\/AndroidRuntime(30530):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)\n06-12 00:32:44.879: E\/AndroidRuntime(30530):    at android.os.Handler.dispatchMessage(Handler.java:99)\n06-12 00:32:44.879: E\/AndroidRuntime(30530):    at android.os.Looper.loop(Looper.java:137)\n06-12 00:32:44.879: E\/AndroidRuntime(30530):    at android.app.ActivityThread.main(ActivityThread.java:4424)\n06-12 00:32:44.879: E\/AndroidRuntime(30530):    at java.lang.reflect.Method.invokeNative(Native Method)\n06-12 00:32:44.879: E\/AndroidRuntime(30530):    at java.lang.reflect.Method.invoke(Method.java:511)\n06-12 00:32:44.879: E\/AndroidRuntime(30530):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)\n06-12 00:32:44.879: E\/AndroidRuntime(30530):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)\n06-12 00:32:44.879: E\/AndroidRuntime(30530):    at dalvik.system.NativeStart.main(Native Method)\n06-12 00:32:44.879: E\/AndroidRuntime(30530): Caused by: java.lang.NullPointerException\n06-12 00:32:44.879: E\/AndroidRuntime(30530):    at matthews.zack.test._testActivity.onInitialize(_testActivity.java:24)\n06-12 00:32:44.879: E\/AndroidRuntime(30530):    at matthews.zack.test._testActivity.onCreate(_testActivity.java:18)\n06-12 00:32:44.879: E\/AndroidRuntime(30530):    at android.app.Activity.performCreate(Activity.java:4465)\n06-12 00:32:44.879: E\/AndroidRuntime(30530):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)\n06-12 00:32:44.879: E\/AndroidRuntime(30530):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)\n06-12 00:32:44.879: E\/AndroidRuntime(30530):    ... 11 more\n<\/code><\/pre>\n<p>It&#8217;s a bit frustrating that android will force close without telling me the exception. I would appreciate the help. Thanks!<\/p>\n<ol>\n<li>\n<p>Here is the mistake:<\/p>\n<pre><code>save = (Button)findViewById(android.R.id.button1);\n<\/code><\/pre>\n<p>You better do something like that:<\/p>\n<pre><code>save = (Button)findViewById(R.id.button1);\n<\/code><\/pre>\n<p>And import the right R file:<\/p>\n<pre><code>import com.yournamespace.R;\n<\/code><\/pre>\n<\/li>\n<li>\n<p>You&#8217;re searching the <code>Button<\/code> using a wrong <code>id<\/code>. It should be:<\/p>\n<pre><code>save = (Button)findViewById(R.id.button1);\n<\/code><\/pre>\n<p>Also, you&#8217;re doing the same thing for the <code>EditText<\/code>. Modify it like this:<\/p>\n<pre><code>EditText firstName = (EditText)findViewById(R.id.text1);\nEditText lastName = (EditText)findViewById(R.id.text2);\n<\/code><\/pre>\n<p>The only time when you would use <code>android.R.layout.some_id_here<\/code> is when you&#8217;re using a framework defined <code>id<\/code>(for example you would use <code>android.R.id.list<\/code> for a <code>ListView<\/code> element used in a layout in a <code>ListActivity<\/code>).<\/p>\n<\/li>\n<li>\n<blockquote>\n<p>It&#8217;s a bit frustrating that android will force close without telling me the exception.<\/p>\n<\/blockquote>\n<p>It&#8217;s right there in the log, its a <code>java.lang.RuntimeException<\/code> and further says<\/p>\n<blockquote>\n<p>Caused by: java.lang.NullPointerException<\/p>\n<\/blockquote>\n<p>You are using <code>findViewById<\/code> with an <em>id<\/em> not defined in your layout. The function cannot find any view with that id and returns null. You later try to dereference such null and hence the <code>NullPointerException<\/code> exception.<\/p>\n<\/li>\n<li>\n<p><code>android.R.id.button1<\/code> should be <code>R.id.button1<\/code><\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-16 20:52:19. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Hey I just started dabbling with android programming, and from my short period of time with it I&#8217;m a bit frustrated. I can get no errors in eclipse, but when I start my app it immediately force closes. I&#8217;m following an android tutorial book verbatim, and still I&#8217;m getting errors. My aim here is to [&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-1432","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1432","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=1432"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1432\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1432"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1432"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1432"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}