Losing OnClickListener when switching from one layout to ano

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Losing OnClickListener when switching from one layout to ano

Postby pslloo » Sun Jun 14, 2009 9:56 pm

I wonder if anyone can help me. When I switch from one layout (main_image in method onCreate) to another layout (create_db in method createNewDatabase) and then back to the original layout (main_image) the OnClickListener for the buttons in layout main_image does not work anymore. To switch back to the original layout I call setContentView(R.layout.main_image) (cancelButton in method createNewDatabase) in method createNewDatabase. The layout seems to be displayed correctly but when I click the button (createNewDbButton) nothing happens.

Code attached
package com.dynastysoftware.PhotoMemories;


import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;
import android.view.View;
import android.database.sqlite.*;

public class PhotoMemories extends Activity {
/** Called when the activity is first created. */

Button createNewDbButton;

@Override
public void onCreate(Bundle savedInstanceState) {

// final TextView instructionsLabel;


super.onCreate(savedInstanceState);
setContentView(R.layout.main_image);

// instructionsLabel = (TextView)findViewById(R.id.instructionsLabel);
// textLabel.setTextColor(this.getResources().getColor(R.color.black));

createNewDbButton = (Button)findViewById(R.id.createNewDbButton);
createNewDbButton.setOnClickListener(new Button.OnClickListener() {
public void onClick (View v) {
// textLabel.setText(text1);
createNewDatabase();
}
});

final Button button2 = (Button)findViewById(R.id.testButton2);
button2.setOnClickListener(new Button.OnClickListener() {
public void onClick (View v) {
createNewDatabase();
}
});

final Button button3 = (Button)findViewById(R.id.testButton3);
button3.setOnClickListener(new Button.OnClickListener() {
public void onClick (View v) {
}
});

final Button button4 = (Button)findViewById(R.id.testButton4);
button4.setOnClickListener(new Button.OnClickListener() {
public void onClick (View v) {
}
});


// Quit the application
final Button quitButton = (Button)findViewById(R.id.quitButton);
quitButton.setOnClickListener(new Button.OnClickListener() {
public void onClick (View v) {
finish();
}
});



}

public void createNewDatabase () {

final TextView dbNameLabel, dbDescLabel;
EditText dbName, dbDesc;

setContentView(R.layout.create_db);

// dbName is the name of the photo album;
// SQLiteDatabase dbName = openOrCreateDatabase ("Hello", MODE_PRIVATE, null);
// dbName.execSQL();
dbNameLabel = (TextView)findViewById(R.id.dbNameLabel);
dbName = (EditText)findViewById(R.id.dbName);
dbName.requestFocus();

dbDescLabel = (TextView)findViewById(R.id.dbDescLabel);
dbDesc = (EditText)findViewById(R.id.dbDesc);

final Button okButton = (Button)findViewById(R.id.okButton);
okButton.setOnClickListener(new Button.OnClickListener() {
public void onClick (View v) {
}
});
final Button cancelButton = (Button)findViewById(R.id.cancelButton);
cancelButton.setOnClickListener(new Button.OnClickListener() {
public void onClick (View v) {
setContentView(R.layout.main_image);
//createNewDbButton.requestFocus();
}
});
}




pslloo
pslloo
pslloo
Freshman
Freshman
 
Posts: 4
Joined: Tue Jun 09, 2009 10:56 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 4 guests