2013-08-30 62 views
0

我對這個錯誤非常抱歉。我希望有人能夠幫助我。 這是我的錯誤:Android中出現NullPOinterException錯誤,請幫忙

08-30 00:24:29.349: E/AndroidRuntime(2139):  at android.view.ViewGroup.addView(ViewGroup.java:3318) 

,這是我的代碼:

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Context; 
import android.support.v4.view.PagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.LinearLayout; 

public class MainActivity extends Activity { 
ViewPager vPager; 
private vpAdapter mAdapter; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    vPager=(ViewPager)findViewById(R.id.pager); 
    mAdapter = new vpAdapter(); 
    vPager.setAdapter(mAdapter); 
} 
    private class vpAdapter extends PagerAdapter{ 

@Override 
public int getCount() { 
    // TODO Auto-generated method stub 
    return 4; 
} 

@Override 
public boolean isViewFromObject(View view, Object object) { 
    // TODO Auto-generated method stub 
    return view==((LinearLayout)object); 
} 

@Override 
public Object instantiateItem(ViewGroup container, int position) { 
    // TODO Auto-generated method stub 
    LayoutInflater inflater =(LayoutInflater)container.getContext() 
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View v = null; 
    switch(position){ 
    case 0: 
     inflater.inflate(R.layout.satu, null); 
     break; 
    case 1: 
     inflater.inflate(R.layout.dua, null); 
     break; 
    case 2: 
     inflater.inflate(R.layout.tiga, null); 
     break; 
    case 3: 
     inflater.inflate(R.layout.empat, null); 
     break; 
    } 
((ViewPager)container).addView(v,0); 
return true;   
} 

} 

我已經嘗試了許多方法,但失敗了。如更改中斷的位置並更改位置視圖組。我忘了導入一個圖書館嗎?請幫助它的主人在這裏:) 如果有人能幫助我,我將非常感激。 謝謝之前:)

+0

添加完整日誌跟蹤 –

回答

3

查看爲空。

你有這樣的

View v = null; 

但我沒有看到你初始化以充氣佈局的任何地方。您只需佈局佈局。因此你得到NullPointerException。在交換機初始化的情況下您的看法V和也投它來查看

v = (View) inflater.inflate(R.layout.rowimage,null, false); 

而且返回

return v; // and not return true; 
+1

因此,在你所有'case 0'到'case 3'使用'v = inflater ...'在這個答案中! –

+0

所以,在null之後,我不得不添加一個「false」語句? –

+0

@AndiAlif你爲什麼需要假。你有休息,你應該返回視圖'V'不正確 – Raghunandan

0

它返回空指針異常,因爲這裏的「V」爲空。開關狀態更改爲以下,

switch(position){ 
     case 0: 
      v= inflater.inflate(R.layout.satu, null); 
      break; 
     case 1: 
      v= inflater.inflate(R.layout.dua, null); 
      break; 
     case 2: 
      v=inflater.inflate(R.layout.tiga, null); 
      break; 
     case 3: 
      v=inflater.inflate(R.layout.empat, null); 
      break; 
     } 
0

嘗試這種方式

@Override 
public Object instantiateItem(ViewGroup container, int position) { 
    // TODO Auto-generated method stub 
    LayoutInflater inflater =(LayoutInflater)container.getContext() 
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View v = null; 
    switch(position){ 
    case 0: 
     v=inflater.inflate(R.layout.satu, null); 
     break; 
    case 1: 
     v=inflater.inflate(R.layout.dua, null); 
     break; 
    case 2: 
     v=inflater.inflate(R.layout.tiga, null); 
     break; 
    case 3: 
     v=inflater.inflate(R.layout.empat, null); 
     break; 
    } 

return v;   
} 
+0

謝謝,我會嘗試編碼:) –

0

你的觀點是空值

會人爲增加XML佈局,但不認爲分配英寸您的開關 -

分配觀 -

v=(View)inflater.inflate(R.layout.satu, null); 

,並返回視圖NOT布爾值 -

return v; 
相關問題