java - Android Slider 中的 NoClassDefFoundError 错误

标签 java android android-fragments slider noclassdeffounderror

我正在创建一个带有 slider 的应用程序,它工作正常,然后我添加了另一个 View 并为新添加的 View 实现了 OnClickListener() ,之后当我收到以下错误时打开我的应用程序

Process: com.spintum.preexam, PID: 31905
java.lang.NoClassDefFoundError: com.spintum.preexam.CustomList$DrawerItemHolder
        at com.spintum.preexam.CustomList.getView(CustomList.java:38)
        at android.widget.AbsListView.obtainView(AbsListView.java:2338)
        at android.widget.ListView.makeAndAddView(ListView.java:1812)
        at android.widget.ListView.fillDown(ListView.java:698)
        at android.widget.ListView.fillFromTop(ListView.java:759)
        at android.widget.ListView.layoutChildren(ListView.java:1645)
        at android.widget.AbsListView.onLayout(AbsListView.java:2149)
        at android.view.View.layout(View.java:15125)
        at android.view.ViewGroup.layout(ViewGroup.java:4862)
        at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:931)
        at android.view.View.layout(View.java:15125)
        at android.view.ViewGroup.layout(ViewGroup.java:4862)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
        at android.view.View.layout(View.java:15125)
        at android.view.ViewGroup.layout(ViewGroup.java:4862)
        at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:374)
        at android.view.View.layout(View.java:15125)
        at android.view.ViewGroup.layout(ViewGroup.java:4862)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:515)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:450)
        at android.view.View.layout(View.java:15125)
        at android.view.ViewGroup.layout(ViewGroup.java:4862)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2317)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2023)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1189)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6223)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:788)
        at android.view.Choreographer.doCallbacks(Choreographer.java:591)
        at android.view.Choreographer.doFrame(Choreographer.java:560)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:774)
        at android.os.Handler.handleCallback(Handler.java:808)
        at android.os.Handler.dispatchMessage(Handler.java:103)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:5292)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
        at dalvik.system.NativeStart.main(Native Method)

logcat显示NoClassDefFound错误的类如下

public class CustomList extends ArrayAdapter<DrawerItem> {

  Context context;
  List<DrawerItem> drawerItemList;
  int layoutResID;

  public CustomList(Context context, int layoutResourceID,
              List<DrawerItem> listItems) {
        super(context, layoutResourceID, listItems);
        this.context = context;
        this.drawerItemList = listItems;
        this.layoutResID = layoutResourceID;

  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        DrawerItemHolder drawerHolder;
        View view = convertView;
         if (view == null) {
              LayoutInflater inflater = ((Activity) context).getLayoutInflater();
              drawerHolder = new DrawerItemHolder();
              view = inflater.inflate(layoutResID, parent, false);
              drawerHolder.ItemName = (TextView) view.findViewById(R.id.drawer_item_text);
              drawerHolder.icon = (ImageView) view.findViewById(R.id.drawer_item_icon);
              view.setTag(drawerHolder);
         } else {
              drawerHolder = (DrawerItemHolder) view.getTag();
         }
         DrawerItem dItem = (DrawerItem) this.drawerItemList.get(position);
        drawerHolder.icon.setImageDrawable(view.getResources().getDrawable(
                    dItem.getImgResID()));
        drawerHolder.ItemName.setText(dItem.getItemName());
         return view;
  }

  private static class DrawerItemHolder {
        TextView ItemName;
        ImageView icon;
    }
}

错误显示在 drawerHolder = new DrawerItemHolder(); 行中。 我没有对此类进行任何更改,因此我认为问题可能是由我在充当 fragment 容器的原始 Activity 中添加的额外 View 引起的。该 Activity 的代码如下

