java.lang.NoClassDefFoundError(查看寻呼机)

时间:2015-12-29 09:09:29

标签: java android android-viewpager

我创建了一个ViewPager,我制作了一个适配器,然后使用(setAdapter)方法将我的适配器放入Viewpager中。

我确信每件事都没问题,但是当我运行项目时,会出现此问题:

 12-29 11:50:26.076 27016-27016/com.example.maysara_.pager E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.NoClassDefFoundError: com.example.maysara_.pager.MyAdapter
    at com.example.maysara_.pager.MainActivity.onCreate(MainActivity.java:22)
    at android.app.Activity.performCreate(Activity.java:5122)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1151)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2320)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2408)
    at android.app.ActivityThread.access$600(ActivityThread.java:167)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1378)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:194)
    at android.app.ActivityThread.main(ActivityThread.java:5405)
    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:838)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
    at dalvik.system.NativeStart.main(Native Method)

这是MainActivity

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ViewPager pager = (ViewPager)findViewById(R.id.viewPager);

    List<Fragment> list = new ArrayList<Fragment>();
    MyAdapter adapter = new MyAdapter(this.getFragmentManager(),list);

    F f1 = new F();
    Bundle b1 = new Bundle();
    b1.putInt("img", R.drawable.one);
    f1.setArguments(b1);
    list.add(f1);

    F f2 = new F ();
    Bundle b2 = new Bundle();
    b2.putInt("img", R.drawable.two);
    f2.setArguments(b2);
    list.add(f2);

    F f3  = new F();
    Bundle b3 = new Bundle();
    b3.putInt("img", R.drawable.three);
    f3.setArguments(b3);
    list.add(f3);

    F f4 = new F();
    Bundle b4 = new Bundle();
    b4.putInt("img" , R.drawable.four);
    f4.setArguments(b4);
    list.add(f4);

    F f5 = new F();
    Bundle b5 = new Bundle();
    b5.putInt("img" , R.drawable.five);
    f5.setArguments(b5);
    list.add(f5);

    pager.setAdapter(adapter);


    }
}

我的适配器代码

 package com.example.maysara_.pager;

    import android.app.Fragment;
    import android.app.FragmentManager;
    import android.support.v13.app.FragmentPagerAdapter;

    import java.util.List;

    public class MyAdapter extends FragmentPagerAdapter 
    {
    List<Fragment> list ;


    public MyAdapter(FragmentManager fm , List<Fragment> list) {
        super(fm);
        this.list=list;
    }

    @Override
    public Fragment getItem(int i) {
        return list.get(i);
    }

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

1 个答案:

答案 0 :(得分:0)

首先,在将它们分配给适配器之前定义你的片段,而不是之后。

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ViewPager pager = (ViewPager)findViewById(R.id.viewPager);

    List<Fragment> list = new ArrayList<Fragment>();

    F f1 = new F();
    Bundle b1 = new Bundle();
    b1.putInt("img", R.drawable.one);
    f1.setArguments(b1);
    list.add(f1);

    F f2 = new F ();
    Bundle b2 = new Bundle();
    b2.putInt("img", R.drawable.two);
    f2.setArguments(b2);
    list.add(f2);

    F f3  = new F();
    Bundle b3 = new Bundle();
    b3.putInt("img", R.drawable.three);
    f3.setArguments(b3);
    list.add(f3);

    F f4 = new F();
    Bundle b4 = new Bundle();
    b4.putInt("img" , R.drawable.four);
    f4.setArguments(b4);
    list.add(f4);

    F f5 = new F();
    Bundle b5 = new Bundle();
    b5.putInt("img" , R.drawable.five);
    f5.setArguments(b5);
    list.add(f5);

    MyAdapter adapter = new MyAdapter(this.getFragmentManager(),list);

    pager.setAdapter(adapter);

    }
}

关于java.lang.NoClassDefFoundError,如果您的代码只在一个模块中,请检查这是否与您的问题有关: NoClassDefFoundError with Android Studio on Android 4