-1

我的應用程序發送USSD請求。它有一個從哪裏啓動片段的活動,該片段具有啓動AlertDialog的按鈕,並在onItemSelected上發送USSD請求。現在,當我嘗試發送請求和日誌時,強制關閉顯示上述錯誤。致命異常onItemSelected

登錄:

FATAL EXCEPTION: main 
Process: com.esqmo.apps.mosungiplus, PID: 14315 
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.CALL dat=tel :*1050*100# } 
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1879) 
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1546) 
at android.app.Activity.startActivityForResult(Activity.java:4298) 
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50) 
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79) 
at android.support.v4.app.ActivityCompatJB.startActivityForResult(ActivityCompatJB.java:30) 
at android.support.v4.app.ActivityCompat.startActivityForResult(ActivityCompat.java:146) 
at android.support.v4.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:932) 
at android.support.v4.app.FragmentActivity$HostCallbacks.onStartActivityFromFragment(FragmentActivity.java:1047) 
at android.support.v4.app.Fragment.startActivity(Fragment.java:940) 
at android.support.v4.app.Fragment.startActivity(Fragment.java:929) 
at com.esqmo.apps.mosungiplus.fragments.AfricellNetFragment.onItemSelected(AfricellNetFragment.java:117) 
at com.esqmo.apps.mosungiplus.dialogs.MainDialog$3.onClick(MainDialog.java:215) 
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:174) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:7325) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

這裏是片段類:

import android.content.Intent; 
import android.net.Uri; 
import android.support.v4.app.Fragment; 
import android.view.View; 
import android.view.LayoutInflater; 
import android.view.ViewGroup; 
import android.os.Bundle; 
import android.widget.LinearLayout; 
import android.support.constraint.ConstraintLayout; 
import android.widget.TextView; 
import android.widget.Button; 
import android.widget.Toolbar; 

import com.esqmo.apps.mosungiplus.R; 
import com.esqmo.apps.mosungiplus.dialogs.MainDialog; 

import java.util.ArrayList; 

public class AfricellCBFragment extends Fragment implements View.OnClickListener, MainDialog.OnItemSelectedListener { 

    String cbCode = "1000"; 
    String hash = Uri.encode("#"); 

    private LinearLayout africellCbInt; 
    private TextView africellCbDialogMsg; 
    private TextView textView; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.custom_africell_cb, container, false); 
    } 

    @Override 
    public void onViewCreated(View view, Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 

     africellCbInt = (LinearLayout) view.findViewById(R.id.africell_cb_int); 
     view.findViewById(R.id.button_africell_cb_1).setOnClickListener(this); 
     view.findViewById(R.id.button_africell_cb_2).setOnClickListener(this); 

    } 

    @Override 
    public void onClick(View view) { 
     switch (view.getId()) { 
      case R.id.button_africell_cb_1: 
       String ussdCode = "*" + cbCode + Uri.encode("#"); 
       startActivity(new Intent("android.intent.action.CALL", Uri.parse("tel:" + ussdCode))); 
       break; 

      //TODO 
      case R.id.button_africell_cb_2: 

       ArrayList<MainDialog.Item> pickerItems = new ArrayList<>(); 
       pickerItems.add(new MainDialog.Item("Forfait gratuit", "0")); 
       pickerItems.add(new MainDialog.Item("Forfait Africell - Africell", "1")); 
       pickerItems.add(new MainDialog.Item("Forfait International et International 500", "2")); 
       pickerItems.add(new MainDialog.Item("Solde bonus", "3")); 
       pickerItems.add(new MainDialog.Item("Forfait Internet", "4")); 
       pickerItems.add(new MainDialog.Item("Forfait Internet nuit", "5")); 


       MainDialog dialog = MainDialog.newInstance(getActivity().getString(R.string.dialog_cb_title), 
         pickerItems, -1); 

       dialog.show(getChildFragmentManager(), "ItemPicker"); 
       break; 
     } 
    } 

    @Override 
    public void onItemSelected(MainDialog fragment, MainDialog.Item item, int index) { 
     if(index == 0){ 
      String ussdCode = "*1000*1" + hash; 
      startActivity(new Intent("android.intent.action.CALL", Uri.parse("tel:" + ussdCode))); 
     } else 
     if(index == 1){ 
      String ussdCode = "*1000*2" + hash; 
      startActivity(new Intent("android.intent.action.CALL", Uri.parse("tel:" + ussdCode))); 
     } else 
     if(index == 2){ 
      String ussdCode = "*1000*3" + hash; 
      startActivity(new Intent("android.intent.action.CALL", Uri.parse("tel:" + ussdCode))); 
     } else 
     if(index == 3){ 
      String ussdCode = "*1000*4" + hash; 
      startActivity(new Intent("android.intent.action.CALL", Uri.parse("tel:" + ussdCode))); 
     } else 
      if(index == 4){ 
       String ussdCode = "*1050*225" + hash; 
       startActivity(new Intent("android.intent.action.CALL", Uri.parse("tel:" + ussdCode))); 
      } 
      else 
       if(index == 5){ 
        String ussdCode = "*1050*445" + hash; 
        startActivity(new Intent("android.intent.action.CALL", Uri.parse("tel:" + ussdCode))); 
       } 
    } 
} 

