我在显示布局时遇到了困难。这是我的意图: 1.用户点击按钮 2.弹出一条消息,用户在是或否之间选择 3.单击是时,将显示名为“dialpad”的布局。
但是,当我点击“是”时,它说“不幸的是,已经停止了”。谁能帮我 ? 我知道这条线路有问题......
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();
}
});
}
}
答案 0 :(得分:0)
试试这个
ViewStub stub = (ViewStub)findViewById(R.id.dialpad);
if (stub != null){
stub.setVisibility(View.VISIBLE);
}
你能提供任何logcat信息???
答案 1 :(得分:0)
嗯,有一个问题是你正在膨胀ViewStub
,然后立即退出你的活动,这将导致各种各样的问题;首先,您的用户永远不会看到新的布局!
我想你可能想要推出一个新的Activity
,它的内容视图包含你想要膨胀的布局,并允许用户根据它执行某些操作...或者你想要的改为拨打dialog.dismiss()
?