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();
}
});
}
}
ExampleGUIDialogActivity.this.finish();
Win Myo Htet 2012-04-03 21:49
Try this
ViewStub stub = (ViewStub)findViewById(R.id.dialpad);
if (stub != null){
stub.setVisibility(View.VISIBLE);
}
Can you provide any logcat info???
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?