2013-08-30 56 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; 
相关问题