自定义对话框,setContentView抛出ResourceNotFoundException

时间:2014-11-07 16:19:54

标签: android dialog android-resources r.java-file

我使用this tutorial创建了一个自定义对话框,当我点击"创建新文件夹"来自一个微调器。我进入OnItemsSelected方法,我检查"创建新文件夹"单击选项,然后我想显示一个对话框来介绍新文件夹的名称。我是这样做的:

RES /布局/ dialog.html

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible" >

<EditText 
    android:id="@+id/editText_dialog"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:hint="@string/editText_dialog"
    android:layout_alignParentTop="true"
    android:layout_marginTop="15dip"
    android:layout_marginBottom="15dip"/>

<Button 
    android:id="@+id/button_create_dialog"
    android:text="@string/button_create_dialog"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/editText_dialog"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="50dip"
    android:background="#176CEC"
    android:textColor="#fff"
    android:textStyle="bold"
    android:textAppearance="?android:attr/textAppearanceLarge"/>

<Button 
    android:id="@+id/button_cancel_dialog"
    android:text="@string/button_cancel_dialog"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/editText_dialog"
    android:layout_alignParentRight="true"
    android:layout_marginRight="50dip"
    android:background="#176CEC"
    android:textColor="#fff"
    android:textStyle="bold"
    android:textAppearance="?android:attr/textAppearanceLarge"/>

</RelativeLayout>

Java文件

public void onItemSelected(AdapterView<?> parent, View view, int position,long id){
    if (parent.getItemAtPosition(position).toString().matches("Create New Folder")){
        final Dialog dialog = new Dialog(this);
        try {
            dialog.setContentView(R.id.dialog);
            dialog.setTitle("Create New Folder");
        }
        catch (Exception e){
            StringWriter errors = new StringWriter();
            e.printStackTrace(new PrintWriter(errors));
            Log.i("Exception", errors.toString());
        }


        Button createButton = (Button) findViewById(R.id.button_create_dialog);
        Button cancelButton = (Button) findViewById(R.id.button_cancel_dialog);

        createButton.setOnClickListener(new Button.OnClickListener(){
            public void onClick(View v){
                EditText newFolderName = (EditText) findViewById(R.id.editText_dialog);
                String folderName = newFolderName.getText().toString().trim();
                if (folderName.length() > 0) {
                    // do stuff
                    dialog.dismiss();
                }
            }
        });

        cancelButton.setOnClickListener(new Button.OnClickListener(){
            public void onClick(View v){
                dialog.dismiss();
            }
        });
        dialog.show();
    }
}

我收到了ResourceNotFoundException。这是stackTrace:

11-07 17:11:49.075: I/Exception(8315): android.content.res.Resources$NotFoundException: Resource ID #0x7f05000e
11-07 17:11:49.075: I/Exception(8315):  at android.content.res.Resources.getValue(Resources.java:1118)
11-07 17:11:49.075: I/Exception(8315):  at android.content.res.Resources.loadXmlResourceParser(Resources.java:2304)
11-07 17:11:49.075: I/Exception(8315):  at android.content.res.Resources.getLayout(Resources.java:934)
11-07 17:11:49.075: I/Exception(8315):  at android.view.LayoutInflater.inflate(LayoutInflater.java:395)
11-07 17:11:49.075: I/Exception(8315):  at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
11-07 17:11:49.075: I/Exception(8315):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
11-07 17:11:49.075: I/Exception(8315):  at android.app.Dialog.setContentView(Dialog.java:471)
11-07 17:11:49.075: I/Exception(8315):  at com.example.example.MainActivity.onItemSelected(MainActivity.java:94)
11-07 17:11:49.075: I/Exception(8315):  at android.widget.AdapterView.fireOnSelected(AdapterView.java:892)
11-07 17:11:49.075: I/Exception(8315):  at android.widget.AdapterView.access$200(AdapterView.java:49)
11-07 17:11:49.075: I/Exception(8315):  at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:860)
11-07 17:11:49.075: I/Exception(8315):  at android.os.Handler.handleCallback(Handler.java:730)
11-07 17:11:49.075: I/Exception(8315):  at android.os.Handler.dispatchMessage(Handler.java:92)
11-07 17:11:49.075: I/Exception(8315):  at android.os.Looper.loop(Looper.java:137)
11-07 17:11:49.075: I/Exception(8315):  at android.app.ActivityThread.main(ActivityThread.java:5103)
11-07 17:11:49.075: I/Exception(8315):  at java.lang.reflect.Method.invokeNative(Native Method)
11-07 17:11:49.075: I/Exception(8315):  at java.lang.reflect.Method.invoke(Method.java:525)
11-07 17:11:49.075: I/Exception(8315):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-07 17:11:49.075: I/Exception(8315):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-07 17:11:49.075: I/Exception(8315):  at dalvik.system.NativeStart.main(Native Method)

您可以想象,资源ID#0x7f05000e与R.id.dialog匹配,我已经检查过了:

R.java里面的

 public static final int dialog = 0x7f05000e;

知道可能出现什么问题?

更新 我忘了说Exception被抛到了这一行:

dialog.setContentView(R.id.dialog);

1 个答案:

答案 0 :(得分:1)

发生此错误的原因是您尝试在findViewByIdview(s) Activity Button createButton = (Button) findViewById(R.id.button_create_dialog); Button cancelButton = (Button) findViewById(R.id.button_cancel_dialog); 中存在 ,但您的对话框中存在

更改此代码:

    Button createButton = (Button) dialog.findViewById(R.id.button_create_dialog);
    Button cancelButton = (Button) dialog.findViewById(R.id.button_cancel_dialog);

致:

Activity

这些按钮不在findViewById中,因此您不能只需致电button's,因为它会尝试在{{1}中找到Activity's ID XML。相反,请致电dialog.findViewById告诉代码在对话框中找到button

更新(抱歉没有代码标签,我从手机更新)

您应该在setContentView方法中使用R.layout.dialog而不是R.id.dialog。