我对这个错误感到很困惑。我希望有人可以帮助我。这是我的错误:
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;
}
}
我尝试了很多方法,但都失败了。比如改变break的位置和改变position view group。我忘了导入库还是什么?请在这里帮助它的主人:) 如果有人能帮助我,我将不胜感激。谢谢之前:)