在片段中调用接口方法会抛出ClassCastException

时间:2016-01-31 09:47:33

标签: java android listview android-fragments fragment

这是我的代码中有问题的一行:

listAdapter.setCustomButtonListener((CustomButtonListener) getActivity());**

此侦听器在AppCompatActivity内完美运行,但当我尝试在片段内使用它时会抛出ClassCastException。这是logcat:

Logcat

FATAL EXCEPTION: main  Process: info.tranetech.laundry, PID: 21707
                                                                        java.lang.ClassCastException: info.tranetech.laundry.pricelist.ItemListTab cannot be cast to tranetech.laundry.pricelist.CustomButtonListener
                                                                            at info.tranetech.laundry.pricelist.ManItems$GetContacts.onPostExecute(ManItems.java:177)
                                                                            at info.tranetech.laundry.pricelist.ManItems$GetContacts.onPostExecute(ManItems.java:98)
                                                                            at android.os.AsyncTask.finish(AsyncTask.java:632)
                                                                            at android.os.AsyncTask.access$600(AsyncTask.java:177)
                                                                            at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                            at android.os.Looper.loop(Looper.java:146)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5748)
                                                                            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:1291)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
                                                                            at dalvik.system.NativeStart.main(Native Method)

这是我的界面

CustomButtonListener.java

public interface CustomButtonListener {
    public void onButtonClickListener(int position, EditText editText, int value);
}

这是我的TabBar(AppCompatActivity)

ItemListTab.java

public class ItemListTab extends AppCompatActivity {

    private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private int[] tabIcons = {
            R.drawable.ic_tab_favourite,
            R.drawable.ic_tab_call,
            R.drawable.ic_tab_contacts
    };

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

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
        setupTabIcons();
    }

    private void setupTabIcons() {
        tabLayout.getTabAt(0).setIcon(tabIcons[0]);
        tabLayout.getTabAt(1).setIcon(tabIcons[1]);
        tabLayout.getTabAt(2).setIcon(tabIcons[2]);
        tabLayout.getTabAt(3).setIcon(tabIcons[0]);
    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFrag(new ManItems(), "");
        adapter.addFrag(new WomanItems(), "");
        adapter.addFrag(new KidItems(), "");
        adapter.addFrag(new HouseHoldItems(), "");
        viewPager.setAdapter(adapter);
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

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

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

        public void addFrag(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }
}

这是我的片段,我称之为

listAdapter.setCustomButtonListener((CustomButtonListener)getActivity());

