在 alertdialog 中使用布局(切换按钮),总是 returns nullPointerException
using layout(toggleButton) inside alert dialog, always returns nullPointerExpection
在我的 MainActivity
中,我有一个方法 showPopUp
由按钮的侦听器调用。所以 MainActivity's
布局只包含一个按钮。
ShowPopUp
方法读取按钮状态和 returns null pointer expception
。
activity_toggle
是我的布局,其中包含 toggleButton
。
以下是我的主要活动代码片段。
public class MainActivity extends ActionBarActivity {
ToggleButton toggleButton;
String i;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showAlertDialog(MainActivity.this);
}
public void showAlertDialog(final Context context){
AlertDialog.Builder ab = new AlertDialog.Builder(context);
View v = LayoutInflater.from(context).inflate(R.layout.activity_toggle,null);
ab.setView(v);
ab.setTitle("toggle mera dil toggle meri jaan");
toggleButton = (ToggleButton)findViewById(R.id.toggleButton1);
toggleButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(toggleButton.isChecked()){
i+=i;
Toast.makeText(MainActivity.this,"Button is on "+i,Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(MainActivity.this,"Button is off mode",Toast.LENGTH_SHORT).show();
}
}
});
ab.setPositiveButton("Ok",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"ok button pressed",Toast.LENGTH_SHORT).show();
boolean tButton = toggleButton.isChecked();
if(tButton){
Toast.makeText(MainActivity.this,"Button is off on a ",Toast.LENGTH_SHORT).show();
}
dialog.dismiss();
}
});
ab.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
ab.create().show();
}
}
试试下面的代码。从inflater view中查找view时必须给出View的引用。
toggleButton = (ToggleButton)v.findViewById(R.id.toggleButton1);
View v = LayoutInflater.from(context).inflate(R.layout.activity_toggle,null);
ab.setView(v);
ab.setTitle("toggle mera dil toggle meri jaan");
toggleButton = (ToggleButton)v.findViewById(R.id.toggleButton1);
使用 v
View 对象传递 AlertDialog
的 setView
方法从对话框布局访问 ToggleButton
:
toggleButton = (ToggleButton)v.findViewById(R.id.toggleButton1);
toggleButton = (ToggleButton)findViewById(R.id.toggleButton1);
此行在您的 activity 中查找视图,但此 R.id.toggleButton1
控件在对话框中使用的 activity_toggle.xml
布局中定义。
试试
toggleButton = (ToggleButton)v.findViewById(R.id.toggleButton1);
在我的 MainActivity
中,我有一个方法 showPopUp
由按钮的侦听器调用。所以 MainActivity's
布局只包含一个按钮。
ShowPopUp
方法读取按钮状态和 returns null pointer expception
。
activity_toggle
是我的布局,其中包含 toggleButton
。
以下是我的主要活动代码片段。
public class MainActivity extends ActionBarActivity {
ToggleButton toggleButton;
String i;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showAlertDialog(MainActivity.this);
}
public void showAlertDialog(final Context context){
AlertDialog.Builder ab = new AlertDialog.Builder(context);
View v = LayoutInflater.from(context).inflate(R.layout.activity_toggle,null);
ab.setView(v);
ab.setTitle("toggle mera dil toggle meri jaan");
toggleButton = (ToggleButton)findViewById(R.id.toggleButton1);
toggleButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(toggleButton.isChecked()){
i+=i;
Toast.makeText(MainActivity.this,"Button is on "+i,Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(MainActivity.this,"Button is off mode",Toast.LENGTH_SHORT).show();
}
}
});
ab.setPositiveButton("Ok",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"ok button pressed",Toast.LENGTH_SHORT).show();
boolean tButton = toggleButton.isChecked();
if(tButton){
Toast.makeText(MainActivity.this,"Button is off on a ",Toast.LENGTH_SHORT).show();
}
dialog.dismiss();
}
});
ab.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
ab.create().show();
}
}
试试下面的代码。从inflater view中查找view时必须给出View的引用。
toggleButton = (ToggleButton)v.findViewById(R.id.toggleButton1);
View v = LayoutInflater.from(context).inflate(R.layout.activity_toggle,null);
ab.setView(v);
ab.setTitle("toggle mera dil toggle meri jaan");
toggleButton = (ToggleButton)v.findViewById(R.id.toggleButton1);
使用 v
View 对象传递 AlertDialog
的 setView
方法从对话框布局访问 ToggleButton
:
toggleButton = (ToggleButton)v.findViewById(R.id.toggleButton1);
toggleButton = (ToggleButton)findViewById(R.id.toggleButton1);
此行在您的 activity 中查找视图,但此 R.id.toggleButton1
控件在对话框中使用的 activity_toggle.xml
布局中定义。
试试
toggleButton = (ToggleButton)v.findViewById(R.id.toggleButton1);