0

我在显示布局时遇到了困难。这是我的意图: 1. 用户单击按钮 2. 弹出一条消息,用户在是或否之间进行选择 3. 单击是时,将显示名为“拨号盘”的布局。

但是当我单击“是”时,它会显示“不幸的是,已停止”。谁能帮我 ?我知道这条线有问题...

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

谢谢您的帮助 !

这是代码:

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(); } }); } }

4

2 回答 2

0

尝试这个

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

你能提供任何logcat信息吗???

于 2012-04-03T21:29:57.660 回答
0

嗯,一个问题是你在膨胀ViewStub然后立即退出你的活动,这会导致各种各样的问题;首先,您的用户将永远不会看到新布局!

我认为您可能想要启动一个新Activity的,它的内容视图是您试图膨胀的布局,并允许用户基于它执行一些操作......或者您可能想调用dialog.dismiss()

于 2012-04-03T21:33:18.333 回答