而且AlertDialog:

import android.app.AlertDialog; 
import android.app.Dialog; 
import android.support.v4.app.DialogFragment; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.text.TextUtils; 
import android.util.Log; 

import com.esqmo.apps.mosungiplus.R; 

import java.util.ArrayList; 
import java.util.List; 

/** 
* Dialog fragment that allows user to select an item from a list 
*/ 
public class MainDialog extends DialogFragment { 
    public static final String LOGTAG = "esQmo.MainDialog"; 

    /** 
    * An item that can be displayed and selected by the MainDialog 
    */ 
    public static class Item { 
     private String title; 
     private int intValue; 
     private String stringValue; 

     private static final String KEY_TITLE  = "title"; 
     private static final String KEY_INT_VALUE = "intValue"; 
     private static final String KEY_STRING_VALUE = "stringValue"; 

     /** 
     * Construct with title and integer value 
     * 
     * @param title Name displayed in list 
     * @param value Integer value associated with item 
     */ 
     /* public Item(String title, int value) { 
      assert(!TextUtils.isEmpty(title)); 

      this.title = title; 
      this.intValue = value; 
     } */ 

     /** 
     * Construct with title and string value 
     * 
     * @param title Name displayed in list 
     * @param value String value associated with item 
     */ 
     public Item(String title, String value) { 
      assert(!TextUtils.isEmpty(title)); 

      this.title = title; 
      this.stringValue = value; 
     } 

     /** 
     * Construct from a bundle of values 
     * @param bundle 
     */ 
     public Item(Bundle bundle) { 
      title = bundle.getString(KEY_TITLE, null); 
      intValue = bundle.getInt(KEY_INT_VALUE, 0); 
      stringValue = bundle.getString(KEY_STRING_VALUE, null); 
     } 

     /** 
     * Get a Bundle of values that can be passed to the Item(Bundle) constructor 
     * to re-create the object 
     * 
     * @return Bundle 
     */ 
     public Bundle getValuesBundle() { 
      Bundle bundle = new Bundle(); 

      bundle.putString(KEY_TITLE, title); 
      bundle.putInt(KEY_INT_VALUE, intValue); 
      if (stringValue != null) { 
       bundle.putString(KEY_STRING_VALUE, stringValue); 
      } 

      return bundle; 
     } 

     public String getTitle() { 
      return title; 
     } 

     public int getIntValue() { 
      return intValue; 
     } 

     public String getStringValue() { 
      return stringValue; 
     } 

     /** 
     * Given a list of items, create a Bundle that can be passed to 
     * Item.itemsFromBundle() to recreate them. 
     * 
     * @param items list of items 
     * @return Bundle 
     */ 
     public static Bundle bundleOfItems(List<Item> items) { 
      int itemCount = items.size(); 
      ArrayList<Bundle> itemBundles = new ArrayList<>(); 
      for (int i = 0; i < itemCount; ++i) { 
       itemBundles.add(items.get(i).getValuesBundle()); 
      } 

      Bundle bundle = new Bundle(); 
      bundle.putParcelableArrayList(ARG_ITEMS, itemBundles); 
      return bundle; 
     } 

     /** 
     * Given a Bundle created by Item.bundleOfItems(), recreate the 
     * original list of items. 
     * 
     * @param bundle Bundle created by Item.bundleOfItems() 
     * @return ArrayList&lt;Item&gt; 
     */ 
     public static ArrayList<Item> itemsFromBundle(Bundle bundle) { 
      ArrayList<Bundle> itemBundles = bundle.getParcelableArrayList(ARG_ITEMS); 
      ArrayList<Item> items = new ArrayList<>(); 
      for (Bundle itemBundle: itemBundles) { 
       items.add(new Item(itemBundle)); 
      } 
      return items; 
     } 
    } 

    /** 
    * Interface for notification of item selection 
    * 
    * If the owning Activity implements this interface, then the fragment will 
    * invoke its onItemSelected() method when the user clicks the OK button. 
    */ 
    public interface OnItemSelectedListener { 
     void onItemSelected(MainDialog fragment, Item item, int index); 
    } 

