繁体   English   中英

如何将通话功能添加到android片段类中?

[英]How to add the call feature to android fragment class?

我正在开发将运行usdd代码的android应用程序。 但是,当我单击列表项时,应用程序崩溃了。 您能告诉我如何在if else语句中从列表中写入第一个值而无需手动写入所有数字吗?

片段类:具有调用功能...

/**
 * 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.
     */

错误日志

06-14 23:40:50.069 11155-11155 / com.buckydroid.app.droidcpu E / InputEventReceiver:调度输入事件的异常。 06-14 23:40:50.069 11155-11155 / com.buckydroid.app.droidcpu E / MessageQueue-JNI:MessageQueue回调中的异常:handleReceiveCallback 06-14 23:40:50.082 11155-11155 / com.buckydroid.app.droidcpu E / MessageQueue-JNI:java.lang.ClassCastException:com.buckydroid.app.droidcpu.ussdz无法在android.widget.AdapterView处layout.ussd $ 1.onItemClick(ussd.java:92)处转换为java.lang.String .performItemClick(AdapterView.java:310)在android.widget.AbsListView.performItemClick(AbsListView.java:1169)在android.widget.AbsListView $ PerformClick.run(AbsListView.java:3217)在android.widget.AbsListView.onTouchUp( android.widget.AbsListView.onTouchEvent(AbsListView.java:3838)的android.view.View.dispatchTouchEvent(View.java:9301)的android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2547)的AbsListView.java:4074) )在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2240)在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)在android.view.ViewGroup.di android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)处的spatchTouchEvent(ViewGroup.java:2254)android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)处的android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java) :2553),位于android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254),位于android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553),位于android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254),位于.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)位于android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)位于com.android.internal.policy.PhoneWindow $ DecorView.superDispatchTouchEvent(PhoneWindow.java:2665) com.android.internal.policy.PhoneWindow $ DecorView.dispatchTouchEvent(PhoneWindow.com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1793)位于android.app.Activity.dispatchTouchEvent(Activity.java:3076)。 java:2626)在android.view.View.dispatchPointerEvent( View.java:9521)位于android.view.ViewRootImpl $ ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4238)位于android.view.ViewRootImpl $ ViewPostImeInputStage.onProcess(ViewRootImpl.java:4104)位于android.view.ViewRootImpl $ InputStage.deliver (ViewRootImpl.java:3650)在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:3703)在android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:3669)在android.view.ViewRootImpl $ AsyncInputStage在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:3677)在android.view.ViewRootImpl $ AsyncInputStage.apply(ViewRootImpl.java:3852)处转发(ViewRootImpl.java:3795)在android.view.ViewRootImpl $ InputStage android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:3703)的android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:3669)的.deliver(ViewRootImpl.java:3650)android.view.ViewRootImpl $ android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.jav)上的InputStage.apply(ViewRootImpl.java:3677) a:3650),位于android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5932),位于android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5906),位于android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5867) android.os.MessageQueue上android.os.MessageQueue.nativePollOnce(Native Method)处的android.view.ViewRootImpl $ WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6035)在android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185) android.os.Looper.loop(Looper.java:135)处的next(MessageQueue.java:323)android.app.ActivityThread.main(ActivityThread.java:5471)处的java.lang.reflect.Method.invoke(Native) com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)处的com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)06-14 23:40:50.082 11155-11155 / com.buckydroid.app.droidcpu D / AndroidRuntime:关闭VM 06-14 23:40:50.085 11155-11155 / com.buckydroid.app.droidcpu E / AndroidRuntime:致命异常:m 进程:com.buckydroid.app.droidcpu,PID:11155主题:主题:{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无法在android.widget.AdapterView.performItemClick(AdapterView.java:310)的layout.ussd $ 1.onItemClick(ussd.java:92)处转换为java.lang.String在android.widget.AbsListView.performItemClick(AbsListView.java :1169),位于android.widget.AbsListView $ PerformClick.run(AbsListView.java:3217),位于android.widget.AbsListView.onTouchUp(AbsListView.java:4074),位于android.widget.AbsListView.onTouchEvent(AbsListView.java:3838)在android.view.View.dispatchTouchEvent(View.java:9301)在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2547)在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2240)在android.view。 ViewGroup.dispatchTransformed android.view.ViewGroup.dispatch上的TouchEvent(ViewGroup.java:2553)android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)上android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java)上的TouchEvent(ViewGroup.java:2254) :2254),位于android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553),位于android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254),位于android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553),位于android com.android.internal上的android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2553)上的.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)com.android.internal处的android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)。 com上com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1793)上com.android.app.Activity.dispatchTouchEvent(Activity.java:3076)上的policy.PhoneWindow $ DecorView.superDispatchTouchEvent(PhoneWindow.java:2665) .android.internal.policy.PhoneWindow $ DecorView.dispatchTouchEvent(电话 Window.java:2626)位于android.view.View.dispatchPointerEvent(View.java:9521)位于android.view.ViewRootImpl $ ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4238)位于android.view.ViewRootImpl $ ViewPostImeInputStage.onProcess(ViewRootImpl .java:4104),位于android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3650),位于android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:3703),位于android.view.ViewRootImpl $ InputStage.forward( android.view.ViewRootImpl $ AsyncInputStage.forward(ViewRootImpl.java:3795)位于android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:3677)位于android.view.ViewRootImpl $ AsyncInputStage.apply (ViewRootImpl.java:3852)在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3650)在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:3703)在android.view.ViewRootImpl $ InputStage在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:3677)处转发(ViewRootImpl.java:3669) android.view.ViewRootImpl.java:3650)上的ndroid.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3650)android.view.View.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5932)android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5906) android.view.ViewRootImpl $ WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6035)处的.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5867)android.os.MessageQueue处的android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)处的。 android.os.MessageQueue.next(MessageQueue.java:323)上的nativePollOnce(Native方法)android.os.Looper.loop(Looper.java:135)上android.app.ActivityThread.main(ActivityThread.java:5471)在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)的java.lang.reflect.Method.invoke(本机方法)处在com.android.internal.os.ZygoteInit.main(ZygoteInit。 Java:616)

因此,如果我在通话活动中做任何错事,请帮助我。 编译器未显示任何错误。

您的例外发生在此行:

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

您正在尝试将非StringString 读取错误日志,它说:

ClassCastException:com.buckydroid.app.droidcpu.ussdz无法转换为java.lang.String

您正在获取自定义类ussdz并尝试将其ussdzString

仅查看代码,您甚至都没有在任何地方使用String item ,只需删除它并修复此错误即可。 如果您确实需要它 ,请执行以下操作:

ussdz item = listView.getItemAtPosition(position);

因为那是listView将返回的类型。

旁注:建议您遵循Java中的命名约定,例如以大写字母开头的类命名,并给它们比ussdz更具描述性的名称,因为这对其他人来说没有任何意义。 这只是一个友好的建议。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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