How can I call a layout after clicking a button?

Go To StackoverFlow.com

0

I am having a difficulty displaying a layout. Here is my intention: 1. User clicks Button 2. A message pops up and user selects between Yes or No 3. When yes is clicked a layout named "dialpad" will be displayed.

But when I click "yes" it says "Unfortunately, has stopped." Can anyone help me ? I do know there's a problem on this line...

ViewStub stub = (ViewStub)findViewById(R.id.dialpad);
stub.setVisibility(View.VISIBLE);

Thank you for your help !

Here's the code:

package com.example; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewStub; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class ExampleGUIDialogActivity extends Activity { final Context context = this; private Button button; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button = (Button) findViewById(R.id.buttonAlert); // add button listener button.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder( context); // set title alertDialogBuilder.setTitle("Your Title"); // set dialog message alertDialogBuilder .setMessage("Click yes to exit!") .setCancelable(false) .setPositiveButton("Yes",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int id) { // if this button is clicked, close // current activity // launch dialer ViewStub stub = (ViewStub)findViewById(R.id.dialpad); stub.setVisibility(View.VISIBLE); //stub.inflate(); //View mDialerView = (View)findViewById(R.id.dtmf_twelve_key_dialer_view); //mDialerView.bringToFront(); ExampleGUIDialogActivity.this.finish(); } }) .setNegativeButton("No",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int id) { // if this button is clicked, just close // the dialog box and do nothing dialog.cancel(); } }); // create alert dialog AlertDialog alertDialog = alertDialogBuilder.create(); // show it alertDialog.show(); } }); } }

2012-04-03 21:21
by user1296153
Can you please post some code? What is the error specifically - Davek804 2012-04-03 21:24
It will be very difficult to help you without seeing any code. Don't you think so - Enrique Marcos 2012-04-03 21:27
remove this line ExampleGUIDialogActivity.this.finish();Win Myo Htet 2012-04-03 21:49


0

Try this

ViewStub stub = (ViewStub)findViewById(R.id.dialpad);
if (stub != null){
    stub.setVisibility(View.VISIBLE);
}

Can you provide any logcat info???

2012-04-03 21:29
by petey
http://pastebin.com/C00Ymgy - user1296153 2012-04-03 22:32


0

Well, one problem is that you're inflating the ViewStub and then immediately exiting your Activity, which will cause all kinds of problems; first among them, your user will never see the new layout!

I think you may want to launch a new Activity which has as its content view the layout you're trying to inflate, and allow the user to perform some action based on it... or perhaps you want to call dialog.dismiss() instead?

2012-04-03 21:33
by Jon O
I removed the ExampleGuiDialogActivity.this.finish() and tried try catch around setVisibility and the application didn't crash, but didn't show the layout - user1296153 2012-04-03 22:34