{"id":1415,"date":"2022-08-30T15:16:20","date_gmt":"2022-08-30T15:16:20","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/16\/android-2-3-3-crash-when-getting-a-linearlayout-from-linearlayoutfindviewbyidr-id-linear_layout_id-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:16:20","modified_gmt":"2022-08-30T15:16:20","slug":"android-2-3-3-crash-when-getting-a-linearlayout-from-linearlayoutfindviewbyidr-id-linear_layout_id-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/android-2-3-3-crash-when-getting-a-linearlayout-from-linearlayoutfindviewbyidr-id-linear_layout_id-collection-of-common-programming-errors\/","title":{"rendered":"Android 2.3.3: Crash when getting a LinearLayout from (LinearLayout)findViewById(R.id.&lt;linear_layout_id&gt;)-Collection of common programming errors"},"content":{"rendered":"<p>I am programming a twitter app. I am making a search bar for usernames\/hashtags, and I am having some trouble adding views generated from XML to the currently existing LinearLayout.<\/p>\n<p>here is Search.java<\/p>\n<pre><code>public class Search extends Activity{\n\nString searchType = \"username\";\n\npublic void onCreate(Bundle savedInstanceState) {\n    super.onCreate(savedInstanceState);\n    setContentView(R.layout.activity_search);\n\n    EditText t = (EditText)findViewById(R.id.search_content);\n    t.setOnClickListener(onClickSearch);\n    t.setWidth(225);\n\n    Button b = (Button)findViewById(R.id.search_button_search);\n    b.setOnClickListener(onClickButton);\n}\n\n@Override\npublic boolean onCreateOptionsMenu(Menu menu) {\n    getMenuInflater().inflate(R.menu.activity_search, menu);\n    return true;\n}\n\nOnClickListener onClickSearch = new OnClickListener(){\n\n    public void onClick(View v) {\n        EditText searchContent = (EditText)findViewById(R.id.search_content);\n        EditText clickedContent = (EditText)v;\n        if((EditText)findViewById(R.id.search_content) == (EditText)v){\n            EditText t = (EditText)v;\n            t.setText(\"\");\n        }\n\n        \/\/EditText searchContent = (EditText)findViewById(R.id.search_content);\n        \/\/EditText clickedContent = (EditText)v;\n\n        \/*if((Button)v == (Button)findViewById(R.id.search_button_type)){\n            EditText t = (EditText)findViewById(R.id.search_content);\n            Search.this.searchByUserName(t.getText().toString());\n        }\n\n        if((Button)v == (Button)findViewById(R.id.search_button_search)){\n\n        }*\/\n    }\n\n};\n\nOnClickListener onClickButton = new OnClickListener(){\n\n    public void onClick(View v) {\n        \/\/if((Button)v == (Button)findViewById(R.id.search_button_search)){\n            EditText t = (EditText)findViewById(R.id.search_content);\n            Search s = new Search();\n            s.searchByUserName(t.getText().toString());\n        \/\/}\n\n        \/\/if((Button)v == (Button)findViewById(R.id.search_button_search)){\n\n        \/\/}\n    }\n\n};\n\npublic void searchByUserName(String userName){\n    \/\/System.out.println(\"beginning-&gt;searchByUserName\");\n    TwitterHandler handler = new TwitterHandler();\n    List views = handler.queryUserName(userName, this);\n\n    LinearLayout ll = (LinearLayout)findViewById(R.id.search_panel);\n    for(int i = 0; i &lt; views.size(); i++){\n        ll.addView(views.get(i));\n    }\n}\n<\/code><\/pre>\n<p>}<\/p>\n<p>here is the XML<\/p>\n<pre><code>\n    \n\n    \n    \n\n    \n\n<\/code><\/pre>\n<p>My LogCat is telling me there is a NullPointerException, the process is forced to quit on the android virtual device. Please Help!!!<\/p>\n<p>Here is the TwitterHandler<\/p>\n<pre><code>public class TwitterHandler extends DefaultHandler{\nprivate final String USER_SEARCH = \"https:\/\/api.twitter.com\/1\/users\/lookup.xml?screen_name=\";\n\npublic List queryUserName(String userName, Context context){\n    System.out.println(\"beginning-&gt;queryUserName\");\n    String url = USER_SEARCH + userName;\n    \/\/ListView view = new ListView(context);\n    List views = new ArrayList();\n    Document doc = null;\n    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();\n    try {\n        System.out.println(\"in try statement-&gt;queryUserName\");\n        DocumentBuilder db = dbf.newDocumentBuilder();\n\n        InputSource is = new InputSource();\n        is.setCharacterStream(new StringReader(url));\n        doc = db.parse(is); \n\n        NodeList nl = doc.getChildNodes();\n\n        for(int i = 0; i &lt; nl.getLength(); i++){\n            Element e = (Element)nl.item(i);\n            TextView t = new TextView(context);\n\n            String name = \"\";\n            String screenName = \"\";\n            Bitmap bitmap = null;\n\n            Node n = (Node)e.getChild(\"name\");\n            name = n.getTextContent();\n            t.setText(name);\n            views.add(t);\n\n            t = new TextView(context);\n            n = (Node)e.getChild(\"screen_name\");\n            screenName = n.getTextContent();\n            t.setText(screenName);\n            views.add(t);\n\n            n = (Node)e.getChild(\"profile_image_url\");\n            bitmap = getBitmap(n.getTextContent());\n            ImageView imageView = new ImageView(context);\n            imageView.setImageBitmap(bitmap);\n            views.add(imageView);\n\n        }\n\n    }catch(Exception e){ }\n    return views;\n}\n\nprivate Bitmap getBitmap(String imageUrl) throws IOException{\nInputStream inputStream = null;\n URL u = new URL(imageUrl);\n URLConnection conn = u.openConnection();\n\n try{\n      HttpURLConnection httpConn = (HttpURLConnection)conn;\n      httpConn.setRequestMethod(\"GET\");\n      httpConn.connect();\n\n      if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {\n          inputStream = httpConn.getInputStream();\n      }\n }\n catch (Exception ex) { }\n\nreturn BitmapFactory.decodeStream(inputStream);\n\n}\n\n}\n<\/code><\/pre>\n<p>Here is the Log<\/p>\n<pre><code>07-26 17:00:24.523: D\/AndroidRuntime(335): Shutting down VM\n07-26 17:00:24.523: W\/dalvikvm(335): threadid=1: thread exiting with uncaught exception     (group=0x40015560)\n07-26 17:00:24.533: E\/AndroidRuntime(335): FATAL EXCEPTION: main\n07-26 17:00:24.533: E\/AndroidRuntime(335): java.lang.ClassCastException:   java.util.AbstractList$SubAbstractListRandomAccess\n07-26 17:00:24.533: E\/AndroidRuntime(335):  at    com.example.twitter.viewer.Search.searchByUserName(Search.java:84)\n07-26 17:00:24.533: E\/AndroidRuntime(335):  at com.example.twitter.viewer.Search$2.onClick(Search.java:71)\n07-26 17:00:24.533: E\/AndroidRuntime(335):  at android.view.View.performClick(View.java:2485)\n07-26 17:00:24.533: E\/AndroidRuntime(335):  at android.view.View$PerformClick.run(View.java:9080)\n07-26 17:00:24.533: E\/AndroidRuntime(335):  at android.os.Handler.handleCallback(Handler.java:587)\n07-26 17:00:24.533: E\/AndroidRuntime(335):  at android.os.Handler.dispatchMessage(Handler.java:92)\n07-26 17:00:24.533: E\/AndroidRuntime(335):  at android.os.Looper.loop(Looper.java:123)\n07-26 17:00:24.533: E\/AndroidRuntime(335):  at android.app.ActivityThread.main(ActivityThread.java:3683)\n07-26 17:00:24.533: E\/AndroidRuntime(335):  at java.lang.reflect.Method.invokeNative(Native Method)\n07-26 17:00:24.533: E\/AndroidRuntime(335):  at java.lang.reflect.Method.invoke(Method.java:507)\n07-26 17:00:24.533: E\/AndroidRuntime(335):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)\n07-26 17:00:24.533: E\/AndroidRuntime(335):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)\n07-26 17:00:24.533: E\/AndroidRuntime(335):  at dalvik.system.NativeStart.main(Native Method)\n<\/code><\/pre>\n<ol>\n<li>\n<p>You are trying to make a <strong>new Instance Object<\/strong> of your <code>Search Activity<\/code>. In which your <code>contentView<\/code> is not available, so you can not define <code>LinearLayout<\/code> at <code>searchByUserName<\/code> method.<\/p>\n<p>Instead just simple use method name..<\/p>\n<p>So Replace these lines,<\/p>\n<pre><code>Search s = new Search();\ns.searchByUserName(t.getText().toString());\n<\/code><\/pre>\n<p>with only,<\/p>\n<pre><code>searchByUserName(t.getText().toString());\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-16 20:49:07. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I am programming a twitter app. I am making a search bar for usernames\/hashtags, and I am having some trouble adding views generated from XML to the currently existing LinearLayout. here is Search.java public class Search extends Activity{ String searchType = &#8220;username&#8221;; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_search); EditText t = (EditText)findViewById(R.id.search_content); t.setOnClickListener(onClickSearch); t.setWidth(225); [&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-1415","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1415","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=1415"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1415\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1415"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1415"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1415"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}