0

我想在FrameLayout中設置Activity。這裏是我的代碼:android中的layoutinflater nullpointerexception

FrameLayout fl = new FrameLayout(this); 
fl = (FrameLayout) findViewById(R.id.actioncontent);   
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);   
View myview =LayoutInflater.from(this).inflate(R.layout.wallpaper, null); 
fl.removeAllViews(); 
fl.addView(myview); 

我得到一個錯誤NullPointerException異常

+0

視圖MyView的= LayoutInflater.from(本).inflate(R.layout.wallpaper,NULL);將其更改爲查看myview = inflater.inflate(R.layout.wallpaper,null); –

+0

謝謝你,我知道了我解決了這個錯誤現在我有另一個問題,我想在這個框架佈局和活動名稱顯示一個活動是壁紙我如何顯示此框架佈局 –

回答

0

變更

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
0

你沒有說明'這個'是什麼,或者NPE在哪裏發生 - 但可能是'this'提供了錯誤的上下文。 請嘗試使用您的活動名稱嗎?例如,MainActivity.this

而且,我下面指出,替代從View myview =LayoutInflater.from(this).inflateView myview = inflater.inflate

1

您需要使用inflator insteed的LayoutInflater.from(this)

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

      View myview =LayoutInflater.from(this).inflate(R.layout.wallpaper, null); 
      fl.removeAllViews(); 
       fl.addView(myview); 

替換爲上面的代碼下面

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

      View myview =inflater .inflate(R.layout.wallpaper, null); 
      fl.removeAllViews(); 
       fl.addView(myview);