public class Home extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mPlanetTitles;
private static int pos;
List<DrawerItem> dataList = new ArrayList<DrawerItem>();
View statistics,test,syllabus,share;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    this.getActionBar().setBackgroundDrawable(
            getResources().getDrawable(R.color.background));
    this.overridePendingTransition(R.layout.fade_in, R.layout.fade_out);
    mTitle = mDrawerTitle = getTitle();
    mPlanetTitles = getResources().getStringArray(R.array.planets_array);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);
    statistics=findViewById(R.id.statistics_container);
    test=findViewById(R.id.test_container);
    syllabus=findViewById(R.id.syllabus_container);
    share=findViewById(R.id.share_container);
    statistics.setOnClickListener(mBottomDrawerListener);
    syllabus.setOnClickListener(mBottomDrawerListener);
    test.setOnClickListener(mBottomDrawerListener);
    share.setOnClickListener(mBottomDrawerListener);
    TextView image=(TextView)findViewById(R.id.statistics_image);
    TextView text=(TextView)findViewById(R.id.statistics);
    image.setTextColor(Color.parseColor("#03a9f4"));
    text.setTextColor(Color.parseColor("#03a9f4"));

    // set a custom shadow that overlays the main content when the drawer opens
    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,GravityCompat.START);
    int width = getResources().getDisplayMetrics().widthPixels;
    DrawerLayout.LayoutParams params = (android.support.v4.widget.DrawerLayout.LayoutParams) mDrawerList.getLayoutParams();
    //params.width = (width * 75) / 100;
    mDrawerList.setLayoutParams(params);
    // set up the drawer's list view with items and click listener
    /*
     * mDrawerList.setAdapter(new ArrayAdapter<String>(this,
     * R.layout.list_item, mPlanetTitles));
     */
    dataList.add(new DrawerItem("Home", R.drawable.home));
    dataList.add(new DrawerItem("Intelligence Questionnaire",
            R.drawable.bulb));
    dataList.add(new DrawerItem("Change Country", R.drawable.location));
    dataList.add(new DrawerItem("LeaderBoard", R.drawable.cup));
    dataList.add(new DrawerItem("How to play", R.drawable.help));
    dataList.add(new DrawerItem("Exam Tips", R.drawable.tip));
    mDrawerList.setAdapter(new CustomList(Home.this, R.layout.list_item,dataList));
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
    // enable ActionBar app icon to behave as action to toggle nav drawer
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);
    // ActionBarDrawerToggle ties together the the proper interactions
    // between the sliding drawer and the action bar app icon
    mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */ mDrawerLayout, /* DrawerLayout object */R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */R.string.drawer_open, /* "open drawer" description for accessibility */
    R.string.drawer_close /* "close drawer" description for accessibility */) {
        public void onDrawerClosed(View view) {
            getActionBar().setTitle(mTitle);
            invalidateOptionsMenu(); // creates call to
                                        // onPrepareOptionsMenu()
        }
        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle(mDrawerTitle);
            invalidateOptionsMenu(); // creates call to
                                        // onPrepareOptionsMenu()
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);
    if (savedInstanceState == null) {
        selectItem(0);
    }
    Typeface iconfont=FontManager.getTypeface(getApplicationContext(),FontManager.FONTAWESOME);
    FontManager.markAsIconContainer(findViewById(R.id.btm_nav_bar),iconfont);
}
public View.OnClickListener mBottomDrawerListener=new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        FragmentManager fragmentManager=getFragmentManager();
        setBlackText(v);
        TextView image,text;
        switch (v.getId()){
            case R.id.statistics_container:
                fragmentManager.beginTransaction().replace(R.id.content_frame,new HomePage()).commit();
                image=(TextView)v.findViewById(R.id.statistics_image);
                text=(TextView)v.findViewById(R.id.statistics);
                image.setTextColor(Color.parseColor("#03a9f4"));
                text.setTextColor(Color.parseColor("#03a9f4"));
                break;
            case R.id.syllabus_container:
                image=(TextView)v.findViewById(R.id.syllabus_image);
                text=(TextView)v.findViewById(R.id.syllabus_btm);
                image.setTextColor(Color.parseColor("#03a9f4"));
                text.setTextColor(Color.parseColor("#03a9f4"));
                break;
            case R.id.test_container:
                image=(TextView)v.findViewById(R.id.test_image);
                text=(TextView)v.findViewById(R.id.Tests);
                image.setTextColor(Color.parseColor("#03a9f4"));
                text.setTextColor(Color.parseColor("#03a9f4"));
                break;
            case R.id.share_container:
                image=(TextView)v.findViewById(R.id.share_image);
                text=(TextView)v.findViewById(R.id.share_btm);
                image.setTextColor(Color.parseColor("#03a9f4"));
                text.setTextColor(Color.parseColor("#03a9f4"));
                break;

        }
}
};
public void setBlackText(View v){
    TextView img[]=new TextView[4];
    TextView t1,t2,t3,t4;
    img[0]=(TextView)findViewById(R.id.statistics_image);
    t4=(TextView)findViewById(R.id.statistics);
    img[1]=(TextView)findViewById(R.id.test_image);
    t1=(TextView)findViewById(R.id.Tests);
    img[2]=(TextView)findViewById(R.id.syllabus_image);
    t2=(TextView)findViewById(R.id.syllabus_btm);
    img[3]=(TextView)findViewById(R.id.share_image);
    t3=(TextView)findViewById(R.id.share_btm);
    for(TextView singleTextView:img){
        singleTextView.setTextColor(Color.parseColor("#000000"));
    }
    t1.setTextColor(Color.parseColor("#000000"));
    t2.setTextColor(Color.parseColor("#000000"));
    t3.setTextColor(Color.parseColor("#000000"));
    t4.setTextColor(Color.parseColor("#000000"));
}
/*
 * @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater
 * inflater = getMenuInflater(); inflater.inflate(R.menu.activity_main,
 * menu); return super.onCreateOptionsMenu(menu); }
 * 
 * Called whenever we call invalidateOptionsMenu()
 * 
 * @Override public boolean onPrepareOptionsMenu(Menu menu) { // If the nav
 * drawer is open, hide action items related to the content view boolean
 * drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
 * //menu.findItem(R.id.action_websearch).setVisible(!drawerOpen); return
 * super.onPrepareOptionsMenu(menu); }
 */

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // The action bar home/up action should open or close the drawer.
    // ActionBarDrawerToggle will take care of this.
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    // Handle action buttons
    switch (item.getItemId()) {
    case 1:
        // create intent to perform web search for this planet
        Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
        intent.putExtra(SearchManager.QUERY, getActionBar().getTitle());
        // catch event that there's no activity to handle intent
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        } else {
            Toast.makeText(this, R.string.app_not_available,
                    Toast.LENGTH_LONG).show();
        }
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

