片段中的Java.lang.NoClassDefFoundError,Listview(列表适配器)以及标签页适配器中的这些片段视图

时间:2015-01-27 07:59:29

标签: android listview fragment

在自定义页面适配器的片段中实现listview时出错。

错误导致三星的错误,所以我确实修改了我的操作栏(更改为标题)和标签页适配器作为自定义的。 如你所见,我没有implements actionbar.tablistener

有错误消息:

java.lang.NoClassDefFoundError: zmnx.com.tripdocent.views.SpotListAdapter
            at zmnx.com.tripdocent.views.FeaturedListFragment.onCreateView(FeaturedListFragment.java:49)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
            at 

这次崩溃,我们只能在三星手机中看到它,如Note2(android 4.4.2)和Galaxy Nexus(android 4.2.1)。我的构建条件是Android Studio 1.0.0,SDK Build tools ver 21.1.2

我该如何解决?

并且有代码:

SpotActivity。class

public class SpotActivity extends FragmentActivity implements OnTabChangeListener, OnPageChangeListener {

    public SharedPreferences settings;

    ViewPager mViewPager;
    private ListView mListView;
    public static SpotInfo spotInfo;
    public static List<SpotInfo> spotInfoList;
    public static List<SpotInfo> nSpotInfoList;
    public static List<SpotInfo> fSpotInfoList;

    private RelativeLayout backLay;


    SpotListAdapter mAdapter;

    HandleJSON jsonHandler = null;

    CityInfo cInfo = null;
    String name = null;
    String path = null;

    String localName = null;
    String type = null;
    String hours = null;
    String price = null;
    String address = null;
    String addressDetail = null;
    String description = null;
    Bitmap bitmapImg = null;

    String tabString[] = null;

    private TabHost mTabHost = null;
    private TabPagerAdapter mPageAdapter = null;

    private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.spotact_layout);

        Intent intent = getIntent();

        cInfo = (CityInfo) intent.getSerializableExtra("city");

        Data.setCurrentCityInfo(cInfo);


        name = cInfo.getCityName();
        path = cInfo.getFilePath();

        Data.setImgPath(path);


        tabString = Constants.TAB_NAMES;

        init();

        initialiseTabHost();

        mPageAdapter =  new TabPagerAdapter(this, getSupportFragmentManager());

        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mPageAdapter);
        mViewPager.setOnPageChangeListener(SpotActivity.this);

    }


    private static void AddTab(SpotActivity  activity, TabHost tabHost, TabHost.TabSpec tabSpec) {

        tabSpec.setContent(new TabFactory(activity));

        tabHost.addTab(tabSpec);

    }

    private void init() {
        getPref();

    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId())  {

            case android.R.id.home:
                // ProjectsActivity is my 'home' activity
                onBackPressed();
                return true;

            case R.id.action_account:

                Intent i= new Intent(this, Accounts.class);

                startActivity(i);
                break;

            default :

        }

        return super.onOptionsItemSelected(item);
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();

        finish();

        Intent intent = new Intent(SpotActivity.this, City.class);
        intent.putExtra("city", cInfo);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        startActivity(intent);

    }

    private void initialiseTabHost() {
        mTabHost = (TabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup();


        SpotActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec(" ").setIndicator(" "));
        SpotActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec(" ").setIndicator(" "));

        mTabHost.setOnTabChangedListener(this);

    }



    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        int pos = this.mViewPager.getCurrentItem();
        this.mTabHost.setCurrentTab(pos);

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onTabChanged(String tabId) {
        int pos = this.mTabHost.getCurrentTab();
        this.mViewPager.setCurrentItem(pos);
    }
}

TabPagerAdapter

public class TabPagerAdapter extends FragmentPagerAdapter {

    private final int NUM_ITEMS = 2;
    private Context mContext;


    public TabPagerAdapter(Context context, FragmentManager fm) {
        super(fm);
        this.mContext = context;
    }


    @Override
    public Fragment getItem(int position) {
        FeaturedListFragment featured =  new FeaturedListFragment();
        NearbyListFragment nearbyListFragment = new NearbyListFragment();

        if (position == 0) {
            return featured;

        } else if (position == 1){
            return nearbyListFragment;
        } else {
            throw new IllegalArgumentException("Invalid page position: " + position);
        }

    }

    @Override
    public int getCount() {
        return NUM_ITEMS;
    }
}