ManItems.java

   public class ManItems extends Fragment implements CustomButtonListener{

    private ListView listView;
    View rootView;
    Context context;
    private ListAdapter listAdapter;
    public static String[] ItemName, prices,ItemImage;
    Button PlaceOrder;
    ProgressDialog pDialog;
    HashMap<String, String> itemMap;
    // Hashmap for ListView
    ArrayList<HashMap<String, String>> ItemList = new ArrayList<HashMap<String, String>>();


    public ManItems() {
        // Required empty public constructor
    }

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        rootView = inflater.inflate(R.layout.content_main, container, false);

        listView = (ListView) rootView.findViewById(R.id.customListView);
        PlaceOrder = (Button) rootView.findViewById(R.id.place_order);

        // Calling async task to get json
        new GetContacts().execute();


        PlaceOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (ListAdapter.map.isEmpty()) {
                    Toast.makeText(getActivity(), "Please select items", Toast.LENGTH_SHORT).show();
                } else {
                    ListAdapter.map.values().removeAll(Collections.singleton("0"));
                    Intent intent = new Intent(getActivity(), OrderList.class);
                    startActivity(intent);
                }
            }
        });

        return  rootView;
    }

    @Override
    public void onButtonClickListener(int position, EditText editText, int value) {
        //  View view = listView.getChildAt(position);
        int quantity = Integer.parseInt(editText.getText().toString());

        quantity = quantity + 1 * value;
        if (quantity < 0)
            quantity = 0;
        editText.setText(quantity + "");

    }

    /**
     * Async task class to get json by making HTTP call
     */
    private class GetContacts extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Showing progress dialog
            pDialog = new ProgressDialog(getActivity());
            pDialog.setMessage("Please wait...");
            pDialog.setCancelable(false);
            pDialog.show();

        }

        @Override
        protected Void doInBackground(Void... arg0) {
            // Creating service handler class instance
            JSONParser JP = new JSONParser();

            // Making a request to url and getting response
            String jsonStr = JP.makeServiceCall("http://openspace.tranetech.com/mis/Laundry/items.php", JSONParser.GET);

            Log.d("Response: ", "> " + jsonStr);

            if (jsonStr != null) {
                try {
                    JSONObject jsonObj = new JSONObject(jsonStr);

                    // Getting JSON Array node
                    JSONArray itmeArray = jsonObj.getJSONArray("Data");

                    ItemName = new String[itmeArray.length()];
                    prices = new String[itmeArray.length()];
                    ItemImage = new String[itmeArray.length()];

                    // looping through All Contacts
                    for (int i = 0; i < itmeArray.length(); i++) {
                        JSONObject item = itmeArray.getJSONObject(i);

                        String price = item.getString("price").toString();
                        String name = item.getString("item_name").toString();
                        String image = item.getString("image").toString();

                        ItemName[i] = name;
                        prices[i] = price;
                        ItemImage[i] =image;

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Log.e("ServiceHandler", "Couldn't get any data from the url");
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            // Dismiss the progress dialog
            if (pDialog.isShowing()) {

                for (int z = 0; z < ItemName.length; z++) {

                    Log.d("Name  ", "" + ItemName[z]);
                    Log.d("Price ", "" + prices[z]);
                    Log.d("Images ", "" + ItemImage[z]);
                }
                pDialog.dismiss();

            }
//            images = getResources().obtainTypedArray(R.array.ProductImages);

            /**
             * Updating parsed JSON data into ListView
             * */
            listAdapter = new ListAdapter(getActivity(), ItemName, ItemImage, prices);
            listView.setAdapter(listAdapter);
            listAdapter.setCustomButtonListener((CustomButtonListener) getActivity());

        }
    }


}

更改此 listAdapter.setCustomButtonListener(ManItems.this);

logcat的

     java.lang.NullPointerException
                                                                            at info.tranetech.laundry.pricelist.ListAdapter.getView(ListAdapter.java:95)
                                                                            at android.widget.AbsListView.obtainView(AbsListView.java:2768)
                                                                            at android.widget.ListView.makeAndAddView(ListView.java:1817)
                                                                            at android.widget.ListView.fillDown(ListView.java:703)
                                                                            at android.widget.ListView.fillFromTop(ListView.java:769)
                                                                            at android.widget.ListView.layoutChildren(ListView.java:1647)
                                                                            at android.widget.AbsListView.onLayout(AbsListView.java:2586)
                                                                            at android.view.View.layout(View.java:15903)
                                                                            at android.view.ViewGroup.layout(ViewGroup.java:4932)
                                                                            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1692)
                                                                            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1534)
                                                                            at android.widget.LinearLayout.onLayout(LinearLayout.java:1443)
                                                                            at android.view.View.layout(View.java:15903)
                                                                            at android.view.ViewGroup.layout(ViewGroup.java:4932)
                                                                            at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1627)
                                                                            at android.view.View.layout(View.java:15903)
                                                                            at android.view.ViewGroup.layout(ViewGroup.java:4932)
                                                                            at android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1034)
                                                                            at android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:744)
                                                                            at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
                                                                            at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1180)
                                                                            at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:757)
                                                                            at android.view.View.layout(View.java:15903)
                                                                            at android.view.ViewGroup.layout(ViewGroup.java:4932)
                                                                            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
                                                                            at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
                                                                            at android.view.View.layout(View.java:15903)
                                                                            at android.view.ViewGroup.layout(ViewGroup.java:4932)
                                                                            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1692)
                                                                            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1534)
                                                                            at android.widget.LinearLayout.onLayout(LinearLayout.java:1443)
                                                                            at android.view.View.layout(View.java:15903)
                                                                            at android.view.ViewGroup.layout(ViewGroup.java:4932)
                                                                            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
                                                                            at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
                                                                            at android.view.View.layout(View.java:15903)
                                                                            at android.view.ViewGroup.layout(ViewGroup.java:4932)
                                                                            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1692)
                                                                            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1534)
                                                                            at android.widget.LinearLayout.onLayout(LinearLayout.java:1443)
                                                                            at android.view.View.layout(View.java:15903)
                                                                            at android.view.ViewGroup.layout(ViewGroup.java:4932)
                                                                            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
                                                                            at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
                                                                            at android.view.View.layout(View.java:15903)
                                                                            at android.view.ViewGroup.layout(ViewGroup.java:4932)
                                                                            at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2418)
                                                                            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2133)
                                                                            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1297)
                                                                            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6773)
                                                                            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:813)
                                                                            at android.view.Choreographer.doCallbacks(Choreographer.java:613)
                                                                            at android.view.Choreographer.doFrame(Choreographer.java:583)
                                                                            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:799)
                                                                            at android.os.Handler.handleCallback(Handler.java:733)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                            at android.os.Looper.loop(Looper.java:146)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5748)
                                                                            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:1291)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)

  [1]: https://www.vlemonn.com/Blog/Android/Android-Custom-ListView-with-ImageView-EditText-and-Button/