/* The click listner for ListView in the navigation drawer */
private class DrawerItemClickListener implements
        ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        selectItem(position);
    }
}

private void selectItem(int position) {
    // update the main content by replacing fragments
    /*
     * Fragment fragment = new HomePage(); Bundle args = new Bundle();
     * args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
     * fragment.setArguments(args);
     */
    pos = position;
    FragmentManager fragmentManager = getFragmentManager();
    switch (pos) {
    case 0:
        fragmentManager.beginTransaction().replace(R.id.content_frame, new Fragment_Statistics()).commit();
        break;
    case 1:
        break;
    case 2:
        fragmentManager.beginTransaction().replace(R.id.content_frame, new Country()).commit();
    case 3:break;
    case 4:break;
    case 5:break;
    }
    // update selected item and title, then close the drawer
    mDrawerList.setItemChecked(position, true);
    setTitle(mPlanetTitles[position]);
    mDrawerLayout.closeDrawer(mDrawerList);
}

@Override
public void setTitle(CharSequence title) {
    mTitle = title;
    getActionBar().setTitle(mTitle);
}

/**
 * When using the ActionBarDrawerToggle, you must call it during
 * onPostCreate() and onConfigurationChanged()...
 */

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Pass any configuration change to the drawer toggls
    mDrawerToggle.onConfigurationChanged(newConfig);
}
}

Home Activity 中使用的 DrawerItem 类如下所示

public class DrawerItem {

String ItemName;
int imgResID;

public DrawerItem(String itemName, int imgResID) {
      super();
      ItemName = itemName;
      this.imgResID = imgResID;
}

public String getItemName() {
      return ItemName;
}
public void setItemName(String itemName) {
      ItemName = itemName;
}
public int getImgResID() {
      return imgResID;
}
public void setImgResID(int imgResID) {
      this.imgResID = imgResID;
}}

有人能看出我哪里出了问题吗?提前致谢..

最佳答案

DrawerItemHolder 类应该是公共(public)的,或者至少不应该是私有(private)的。

关于java - Android Slider 中的 NoClassDefFoundError 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38081777/

相关文章:

java - 如何修复 “Error: Can not find or load the main class”?

java - 为什么 Android 解析 XML 不正确?

android - 如何在 fragment 中使用 setSupportActionBar

android - 无法启动 Activity ComponentInfo{Activity} : android. view.InflateException: Binary XML file line Error inflating class fragment

java - 测试应用程序服务器响应

java - 在 String 中的特定位置查找模式并将其替换为 Java 中的其他内容

java - 考虑到 sychronized 关键字的成本,有哪些技巧可以使延迟初始化线程安全且高效?

Android CameraX 检索摄像机配置文件参数时出错

java - 如何使用 Android Studio 删除部分 android 包名称?

android - 如何为具有多个 fragment 的 Activity 弹出堆栈?