The problem of Create Dialog

Put your problem here if it does not fit any of the other categories.

The problem of Create Dialog

Postby NeoLiu » Wed Jan 23, 2008 10:27 am

I had added a dialog in the Activity

private static EditText Red;
private static EditText Green;
private static EditText Blue;

protected void onCreate(Bundle icicle) {
super.onCreate(icicle);

requestWindowFeature(Window.FEATURE_NO_TITLE);
extras = getIntent().getExtras();

mImagePSView = new ImageDrawView(this, extras);
setContentView(R.layout.layout1);
}


public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);

menu.add(0, MENU_CREAT, R.string.menu_create);

return true;
}



public boolean onOptionsItemSelected(Menu.Item item) {
switch (item.getId()) {
case MENU_CREAT:
CreateDialog();
return true;
return super.onOptionsItemSelected(item);
}



public void CreatePalette()
{
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.layout2);
dialog.setTitle("Set RGB");

Red = (EditText)findViewById(R.id.red);
Green = (EditText)findViewById(R.id.green);
Blue = (EditText)findViewById(R.id.blue);

Button button = (Button)findViewById(R.id.palette_ok);
if (button == null)
{
Paledialog.show();
return;
}
button.setOnClickListener(mPaleOkListener);
button = (Button)findViewById(R.id.palette_cancel);
button.setOnClickListener(mPaleCancelListener);

dialog.show();
}

layout1.xml is a normal layout

layout2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="70dip"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView id="@+id/labelred"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Red:"/>

<TextView id="@+id/labelgreen"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft = "@+id/green"
android:layout_alignTop="@+id/labelred"
android:text="Green:"/>

<TextView id="@+id/labelblue"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft = "@+id/blue"
android:layout_alignTop="@+id/labelred"
android:text="Blue:"/>

<EditText id="@+id/red"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"
android:layout_below="@id/labelred"/>

<EditText id="@+id/green"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:layout_toRight = "@+id/red"
android:layout_alignTop="@id/red"
android:background="@android:drawable/editbox_background"/>

<EditText id="@+id/blue"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:layout_toRight = "@+id/green"
android:layout_alignTop="@id/red"
android:background="@android:drawable/editbox_background"/>

<Button id="@+id/palette_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/red"
android:layout_alignParentRight="true"
android:text="OK" />

<Button id="@+id/palette_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeft="@+id/palette_ok"
android:layout_alignTop="@+id/palette_ok"
android:text="Cancel" />
</RelativeLayout>


Follow the code i can create the dialog and display it
but used Red = (EditText)findViewById(R.id.red);
the findViewById can't find the View, the red is null
the all findViewById are null, i don't know why the view is null
Please help me , thanks
NeoLiu
Junior Developer
Junior Developer
 
Posts: 17
Joined: Mon Jan 07, 2008 3:27 am

Top

Postby Ishtar » Wed Jan 23, 2008 10:59 am

Hi

Let's see. The contentview you set in activity is layout1.xml and the contentview in your dialog is layout2.xml. The buttons are in layout2.xml. I assume CreatePalette is a method of your activity. (Correct me if im wrong) If so, you're using the findViewById method of the activity (and this can't find your buttons in its layout1.xml!). You could try:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. Red = (EditText)dialog.findViewById(R.id.red);
  3.  
  4.  Green = (EditText)dialog.findViewById(R.id.green);
  5.  
  6.  Blue = (EditText)dialog.findViewById(R.id.blue);
  7.  
  8.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

However this looks to easy to just work.. Maybe you have to try something like this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class PaletteDialog extends Dialog
  3.  
  4. {
  5.  
  6.  protected void onStart()
  7.  
  8.  {
  9.  
  10.   super.onStart();
  11.  
  12.   setContentView(R.layout.layout2);
  13.  
  14.   setTitle("Set RGB");
  15.  
  16.  
  17.  
  18.   Red = (EditText)findViewById(R.id.red);
  19.  
  20.   Green = (EditText)findViewById(R.id.green);
  21.  
  22.   Blue = (EditText)findViewById(R.id.blue);
  23.  
  24.   //etc, etc, etc..
  25.  
  26.  }
  27.  
  28. }
  29.  
  30.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


The findViewById method in Dialog class:
public View findViewById(int id)
Finds a view that was identified by the id attribute from the XML that was processed in onStart().


That's why i suggest overriding onStart :wink:
Good luck

Ishtar
Ishtar
Experienced Developer
Experienced Developer
 
Posts: 50
Joined: Wed Jan 16, 2008 10:22 pm
Location: Netherlands

Re: The problem of Create Dialog

Postby NeoLiu » Thu Jan 24, 2008 1:45 am

Hi Ishtar

This method is available . Thanks :D
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Red = (EditText)dialog.findViewById(R.id.red);
  2.  
  3.  Green = (EditText)dialog.findViewById(R.id.green);
  4.  
  5.  Blue = (EditText)dialog.findViewById(R.id.blue);
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
NeoLiu
Junior Developer
Junior Developer
 
Posts: 17
Joined: Mon Jan 07, 2008 3:27 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 24 guests