简体   繁体   中英

How to add the call feature to android fragment class?

I was working on an android app which will run an ussd code . But application is crashing when i click on the list item. And can you tell me how to write the first value from the list in the if else statement without writing all numbers manually?

Fragment Class: it has the call functionality ...

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link ussd.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link ussd#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ussd extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private ListView listView;
    private ussdadaper ussd;
    private List<ussdz> ussdlist;

    private OnFragmentInteractionListener mListener;

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

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment ussd.
     */
    // TODO: Rename and change types and number of parameters
    public static ussd newInstance(String param1, String param2) {
        ussd fragment = new ussd();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }


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

        listView = (ListView)rootView.findViewById(R.id.listView);
        ussdlist = new ArrayList<>();
        ussdlist.add(new ussdz(1,"*#44336#","Software Version Info"));

        listView.setOnItemClickListener(
                new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        String item = (String)listView.getItemAtPosition(position);
                        if(position == 0){

                        Intent callIntent = new Intent(Intent.ACTION_CALL);
                        callIntent.setData(Uri.parse("*#44336#"));
                        callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(callIntent);
                    }}
                }
        );


        ussd = new ussdadaper(getActivity(),ussdlist);
        listView .setAdapter(ussd);   //you should setthe adapter to the listview
        return rootView;
    }  

    // TODO: Rename method, update argument and hook method into UI event
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }


    public interface OnFragmentInteractionListener {
        public void onFragmentInteractionHome(Uri uri);
        public void openHome(View view);
    }
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p/>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */

ERROR LOG :

06-14 23:40:50.069 11155-11155/com.buckydroid.app.droidcpu E/InputEventReceiver: Exception dispatching input event. 06-14 23:40:50.069 11155-11155/com.buckydroid.app.droidcpu E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback 06-14 23:40:50.082 11155-11155/com.buckydroid.app.droidcpu E/MessageQueue-JNI: java.lang.ClassCastException: com.buckydroid.app.droidcpu.ussdz cannot be cast to java.lang.String at layout.ussd$1.onItemClick(ussd.java:92) at android.widget.AdapterView.performItemClick(AdapterView.java:310) at android.widget.AbsListView.performItemClick(AbsListView.java:1169) at android.widget.AbsListView$PerformClick.run(AbsListView.java:3217) at android.widget.AbsListView.onTouchUp(AbsListView.java:4074) at android.widget.AbsListView.onTouchEvent(AbsListView.java:3838) at android.view.View.dispatchTouchEvent(View.java:9301) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2547) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2240) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553) at android.view.ViewGroup.di spatchTouchEvent(ViewGroup.java:2254) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254) at com.android.internal.policy.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2665) at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1793) at android.app.Activity.dispatchTouchEvent(Activity.java:3076) at com.android.internal.policy.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2626) at android.view.View.dispatchPointerEvent( View.java:9521) at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4238) at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4104) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3650) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3703) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3669) at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3795) at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3677) at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3852) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3650) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3703) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3669) at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3677) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.jav a:3650) at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5932) at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5906) at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5867) at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6035) at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185) at android.os.MessageQueue.nativePollOnce(Native Method) at android.os.MessageQueue.next(MessageQueue.java:323) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5471) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 06-14 23:40:50.082 11155-11155/com.buckydroid.app.droidcpu D/AndroidRuntime: Shutting down VM 06-14 23:40:50.085 11155-11155/com.buckydroid.app.droidcpu E/AndroidRuntime: FATAL EXCEPTION: m ain Process: com.buckydroid.app.droidcpu, PID: 11155 Theme: themes:{default=overlay:com.zyxxeil.milos.ui, iconPack:com.zyxxeil.milos.ui, fontPkg:com.zyxxeil.milos.ui, com.android.systemui=overlay:com.zyxxeil.milos.ui, com.android.systemui.navbar=overlay:com.zyxxeil.milos.ui} java.lang.ClassCastException: com.buckydroid.app.droidcpu.ussdz cannot be cast to java.lang.String at layout.ussd$1.onItemClick(ussd.java:92) at android.widget.AdapterView.performItemClick(AdapterView.java:310) at android.widget.AbsListView.performItemClick(AbsListView.java:1169) at android.widget.AbsListView$PerformClick.run(AbsListView.java:3217) at android.widget.AbsListView.onTouchUp(AbsListView.java:4074) at android.widget.AbsListView.onTouchEvent(AbsListView.java:3838) at android.view.View.dispatchTouchEvent(View.java:9301) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2547) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2240) at android.view.ViewGroup.dispatchTransformed TouchEvent(ViewGroup.java:2553) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254) at com.android.internal.policy.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2665) at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1793) at android.app.Activity.dispatchTouchEvent(Activity.java:3076) at com.android.internal.policy.PhoneWindow$DecorView.dispatchTouchEvent(Phone Window.java:2626) at android.view.View.dispatchPointerEvent(View.java:9521) at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4238) at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4104) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3650) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3703) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3669) at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3795) at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3677) at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3852) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3650) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3703) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3669) at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3677) at a ndroid.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3650) at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5932) at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5906) at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5867) at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6035) at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185) at android.os.MessageQueue.nativePollOnce(Native Method) at android.os.MessageQueue.next(MessageQueue.java:323) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5471) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

So If i am doing anything wrong in the call activity then please help me. Compiler shows no error.

Your Exception occurs at this line:

String item = (String)listView.getItemAtPosition(position);

You're trying to cast something that is not a String to String . Reading the error log, it says:

ClassCastException: com.buckydroid.app.droidcpu.ussdz cannot be cast to java.lang.String

You're getting your custom class ussdz and trying to cast it to String .

Just looking at your code, you're not even using String item anywhere, you can simply remove it and fix this error. If you actually do need it , just do this:

ussdz item = listView.getItemAtPosition(position);

Because that's the type which the listView will return.

Sidenote: It is recommended that you follow naming conventions in Java, such as naming the classes starting with an Uppercase letter, and giving them a bit more descriptive names than ussdz because that means nothing to someone else reading your code. This is just a friendly advice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM