[英]alert dialog in android with onclicklistener
該應用程序以日志 cat 錯誤開始停止,我多次檢查我的代碼,但沒有弄清楚問題是什么我猜這可能是我在布局中做錯了但沒有錯,提前致謝
我的 logcat 錯誤是:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ahmed_samir.dialog, PID: 4229
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ahmed_samir.dialog/com.example.ahmed_samir.dialog.MainActivity}: android.content.res.Resources$NotFoundException: File from xml type layout resource ID #0x1020009
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: android.content.res.Resources$NotFoundException: File from xml type layout resource ID #0x1020009
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2696)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2651)
at android.content.res.Resources.getLayout(Resources.java:1082)
at android.view.LayoutInflater.inflate(LayoutInflater.java:412)
at com.android.internal.app.AlertController.setupView(AlertController.java:479)
at com.android.internal.app.AlertController.installContent(AlertController.java:236)
at android.app.AlertDialog.onCreate(AlertDialog.java:356)
at android.app.Dialog.dispatchOnCreate(Dialog.java:373)
at android.app.Dialog.show(Dialog.java:274)
at com.example.ahmed_samir.dialog.MainActivity.showDialog(MainActivity.java:65)
at com.example.ahmed_samir.dialog.MainActivity.onCreate(MainActivity.java:42)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.io.FileNotFoundException:
at android.content.res.AssetManager.openXmlAssetNative(Native Method)
at android.content.res.AssetManager.openXmlBlockAsset(AssetManager.java:485)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2678)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2651)
at android.content.res.Resources.getLayout(Resources.java:1082)
at android.view.LayoutInflater.inflate(LayoutInflater.java:412)
at com.android.internal.app.AlertController.setupView(AlertController.java:479)
at com.android.internal.app.AlertController.installContent(AlertController.java:236)
at android.app.AlertDialog.onCreate(AlertDialog.java:356)
at android.app.Dialog.dispatchOnCreate(Dialog.java:373)
at android.app.Dialog.show(Dialog.java:274)
at com.example.ahmed_samir.dialog.MainActivity.showDialog(MainActivity.java:65)
at com.example.ahmed_samir.dialog.MainActivity.onCreate(MainActivity.java:42)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
我更新的代碼:
public class MainActivity extends Activity {
//mobily
private Button button;
private AlertDialog myDialog;
private TextView result;
private SharedPreferences prefs;
private String prefName = "report";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText input = new EditText(this);
button = (Button) findViewById(R.id.button1);
result = (TextView) findViewById(R.id.tv);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showDialog();
}
});
showDialog();
}
public void showDialog() {
if (myDialog == null) {
myDialog = new AlertDialog.Builder(this)
.setTitle("ID Number")
.setMessage("Please enter your ID")
.setView(input)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//your code
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.create();
}
myDialog.show();
}
}
您收到該錯誤是因為您將EditText
輸入設置為不同的Dialog
因為每次單擊按鈕時都會創建一個新的Dialog
因此錯誤。
您的代碼第一次運行時,您的輸入被設置為 dialog1 的視圖,但是當您第二次調用它時,您告訴EditText
被設置為 dialog2 中的視圖,但它已經有一個父級,即 dialog1。
而是設置 AlertDialog 的全局變量,然后僅當對話框不存在時才創建對話框。 像這樣的東西:
private AlertDialog myDialog;
public void showDialog() {
if (myDialog == null) {
myDialog = new AlertDialog.Builder(this)
.setTitle("ID Number")
.setMessage("Please enter your ID")
.setView(input)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//your code
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.create();
}
myDialog.show();
}
然后在您的onClick
調用showDialog()
方法,而不是每次都重新創建對話框。
還要注意我如何調用構建器中的每個方法,命令是鏈接的,這是Builder
一次輕松調用多個方法然后創建的點。
正如例外所說,您的警報仍然附加到視圖中。 嘗試關閉正面按鈕和負面按鈕上的對話框。
經過大量研究,我找到了我的答案,我想與她分享我的知識和她我的完整代碼和 xml 文件,希望它有用
public class NationalId extends Activity {
final Context context = this;
private Button button;
private TextView result;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_national_id);
// components from main.xml
button = (Button) findViewById(R.id.button1);
result = (TextView) findViewById(R.id.tv1);
// add button listener
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// get prompts.xml view
LayoutInflater li = LayoutInflater.from(context);
View promptsView = li.inflate(R.layout.prompts, null);
android.app.AlertDialog.Builder alertDialogBuilder = new android.app.AlertDialog.Builder(
context);
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
final EditText userInput = (EditText) promptsView
.findViewById(R.id.editTextDialogUserInput);
// set dialog message
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// get user input and set it to result
// edit text
result.setText(userInput.getText());
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
// create alert dialog
android.app.AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
}
}
我的主要 xml 文件
<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"
tools:context=".MainActivity" >
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="47dp"
android:ems="10"
android:inputType="phone"
android:hint="yourid"
>
<requestFocus />
</TextView>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv1"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:text="Edit"
/>
最后我的提示 xml 布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="YOUR ID : "
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editTextDialogUserInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
>
<requestFocus />
</EditText>
我最誠摯的問候..
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.