FeaturedListFragment

public class FeaturedListFragment extends ListFragment {

    public List<SpotInfo> spotList = null;
    public static SpotListAdapter mAdapter = null;
    public ListView mListView = null;
    public static final String ARG_OBJECT = "object";
    public String imgPath = null;

    public FeaturedListFragment() {

        spotList = Data.getSpotFeaturedInfo();
        imgPath = Data.getImgPath();


    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.spot_list, container, false);

        spotList = Data.getSpotFeaturedInfo();
        imgPath = Data.getImgPath();

        mListView = (ListView) view.findViewById(android.R.id.list);

        mAdapter = new SpotListAdapter(getActivity().getBaseContext(),imgPath, spotList);
        setListAdapter(mAdapter);
       mAdapter.notifyDataSetChanged();

        return view;
    }
}

SpotListAdapter

public class SpotListAdapter extends BaseAdapter {

    Context context;
    LayoutInflater inflater;

    int cnt = 0;



    private List<SpotInfo> spotList  = null;
    private ArrayList<SpotInfo> arrayList = null;
    private String path = null;



    public SpotListAdapter(Context context, String path, List<SpotInfo> spotList) {
        this.context = context;
        this.spotList = spotList;
        this.path = path;
        this.arrayList = new ArrayList<SpotInfo>();
        this.arrayList.addAll(spotList);

        inflater = LayoutInflater.from(context);

    }

    public class ViewHolder {
        TextView spotName;
        TextView dist;
        TextView address;
        ImageView img;

    }

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

    @Override
    public Object getItem(int position) {
        return spotList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;


        if(convertView == null) {
            holder = new ViewHolder();

             convertView = inflater.inflate(R.layout.spot_row, null);
             holder.img = (ImageView) convertView.findViewById(R.id.down_thumb_img);
             holder.spotName = (TextView) convertView.findViewById(R.id.down_title_txt);

             holder.address = (TextView) convertView.findViewById(R.id.loc_txt);

            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }


        FileTotal ft = new FileTotal();

        String extStore = System.getenv("EXTERNAL_STORAGE");

        String filePath = "/Download/" + path + "-v001/images/";

        String imgCode = spotList.get(position).getCode() + ".jpg";
        String fileName = extStore + filePath  + imgCode ;

        File f = new File(fileName);
        boolean ex = ft.isFileExist(f);

        if(ex) {

            Bitmap bmp = decodeSampledBitmapFromFile(fileName, 180, 180);


            Canvas canvas = new Canvas();
            Bitmap mask = BitmapFactory.decodeResource(context.getResources(), R.drawable.list_thumbframe);
            Bitmap tempResult = Bitmap.createScaledBitmap(mask, 180, 180, true);
            mask.recycle();


            Bitmap result = Bitmap.createBitmap(180, 180, Bitmap.Config.ARGB_8888);

            canvas.setBitmap(result);

            Paint paint = new Paint();
            paint.setFilterBitmap(false);

            canvas.drawBitmap(bmp, 0, 0, paint);
            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));

            canvas.drawBitmap(tempResult, 0, 0, paint);
            paint.setXfermode(null);

            holder.img.setImageBitmap(result);
            holder.img.invalidate();
        }


        final String sName =  spotList.get(position).getName();
        final String  descr = spotList.get(position).getDesc();

        holder.spotName.setText(sName);
        holder.address.setText(descr);

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, SpotDetail.class);
                SpotInfo spotInfo = new SpotInfo();
                spotInfo = spotList.get(position);


                intent.putExtra("spot", spotInfo);
                intent.putExtra("path", path);

                context.startActivity(intent);

            }
        });

        return convertView;
    }

    public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight)
    { // BEST QUALITY MATCH

        //First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);

        // Calculate inSampleSize, Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        int inSampleSize = 1;

        if (height > reqHeight)
        {
            inSampleSize = Math.round((float)height / (float)reqHeight);
        }
        int expectedWidth = width / inSampleSize;

        if (expectedWidth > reqWidth)
        {
            //if(Math.round((float)width / (float)reqWidth) > inSampleSize) // If bigger SampSize..
            inSampleSize = Math.round((float)width / (float)reqWidth);
        }

        options.inSampleSize = inSampleSize;

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;

        return BitmapFactory.decodeFile(path, options);
    }

}

0 个答案:

没有答案