错误位置

for (int z = 0; z < MainActivity_items.ItemImage.length; z++) {

            Picasso.with(context)
                    .load(""+MainActivity_items.ItemImage[position])
                    .into(listViewHolder.ivProduct);
        }

ListAdapter.java

public class ListAdapter extends BaseAdapter {

    public ArrayList<Integer> quantity = new ArrayList<Integer>();
    public ArrayList<Integer> price = new ArrayList<Integer>();
    private String[] listViewItems, prices, static_price;
    String[] images;
    View row = null;

    static String get_price, get_quntity;
    int g_quntity, g_price, g_minus;

    private Context context;
    CustomButtonListener customButtonListener;

    static HashMap<String, Integer> map = new HashMap<>();


    public ListAdapter(Context context, String[] listViewItems, String[] images, String[] prices) {
        this.context = context;
        this.listViewItems = listViewItems;
        this.images = images;
        this.prices = prices;

        for (int i = 0; i < listViewItems.length; i++) {
            quantity.add(0);
        }
    }

    public void setCustomButtonListener(CustomButtonListener customButtonListner) {
        this.customButtonListener = customButtonListner;
    }

    @Override
    public int getCount() {
        return listViewItems.length;
    }

    @Override
    public String getItem(int position) {
        return listViewItems[position];
    }


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

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

        final ListViewHolder listViewHolder;
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = layoutInflater.inflate(R.layout.activity_custom_listview, parent, false);
            listViewHolder = new ListViewHolder();
            listViewHolder.tvProductName = (TextView) row.findViewById(R.id.tvProductName);
            listViewHolder.ivProduct = (ImageView) row.findViewById(R.id.ivproduct);
            listViewHolder.tvPrices = (TextView) row.findViewById(R.id.tvProductPrice);
            listViewHolder.btnPlus = (ImageButton) row.findViewById(R.id.ib_addnew);
            listViewHolder.edTextQuantity = (EditText) row.findViewById(R.id.editTextQuantity);
            listViewHolder.btnMinus = (ImageButton) row.findViewById(R.id.ib_remove);
            static_price = context.getResources().getStringArray(R.array.Price);
            row.setTag(listViewHolder);
        } else {
            row = convertView;
            listViewHolder = (ListViewHolder) convertView.getTag();
        }

        //Loading image from below url into imageView

for (int z = 0; z < MainActivity_items.ItemImage.length; z++) {
    Picasso.with(context)
           .load(""+MainActivity_items.ItemImage[position])
           .into(listViewHolder.ivProduct);
}

