我正在尝试实现自定义对话框,我必须在自定义对话框中拨打一个我正在起诉意图的数字,但是它不工作的应用程序每次强制关闭。
我用于自定义对话框的方法也显示已弃用。如果有人可以建议自定义对话框的替代方式
public class MainActivity extends AppCompatActivity {
Button b1,b2,b3,b4,b5,b6;
EditText et1;
ImageButton ib1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button1);
b2=(Button)findViewById(R.id.button2);
b3=(Button)findViewById(R.id.button3);
b4=(Button)findViewById(R.id.button4);
b5=(Button)findViewById(R.id.button5);
et1=(EditText)findViewById(R.id.editText);
ib1=(ImageButton)findViewById(R.id.imageButton);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(1);
}
});
}
public Dialog onCreateDialog(int id)
{
Dialog dialog=new Dialog(this);
switch (id) {
case 1:
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.call);
ib1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent dial = new Intent(Intent.ACTION_DIAL);
String num = et1.getText().toString();
Uri uri = Uri.parse("tel:"+num);
dial.setData(uri);
}
});
break;
}
dialog.show();
return dialog;
}
}
logcat的
FATAL EXCEPTION: main
Process: bt4u.com.intentdemo1, PID: 15895
Theme: themes:{default=overlay:com.rr.neptune, iconPack:system, fontPkg:com.rr.neptune, com.android.systemui=overlay:com.rr.neptune, com.android.systemui.navbar=overlay:com.rr.neptune}
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at bt4u.com.intentdemo1.MainActivity.onCreateDialog(MainActivity.java:52)
at android.app.Activity.onCreateDialog(Activity.java:3348)
at android.app.Activity.createDialog(Activity.java:1074)
at android.app.Activity.showDialog(Activity.java:3442)
at android.app.Activity.showDialog(Activity.java:3400)
at bt4u.com.intentdemo1.MainActivity$1.onClick(MainActivity.java:36)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21156)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5466)
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)
主要活动
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="bt4u.com.intentdemo1.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Make A Call"
android:id="@+id/button1"
android:layout_marginTop="37dp"
android:layout_alignParentTop="true"
android:layout_alignLeft="@+id/button2"
android:layout_alignStart="@+id/button2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send A Text"
android:id="@+id/button2"
android:layout_marginTop="40dp"
android:layout_below="@+id/button1"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open Website"
android:id="@+id/button3"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Google Search"
android:id="@+id/button4"
android:layout_marginTop="40dp"
android:layout_below="@+id/button3"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send An Email"
android:id="@+id/button5"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/button3"
android:layout_alignEnd="@+id/button3"
android:layout_marginBottom="39dp" />
</RelativeLayout>
call.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="@color/background">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="@+id/editText"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ImageButton
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/imageButton"
android:src="@drawable/call"
android:scaleType="fitXY"
android:background="@null"
android:layout_below="@+id/editText"
android:onClick="call"
android:layout_centerHorizontal="true" />
</RelativeLayout>
答案 0 :(得分:0)
NullpointerException是因为您尝试从主活动xml获取ImageButton
,但ImageButton位于call.xml
。
所以它应该是
case 1:
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.call);
//Initialize your ImageButton here. Do the same for your EditText as well.
ImageButton ib1 = (ImageView) dialog.findViewById(R.id.imageButton);
EditText et1 = (EditText) dialog.findViewById(R.id.editText); //INITIALIZE EDITTEXT HERE.
ib1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent dial = new Intent(Intent.ACTION_DIAL);
String num = et1.getText().toString();
Uri uri = Uri.parse("tel:"+num);
dial.setData(uri);
}
});
break;
这应该有效