Android調試筆記

來源:互聯網
上載者:User

1. findViewById返回Null

1).在另一個view的元素應該用baseView.findViewById()來拿
findViewById()是要指定view的,如果在該view下找不到,自然報null。平時注意養成寫view.findViewById()的習慣就不容易錯了。
2).findViewById在setContentView(R.layout.main);之前.
即在setContentView調用之前,調用了findViewById去找main布局中的介面元素lv_contactbook,那麼所得到的lv一定是null。正確的做法是將上面代碼中加粗的哪一行,挪至setContentView方法調用之後即可。
3).clean一下工程,讓ID重建
這種情況是調用LayoutInflater.inflate將布局xml規定的內容轉化為相應的對象。比如有rowview.xml布局檔案(比如在自訂Adapter的時候,用作ListView中的一行的內容的布局),假定在自定的Adapter的getView方法中有類似如下的代碼:
View rowview = (View)inflater.inflate(R.layout.rowview, parent, false);
TextView tv_contact_id =(TextView)rowview.findViewById(R.id.tv_contact_id);
TextView tv_contactname =(TextView)rowview.findViewById(R.id.tv_contactname);

有時候居然也會發現rowview非空,但tv_contact_id和tv_contactname都是null!仔細看代碼,怎麼也看不出錯誤來。到底是什麼原因造成的呢?答案是Eclipse造成的,要解決這個問題,需要這個項目clean一次(Project菜單 -> Clean子功能表),這樣就OK了。


2. java.lang.SecurityException: Permission denied (missing INTERNET permission?)

這是一個經典錯誤, Socket不能對外串連,錯誤不會被報出,調試的時候,能看到Exception。

原因是: 需要訪問到網路,所以,在AndroidManifest.xml中,需要進行如下配置:

//加入以下許可


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.