在我的应用程序中,我尝试在项目单击上激活电话呼叫以进行列表项目单击。但是这段代码根本不起作用。每当我点击电话时运行此代码,应用程序就会崩溃。我在这里附上了我的崩溃报告。因为我是android开发的新手。我不知道如何摆脱这种恐怖行为。
我的代码是 -
public class AlertDialogFragment extends DialogFragment {
private ListView listView1;
private ListView listView2;
private Button cancelButton1;
private Button cancelButton2;
private String[] companyName;
private String[] actionName;
private ArrayAdapter<String> adapter;
protected FragmentActivity mActivity;
public AlertDialogFragment(){
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setCancelable(true);
setStyle(DialogFragment.STYLE_NORMAL, R.style.DialogStyle);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.first_alertlist_contact, container, false);
//Set Title Dialog
getDialog().setTitle("Contact");
//Button,ListView1 Initialization
listView1=(ListView) rootView.findViewById(R.id.listView1);
cancelButton1=(Button) rootView.findViewById(R.id.cancel_button1);
// Defined Array values to show in ListView
companyName = getResources().getStringArray(R.array.company_name);
//Create and set Adepter TO ListView1
adapter=new ArrayAdapter<String>(getActivity(), R.layout.first_alertlist_textstyle,android.R.id.text1,companyName);
listView1.setAdapter(adapter);
cancelButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dismiss();
}
});
listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
if(itemPosition == 0)
{
dismiss();
showDialog2();
}
if(itemPosition == 1)
{
dismiss();
showDialog2();
}
if(itemPosition == 2)
{
dismiss();
showDialog2();
}
.....
}
});
return rootView;
}
private void showDialog2(){
final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this.getActivity(), R.style.DialogStyle);
LayoutInflater inflater = this.getActivity().getLayoutInflater();
View dialogView = inflater.inflate(R.layout.second_alertlist_contact, null);
dialogBuilder.setView(dialogView);
listView2 = (ListView) dialogView.findViewById(R.id.listView2);
cancelButton2=(Button) dialogView.findViewById(R.id.cancel_button2);
// Defined Array values to show in ListView
actionName = getResources().getStringArray(R.array.contact_way);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(),
R.layout.first_alertlist_textstyle, android.R.id.text1, actionName);
listView2.setAdapter(adapter);
listView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
if(itemPosition == 0)
{
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("+1234667");
//builder.setMessage("Are you sure you want to log out?");
builder.setPositiveButton("Call", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// close the dialog, go to login page
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:1234567"));
startActivity(callIntent);
dialog.dismiss();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}
if(itemPosition == 1)
{
dismiss();
System.out.println("Hello");;
}
if(itemPosition == 2)
{
dismiss();
System.out.println("Hello");;
}
if(itemPosition == 3)
{
dismiss();
System.out.println("Hello");;
}
}
});
dialogBuilder.show();
}
}
07-10 15:40:49.134 12970-12970/com.testgrid E/InputEventReceiver: Exception dispatching input event.
07-10 15:40:49.134 12970-12970/com.testgrid E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
07-10 15:40:49.134 12970-12970/com.testgrid E/MessageQueue-JNI:
java.lang.NullPointerException: Attempt to invoke virtual method
'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference
at
android.support.v7.app.AlertDialog.resolveDialogTheme(AlertDialog.java:113)
at
android.support.v7.app.AlertDialog$Builder.<init>(AlertDialog.java:291)
at
com.testgrid.AlertDialogFragment$3.onItemClick(AlertDialogFragment.java:155)
at
android.widget.AdapterView.performItemClick(AdapterView.java:310)
答案 0 :(得分:1)
你需要一些权限来打电话,把它放在你的清单
中<uses-permission android:name="android.permission.CALL_PHONE" />
也许本教程有助于https://www.mkyong.com/android/how-to-make-a-phone-call-in-android/
答案 1 :(得分:0)
使用以下代码制作对话框和通话按钮: 将上下文设置为youractivityname.this,或者如果您在片段getActivity()中。
AlertDialog.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder = new AlertDialog.Builder(context, android.R.style.Theme_Material_Dialog_Alert);
} else {
builder = new AlertDialog.Builder(context);
}
builder.setTitle("Title")
.setMessage("some message")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// call specific number
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:1234567"));
startActivity(callIntent);
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do anything
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();