我的应用发送USSD请求。它具有一个活动,从该活动启动片段,该片段具有启动AlertDialog的按钮,并且onItemSelected发送USSD请求。现在,当我尝试发送请求并且日志显示上述错误时,强制关闭。
记录:
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<Item>
*/
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);
任何人都可以帮忙解决这个问题?因为有时它可以随机工作。这行是什么
找不到活动来处理Intent {act = android.intent.action.CALL dat = tel:* 1050 * 100#}来自日志?
答案 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);
3。你可以使用ACTION_DIAL显示拨号号码
答案 1 :(得分: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" />