2017-02-10 7 views
2

アプリの停止はそのログで始まります。cat errorsと私のコードを何度も確認せずに何を問題にしますか? 私のレイアウトは間違っていました。アンドロイドの警告ダイアログonclicklistener

を進めるマイ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(); 
} 

} 

答えて

1

は、研究の多くの後、私は私の答えを見つけたと私は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> 
java.lang.IllegalStateException:間違っているかを知るには十分ではありません活動
0

あなたのアラートは依然としてビューに添付されています。 否定と同様に、正のボタンでダイアログを閉じることをお試しください。

1

Dialogの入力を別のDialogに設定しているため、このエラーが発生します。これは、ボタンをクリックするたびに新しいDialogが作成されるためです。

コードが初めて実行されるとき、入力はdialog1のビューとして設定されますが、もう一度呼び出すとEditTextがdialog2のビューとして設定されていると伝えられていますが、ダイアログ1。

代わりにAlertDialogのグローバル変数を設定し、ダイアログがまだ存在しない場合にのみダイアログを作成してください。このような何か:あなたのonClick呼び出しで、その後

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(); 
    } 

showDialog()方法の代わりに、ダイアログを毎回再作成。

ビルダーの各メソッドをどのように呼び出すかは、コマンドが連鎖していて、簡単に複数のメソッドを簡単に呼び出すことができるBuilderのポイントです。

+0

は、まだ私はいくつかのエラーを得ました。更新されたコードを投稿する必要があります。 –

+0

の方法を実行できませんでした

よろしく..

Pztar

+0

私は前もってコードを更新しました.. –

関連する問題