Issue
I'm using this tutorial to create a custom dialog which should be shown when I click on the "Create New Folder" from a spinner. I get inside the OnItemsSelected method, I check "Create New Folder" option was clicked, and then I want to show a dialog to introduce the name of the new folder. I'm doing it like this:
res/layout/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 File
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();
}
}
I'm getting a ResourceNotFoundException. This is the 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)
As you can imagine, the resource ID #0x7f05000e matches R.id.dialog, I've checked it:
inside R.java
public static final int dialog = 0x7f05000e;
Any idea what can be the problem?
UPDATE I forgot to say the Exception is thrown on this line:
dialog.setContentView(R.id.dialog);
Solution
This error occurred because you try to do findViewById
on view(s)
that not exists in your Activity
, but exists in your dialog.
Change this code :
Button createButton = (Button) findViewById(R.id.button_create_dialog);
Button cancelButton = (Button) findViewById(R.id.button_cancel_dialog);
To :
Button createButton = (Button) dialog.findViewById(R.id.button_create_dialog);
Button cancelButton = (Button) dialog.findViewById(R.id.button_cancel_dialog);
Those buttons are not in the Activity
, so you cant just call findViewById
because it will try to find the button's
id in the Activity's
XML. Instead, call dialog.findViewById
to tell the code to find the button
in your dialog.
UPDATE (sorry no code tag, i update from phone)
You should use R.layout.dialog instead of R.id.dialog in your setContentView method.
Answered By - Blaze Tama
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.