2017-02-10 173 views
2

应用程序停止与该日志猫错误,并检查我的代码,很多次都没有搞清楚什么问题 我猜它或许真的我做了错误的,我的布局,但没有错 感谢推进警报对话框,在启动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> 

我最诚挚的问候..

0

由于例外说,你的警报仍然附加到视图。 尝试解除正面按钮上的对话框以及否定。

1

由于您将您的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()方法,而不是每次都重新创建对话框。

还注意到我是如何在构建器中调用每个方法的,这些命令被链接在一起,这是一个可以一次调用多个方法然后创建的点。

+0

还是我得到了一些错误:java.lang.IllegalStateException:无法执行活动 –

+0

这是不够的,知道什么是错的方法。你必须发布你的更新代码。 – Pztar

+0

我已经更新了我的代码,在此先感谢.. –