//      listViewHolder.ivProduct.setImageResource(images.getResourceId(position, -1));
        listViewHolder.edTextQuantity.setText(quantity.get(position) + "");
        listViewHolder.tvProductName.setText(listViewItems[position]);
        listViewHolder.tvPrices.setText(prices[position]);


        listViewHolder.btnPlus.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (customButtonListener != null) {
                    customButtonListener.onButtonClickListener(position, listViewHolder.edTextQuantity, 1);

                    quantity.set(position, quantity.get(position) + 1);
                    //price.set(position, price.get(position) + 1);

                    row.getTag(position);

                    get_price = listViewHolder.tvPrices.getText().toString();

                    g_price = Integer.valueOf(static_price[position]);

                    get_quntity = listViewHolder.edTextQuantity.getText().toString();
                    g_quntity = Integer.valueOf(get_quntity);

                    Integer.parseInt(listViewHolder.edTextQuantity.getText().toString());
                    map.put("" + listViewHolder.tvProductName.getText().toString(), Integer.valueOf(Integer.parseInt(listViewHolder.edTextQuantity.getText().toString())));
//                    Log.d("A ", "" + a);
//                    Toast.makeText(context, "A" + a, Toast.LENGTH_LONG).show();
//                    Log.d("Position ", "" + position);
//                    System.out.println(+position + " Values " + map.values());
                    listViewHolder.tvPrices.getTag();
                    listViewHolder.tvPrices.setText("" + g_price * g_quntity);
                    ShowHashMapValue();

                }


            }

        });
        listViewHolder.btnMinus.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (customButtonListener != null) {

                    customButtonListener.onButtonClickListener(position, listViewHolder.edTextQuantity, -1);
                    if (quantity.get(position) > 0)
                        quantity.set(position, quantity.get(position) - 1);

                    get_price = listViewHolder.tvPrices.getText().toString();
                    g_minus = Integer.valueOf(get_price);
                    g_price = Integer.valueOf(static_price[position]);
                    int minus = g_minus - g_price;
                    if (minus >= g_price) {
                        listViewHolder.tvPrices.setText("" + minus);
                    }
                    map.put("" + listViewHolder.tvProductName.getText().toString(), Integer.valueOf(Integer.parseInt(listViewHolder.edTextQuantity.getText().toString())));
                    ShowHashMapValue();
                }
            }
        });


        return row;
    }

    private void ShowHashMapValue() {
        /**
         * get the Set Of keys from HashMap
         */
        Set setOfKeys = map.keySet();

/**
 * get the Iterator instance from Set
 */
        Iterator iterator = setOfKeys.iterator();

/**
 * Loop the iterator until we reach the last element of the HashMap
 */
        while (iterator.hasNext()) {
/**
 * next() method returns the next key from Iterator instance.
 * return type of next() method is Object so we need to do DownCasting to String
 */
            String key = (String) iterator.next();

/**
 * once we know the 'key', we can get the value from the HashMap
 * by calling get() method
 */
            int value = map.get(key);

            System.out.println("Key: " + key + ", Value: " + value);
        }
    }
}

3 个答案:

答案 0 :(得分:1)

正如错误所述,ItemListTab活动无法转换为CustomButtonListener,因为它没有实现它,当前片段。所以你需要改变

listAdapter.setCustomButtonListener((CustomButtonListener) getActivity());

listAdapter.setCustomButtonListener(ManItems.this);
/* Not listAdapter.setCustomButtonListener(this), otherwise
 you would be referring to the AsyncTask 'GetContacts' itself */

答案 1 :(得分:0)

希望这对你有所帮助 用于在片段中使用接口侦听器 不要在片段类上实现,在Tab Activity中实现Listener 并且图片查看或片段的任何内容都应该是公共静态

答案 2 :(得分:0)

如果您使用getActivity()

,您的活动ItemListTab必须实现CustomButtonListener

传递ManItems的引用,因为ManItems实现了CustomButtonListener