    private static final String ARG_TITLE = "ARG_TITLE"; 
    private static final String ARG_ITEMS = "ARG_ITEMS"; 
    private static final String ARG_SELECTED_INDEX = "ARG_SELECTED_INDEX"; 

    /** 
    * Create a new instance of MainDialog with specified arguments 
    * 
    * @param title Dialog title text 
    * @param items Selectable items 
    * @param selectedIndex initial selection index, or -1 if no item should be pre-selected 
    * @return MainDialog 
    */ 
    public static MainDialog newInstance(String title, ArrayList<Item> items, int selectedIndex) { 
     Bundle args = new Bundle(); 
     args.putString(ARG_TITLE, title); 
     args.putBundle(ARG_ITEMS, Item.bundleOfItems(items)); 
     args.putInt(ARG_SELECTED_INDEX, selectedIndex); 

     MainDialog fragment = new MainDialog(); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    private String title; 
    private ArrayList<Item> items; 
    private int selectedIndex; 

    /** 
    * Constructor 
    */ 
    public MainDialog() { 
    } 

    @Override 
    public void onSaveInstanceState(Bundle outState) { 
     super.onSaveInstanceState(outState); 

     outState.putInt(ARG_SELECTED_INDEX, selectedIndex); 
    } 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     Bundle args = getArguments(); 
     if (args != null) { 
      title = args.getString(ARG_TITLE, "Dialog"); 
      items = Item.itemsFromBundle(args.getBundle(ARG_ITEMS)); 
      selectedIndex = args.getInt(ARG_SELECTED_INDEX, -1); 
     } 

     if (savedInstanceState != null) { 
      selectedIndex = savedInstanceState.getInt(ARG_SELECTED_INDEX, selectedIndex); 
     } 

     String[] itemTitles = getItemTitlesArray(); 

     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
     builder.setIcon(R.drawable.homepage_landing_icon). 
       setTitle(title) 
       .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         Log.d(LOGTAG, "OK button clicked"); 

         Fragment fragment = getParentFragment(); 
         if (fragment instanceof OnItemSelectedListener) { 
          if (0 <= selectedIndex && selectedIndex < items.size()) { 
           Item item = items.get(selectedIndex); 
           OnItemSelectedListener listener = (OnItemSelectedListener) fragment; 
           listener.onItemSelected(MainDialog.this, item, selectedIndex); 

         } 
         } 
        } 
       }) 
       .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         Log.d(LOGTAG, "Cancel button clicked"); 

         // OK, just let the dialog be closed 
        } 
       }) 
       .setSingleChoiceItems(itemTitles, selectedIndex, new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         Log.d(LOGTAG, "User clicked item with index " + which); 
         selectedIndex = which; 
        } 
       }); 

     return builder.create(); 
    } 

    private String[] getItemTitlesArray() { 
     final int itemCount = items.size(); 
     String[] itemTitles = new String[itemCount]; 
     for (int i = 0; i < itemCount; ++i) { 
      itemTitles[i] = items.get(i).getTitle(); 
     } 
     return itemTitles; 
    } 

錯誤點這裏查看片段:

startActivity(new Intent("android.intent.action.CALL", Uri.parse("tel :" + ussdCode))); 

,爲AlertDialog:

listener.onItemSelected(MainDialog.this, item, selectedIndex); 

任何人都可以幫助解決這個?因爲有時它可以工作但是隨機。從日誌:什麼是線

無活動處理意向{* 1050 * 100#act =被android.intent.action.CALL DAT =電話}?

+0

壓低選民:不要只是投票在人們的問題上,也請提及其背後的原因!如何爲地獄改善他們的問題,如果你不告訴他們什麼是錯的?!? –

回答

2

1.您是否向Manifest添加了<uses-permission android:name="android.permission.CALL_PHONE" />權限?

2.更改代碼

Intent intent = new Intent (Intent.ACTION_CALL); 
intent.setData (Uri.parse ("tel:" + phoneNumber)); 
context.startActivity (intent); 

可以使用ACTION_DIAL顯示撥號器提供的號碼

+0

感謝您的回覆,但它不起作用。上下文無法解決,甚至沒有上下文,該應用程序仍然崩潰....我失去了我知道的東西,但無法弄清楚,可憐的noob –

1

用它來呼叫 -

String ussdCode = "*1000*2" + hash; 
Intent intent = new Intent(Intent.ACTION_CALL); 
intent.setData(Uri.parse("tel:" +ussdCode)); 
startActivity(intent); 

不要忘記在Manifest.xml上添加權限 -

<uses-permission android:name="android.permission.CALL_PHONE" /> 
+0

感謝您的答覆,但問題依然存在。 –