关于ViewPager和Fragment的错误

时间:2016-05-30 16:39:35

标签: android android-fragments android-viewpager

错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{fsy.textlayoutdemo/fsy.textlayoutdemo.activity.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class LinearLayou
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
    at android.app.ActivityThread.access$700(ActivityThread.java:159)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:176)
    at android.app.ActivityThread.main(ActivityThread.java:5419)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
    at dalvik.system.NativeStart.main(Native Method)
 Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class LinearLayou
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:470)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
    at fsy.textlayoutdemo.activity.MainActivity.initViews(MainActivity.java:38)
    at fsy.textlayoutdemo.activity.MainActivity.onCreate(MainActivity.java:23)
    at android.app.Activity.performCreate(Activity.java:5372)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349) 
    at android.app.ActivityThread.access$700(ActivityThread.java:159) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:176) 
    at android.app.ActivityThread.main(ActivityThread.java:5419) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:525) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025) 
    at dalvik.system.NativeStart.main(Native Method) 
 Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.LinearLayou" on path: /data/app/fsy.textlayoutdemo-2.apk
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:64)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
    at android.view.LayoutInflater.createView(LayoutInflater.java:565)
    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:658)
    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:675)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:700)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:470) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:398) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:354) 
    at fsy.textlayoutdemo.activity.MainActivity.initViews(MainActivity.java:38) 
    at fsy.textlayoutdemo.activity.MainActivity.onCreate(MainActivity.java:23) 
    at android.app.Activity.performCreate(Activity.java:5372) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349) 
    at android.app.ActivityThread.access$700(ActivityThread.java:159) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:176) 
    at android.app.ActivityThread.main(ActivityThread.java:5419) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:525) 

这是我的MainActivity:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initVariables();
    initViews();
    loadData();
}
private void initVariables(){


};
private void initViews(){
    View view1,view2,view3,view4;
    ViewPager viewPager;
    List<View> viewList;
    List<String> titleList;
    PagerAdapter pagerAdapter;
    viewPager = (ViewPager)findViewById(R.id.viewpager);
    LayoutInflater inflater = getLayoutInflater();
    view1 = inflater.inflate(R.layout.fragment1_layout,null);
    view2 = inflater.inflate(R.layout.fragment2_layout,null);
    view3 = inflater.inflate(R.layout.fragment3_layout,null);
    view4 = inflater.inflate(R.layout.fragment4_layout,null);

    viewList = new ArrayList<View>();
    viewList.add(view1);
    viewList.add(view2);
    viewList.add(view3);
    viewList.add(view4);

    titleList = new ArrayList<String>();
    titleList.add("个性推荐");
    titleList.add("歌单");
    titleList.add("主播电台");
    titleList.add("排行榜");


    List<android.support.v4.app.Fragment> fragments = new ArrayList<android.support.v4.app.Fragment>();
    fragments.add(new Fragment1());
    fragments.add(new Fragment2());
    fragments.add(new Fragment3());
    fragments.add(new Fragment4());
    IndexFragAdapter adapter = new IndexFragAdapter(getSupportFragmentManager(),fragments);

    viewPager = (ViewPager) findViewById(R.id.viewpager);
    viewPager.setAdapter(adapter);
    pagerAdapter = new fsy.textlayoutdemo.adapter.PagerAdapter(viewList);
    viewPager.setAdapter(pagerAdapter);



};
private void loadData(){

};
}

PagerAdapter:

public class PagerAdapter extends android.support.v4.view.PagerAdapter{
private List<View> viewList;
public PagerAdapter(List<View> viewList) {
    this.viewList = viewList;

}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
    // TODO Auto-generated method stub
    return arg0 == viewList.get((int)Integer.parseInt(arg1.toString()));
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return viewList.size();
    //返回要滑动的VIew的个数
}

@Override
public void destroyItem(ViewGroup container, int position,
                        Object object) {
    // TODO Auto-generated method stub
    container.removeView(viewList.get(position));
    //从当前container中删除指定位置(position)的View;
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
    // TODO Auto-generated method stub
    container.addView(viewList.get(position));


    return position;
}

@Override
public CharSequence getPageTitle(int position) {
    // TODO Auto-generated method stub
    switch (position) {
        case 0:
            return "个性推荐";
        case 1:
            return "歌单";
        case 2:
            return "主播电台";
        case 3:
            return "排行榜";
        default:
            return "";
    }

};


}

IndexFragAdapter:

public class IndexFragAdapter extends FragmentPagerAdapter{
private List<Fragment> mFragments;
public IndexFragAdapter(FragmentManager fm,List<Fragment> fragments) {
    super(fm);
    mFragments=fragments;
}

@Override
public Fragment getItem(int position) {
    return mFragments.get(position);
}

@Override
public int getCount() {
    return mFragments.size();
}
}

片段:

public class Fragment1 extends android.support.v4.app.Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment1_layout,container,false);

    return view;

}
}

我该如何编译项目?

1 个答案:

答案 0 :(得分:2)

 Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.LinearLayou"

您在布局资源中输入了拼写错误,其中LinearLayou代替LinearLayout