第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

空指針異常 - findViewById()

空指針異常 - findViewById()

蕪湖不蕪 2019-05-28 17:56:15
空指針異常 - findViewById()任何人都可以幫我找出這個程序可能存在的問題。在該onCreate()方法中,findViewById()對所有id返回null,這會在以后導致空指針異常。我無法弄清楚為什么findViewById()找不到這個視圖。有什么建議?這是主要代碼:public class MainActivity extends Activity {     ViewPager pager;     MyPagerAdapter adapter;     LinearLayout layout1, layout2, layout3;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         layout1 = (LinearLayout) findViewById(R.id.first_View);         layout2 = (LinearLayout) findViewById(R.id.second_View);         layout3 = (LinearLayout) findViewById(R.id.third_View);         adapter = new MyPagerAdapter();         pager = (ViewPager) findViewById(R.id.main_pager);         pager.setAdapter(adapter);     }     private class MyPagerAdapter extends PagerAdapter     {         @Override         public int getCount() {              return 3;         }         @Override         public Object instantiateItem(ViewGroup collection, int position) {             LinearLayout l = null;             if (position == 0 )             {                 l = layout1;             }             if (position == 1)             {                 l = layout2;             }             if (position == 2)             {                 l = layout3;             }                 collection.addView(l, position);                 return l;         }         @Override         public boolean isViewFromObject(View view, Object object) {             return (view==object);         }          @Override          public void destroyItem(ViewGroup collection, int position, Object view) {                  collection.removeView((View) view);          }     }}
查看完整描述

4 回答

?
海綿寶寶撒

TA貢獻1809條經(jīng)驗 獲得超8個贊

findViewById()返回一個View,如果它在你提供的布局中存在setContentView(),否則返回null,這就是你發(fā)生的事情。

示例如果你setContentView(R.layout.activity_first);然后調(diào)用findViewById(R.id.first_View);它將返回一個View,這是你的布局。

但是如果你調(diào)用findViewById(R.id.second_View);它會返回,null因為你的activity_first.xml布局中沒有一個視圖被調(diào)用@+id/second_View


查看完整回答
反對 回復 2019-05-28
?
肥皂起泡泡

TA貢獻1829條經(jīng)驗 獲得超6個贊

您嘗試獲取的視圖未在activity_main布局中定義。您需要以編程方式夸大您嘗試添加到尋呼機的視圖.-

@Overridepublic Object instantiateItem(ViewGroup collection, int position) {
    LinearLayout l = null;

    if (position == 0) {
        l = (LinearLayout) View.inflate(this, R.layout.activity_first, null);
    }
    if (position == 1) {
        l = (LinearLayout) View.inflate(this, R.layout.activity_second, null);
    }
    if (position == 2) {
        l = (LinearLayout) View.inflate(this, R.layout.activity_third, null);
    }

    collection.addView(l, position);
    return l;}


查看完整回答
反對 回復 2019-05-28
  • 4 回答
  • 0 關(guān)注
  • 892 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號