LoginDialog l=new LoginDialog();
l.LoginDialog2(M.this);
我写了个LoginDialog类 如下 结果 老是 报错
public class LoginDialog extends Activity implements OnClickListener {
private Context mcontext=null;
public void LoginDialog2 (Context context) {
this.mcontext = context;
LayoutInflater layoutInflater = LayoutInflater.from(context);
View longinDialogView = layoutInflater.inflate(MResource.getIdByName(getApplication(), "layout", "logindialog"), null);
//获取布局中的控件
EditText mUserName = (EditText)longinDialogView.findViewById( MResource.getIdByName(getApplication(), "id", "edit_username"));
EditText mPassword = (EditText)longinDialogView.findViewById( MResource.getIdByName(getApplication(), "id", "edit_password"));
//创建一个AlertDialog对话框
AlertDialog longinDialog = new AlertDialog.Builder(context)
.setTitle("登录框")
.setView(longinDialogView) //加载自定义的对话框式样
.setPositiveButton("确定", this)
.setNeutralButton("取消", this)
.create();
longinDialog.show();
}
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
}
}
错误提示 java.lang.NullPointerException
自定义对话框不是这么搞啊
MyDialog.java
package com.antex.assist;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.antex.R;
/**
* <h3>package:</h3> com.antex.assist <br>
* <h3>file:</h3> MyDialog.java <br>
*
* <h3>Description:自定义dialog</h3><br>
* <h3>创建时间:</h3>2013-10
*
*
* <pre>
* @author xiaosanyu<br>
* email: 446251495@qq.com<br>
* blog: <a href="http://blog.csdn.net/a87b01c14">http://blog.csdn.net/a87b01c14</a>
* </pre>
*
*/
public class MyDialog extends Dialog {
/**
* 构造函数
*
* @param context
* 上下文环境
* @param theme
* 主题
*/
public MyDialog(Context context, int theme) {
super(context, theme);
}
/**
* 构造函数
*
* @param context
* 上下文环境
*/
public MyDialog(Context context) {
super(context);
}
/**
* Helper class for creating a custom dialog
*/
public static class Builder {
/** 上下文环境 */
private Context context;
/** 标题 */
private String title;
/** 消息内容 */
private String message;
/** 左侧按钮文字 */
private String positiveButtonText;
/** 右侧按钮文字 */
private String negativeButtonText;
/** 中间按钮文字 */
private String neutralButtonText;
/** 子视图 */
private View contentView;
/** 标题图标 */
private int titleimg = 0;
/** 左侧按钮点击监听 */
private DialogInterface.OnClickListener positiveButtonClickListener;
/** 中间按钮点击监听 */
private DialogInterface.OnClickListener neutralButtonClickListener;
/** 右侧按钮点击监听 */
private DialogInterface.OnClickListener negativeButtonClickListener;
/**
* 构造器
*
* @param context
* 上下文环境
*/
public Builder(Context context) {
this.context = context;
}
/**
* Set the Dialog message from String
*
* @param message
* String类型对话框消息
* @return Builder 是个抽象类。用于抽象创建下属dialog等类
*/
public Builder setMessage(String message) {
this.message = message;
return this;
}
/**
* Set the Dialog message from resource
*
* @param message
* int型根据ID获取会话框消息
* @return Builder 是个抽象类。用于抽象创建下属dialog等类
*/
public Builder setMessage(int message) {
this.message = (String) context.getText(message);
return this;
}
/**
* Set the Dialog title from resource
*
* @param title
* int型根据ID获取对话框标题
* @return Builder 是个抽象类。用于抽象创建下属dialog等类
*/
public Builder setTitle(int title) {
this.title = (String) context.getText(title);
return this;
}
/**
* Set the Dialog titleimg from resource
*
* @param titleimg
* 对话框图标
* @return Builder 是个抽象类。用于抽象创建下属dialog等类
*/
public Builder setIcon(int titleimg) {
this.titleimg = titleimg;
return this;
}
/**
* Set the Dialog title from String
*
* @param title
* String型对话框标题
* @return Builder 是个抽象类。用于抽象创建下属dialog等类
*/
public Builder setTitle(String title) {
this.title = title;
return this;
}
/**
* Set a custom content view for the Dialog. If a message is set, the
* contentView is not added to the Dialog...
*
* @param v
* 对话框子视图
* @return Builer 是个抽象类。用于抽象创建下属dialog等类
*/
public Builder setContentView(View v) {
this.contentView = v;
return this;
}
/**
* Set the positive button resource and it's listener
*
* @param positiveButtonText
* 按钮文本
* @param listener
* 按钮点击监听
* @return Builer 是个抽象类。用于抽象创建下属dialog等类
*/
public Builder setPositiveButton(int positiveButtonText,
DialogInterface.OnClickListener listener) {
this.positiveButtonText = (String) context
.getText(positiveButtonText);
this.positiveButtonClickListener = listener;
return this;
}
/**
* Set the positive button text and it's listener
*
* @param positiveButtonText
* 按钮文本
* @param listener
* 按钮点击监听
* @return Builer 是个抽象类。用于抽象创建下属dialog等类
*/
public Builder setPositiveButton(String positiveButtonText,
DialogInterface.OnClickListener listener) {
this.positiveButtonText = positiveButtonText;
this.positiveButtonClickListener = listener;
return this;
}
/**
* Set the negative button resource and it's listener
*
* @param negativeButtonText
* 按钮文本
* @param listener
* 按钮点击监听
* @return Builer 是个抽象类。用于抽象创建下属dialog等类
*/
public Builder setNegativeButton(int negativeButtonText,
DialogInterface.OnClickListener listener) {
this.negativeButtonText = (String) context
.getText(negativeButtonText);
this.negativeButtonClickListener = listener;
return this;
}
/**
* Set the negative button text and it's listener
*
* @param negativeButtonText
* 按钮文本
* @param listener
* 按钮点击监听
* @return Builer 是个抽象类。用于抽象创建下属dialog等类
*/
public Builder setNegativeButton(String negativeButtonText,
DialogInterface.OnClickListener listener) {
this.negativeButtonText = negativeButtonText;
this.negativeButtonClickListener = listener;
return this;
}
/**
* Set the Neutral button resource and it's listener
*
* @param neutralButtonText
* 按钮文本
* @param listener
* 按钮点击监听
* @return Builer 是个抽象类。用于抽象创建下属dialog等类
*/
public Builder setNeutralButton(int neutralButtonText,
DialogInterface.OnClickListener listener) {
this.neutralButtonText = (String) context
.getText(neutralButtonText);
this.negativeButtonClickListener = listener;
return this;
}
/**
* Set the Neutral button text and it's listener
*
* @param neutralButtonText
* 按钮文本
* @param listener
* 按钮点击监听
* @return Builer 是个抽象类。用于抽象创建下属dialog等类
*/
public Builder setNeutralButton(String neutralButtonText,
DialogInterface.OnClickListener listener) {
this.neutralButtonText = neutralButtonText;
this.neutralButtonClickListener = listener;
return this;
}
/**
* Create the custom dialog
*
* @return MyDialog 返回自定义Dialog
*/
public MyDialog create() {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// instantiate the dialog with the custom Theme
final MyDialog dialog = new MyDialog(context, R.style.MyDialog);
View layout = inflater.inflate(R.layout.dialog, new LinearLayout(
context), false);
ImageView titleimage = (ImageView) layout
.findViewById(R.id.dialog_title_image);
TextView tv = (TextView) layout.findViewById(R.id.dialog_msg);
Button positiveButton = (Button) layout
.findViewById(R.id.positiveButton);
Button negativeButton = (Button) layout
.findViewById(R.id.negativeButton);
Button neutralButton = (Button) layout
.findViewById(R.id.neutralButton);
dialog.addContentView(layout, new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
// set the dialog title
if (title != null)
((TextView) layout.findViewById(R.id.dialog_title))
.setText(title);
if (titleimg != 0)
titleimage.setBackgroundResource(titleimg);
else
titleimage.setVisibility(View.GONE);
// set the confirm button
if (positiveButtonText != null) {
positiveButton.setText(positiveButtonText);
if (positiveButtonClickListener != null) {
positiveButton
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
positiveButtonClickListener.onClick(dialog,
DialogInterface.BUTTON_POSITIVE);
}
});
}
} else {
// if no confirm button just set the visibility to GONE
positiveButton.setVisibility(View.GONE);
}
// set the cancel button
if (negativeButtonText != null) {
negativeButton.setText(negativeButtonText);
if (negativeButtonClickListener != null) {
negativeButton
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
negativeButtonClickListener.onClick(dialog,
DialogInterface.BUTTON_POSITIVE);
}
});
}
} else {
// if no confirm button just set the visibility to GONE
negativeButton.setVisibility(View.GONE);
}
// set the neutral button
if (neutralButtonText != null) {
neutralButton.setText(neutralButtonText);
LayoutParams lp = positiveButton.getLayoutParams();
lp.width = convertDIP2PX(80);
positiveButton.setLayoutParams(lp);
RelativeLayout.LayoutParams contentLayoutParams = new RelativeLayout.LayoutParams(
convertDIP2PX(80),
RelativeLayout.LayoutParams.WRAP_CONTENT);
contentLayoutParams.leftMargin = convertDIP2PX(15);
contentLayoutParams.addRule(RelativeLayout.RIGHT_OF,
R.id.neutralButton);
negativeButton.setLayoutParams(contentLayoutParams);
if (neutralButtonClickListener != null) {
neutralButton
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
neutralButtonClickListener.onClick(dialog,
DialogInterface.BUTTON_POSITIVE);
}
});
}
} else {
// if no neutral button just set the visibility to GONE
neutralButton.setVisibility(View.GONE);
}
// set the content message
if (message != null) {
tv.setText(message);
}
if (contentView != null) {
// add the contentView to the dialog body
if (message == null || message == "")
tv.setVisibility(View.GONE);
LinearLayout ll = (LinearLayout) layout
.findViewById(R.id.content);
// WindowManager manager = (WindowManager) context
// .getSystemService(Context.WINDOW_SERVICE);
// Display display = manager.getDefaultDisplay();
// int width = display.getWidth();
LinearLayout.LayoutParams contentLayoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
contentLayoutParams.weight = 0;
// contentLayoutParams.gravity=Gravity.CENTER_VERTICAL;
contentLayoutParams.leftMargin = 30;
contentLayoutParams.rightMargin = 30;
contentLayoutParams.topMargin = 10;
ll.addView(contentView, contentLayoutParams);
}
dialog.setContentView(layout);
return dialog;
}
/**
* 转换dip为px
*
* @param dip
* dip大小
* @return int dip单位转换成px大小
*/
public int convertDIP2PX(int dip) {
float scale = context.getResources().getDisplayMetrics().density;
return (int) (dip * scale + 0.5f);
}
}
}
布局文件
dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/dialog_bg"
android:gravity="center_horizontal"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="20dip" >
<ImageView
android:id="@+id/dialog_title_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginTop="15dip"
android:background="@drawable/dialog_title_image"
android:contentDescription="@string/button_back" />
<TextView
android:id="@+id/dialog_title"
style="@style/DialogText.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="19dp"
android:layout_marginTop="20dip"
android:layout_toRightOf="@+id/dialog_title_image" />
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="1dip"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:layout_marginTop="5dip"
android:background="@drawable/lins" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:fillViewport="true" >
<LinearLayout
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/dialog_msg"
style="@style/DialogText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginRight="5dip"
android:layout_marginTop="10dip" />
</LinearLayout>
</ScrollView>
<RelativeLayout
android:id="@+id/drl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="bottom|center_horizontal"
android:paddingBottom="30dip"
android:paddingTop="10dip" >
<Button
android:id="@+id/positiveButton"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="@drawable/button_selector"
android:text="@string/button_ok"
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold" />
<Button
android:id="@+id/neutralButton"
android:layout_width="80dip"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_toRightOf="@id/positiveButton"
android:background="@drawable/button_selector"
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold" />
<Button
android:id="@+id/negativeButton"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:layout_marginLeft="30dip"
android:layout_toRightOf="@id/neutralButton"
android:background="@drawable/button_selector"
android:text="@string/button_cancel"
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>
</LinearLayout>
style
<style name="MyDialog" parent="@android:Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@drawable/dialog_bg</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
<style name="DialogText">
<item name="android:textColor">#FF000000</item>
<item name="android:textSize">20sp</item>
<item name="android:textStyle">bold</item>
</style>
<style name="DialogText.Title">
<item name="android:textSize">25sp</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">#1E90FF</item>
</style>
调用
MyDialog.Builder customBuilder = new MyDialog.Builder(mContext0);
LayoutInflater layout =getLayoutInflater();
View longinDialogView = layout.inflate(R.layout.logindialog, null, false);
//获取布局中的控件
EditText mUserName = (EditText)longinDialogView.findViewById( MResource.getIdByName(getApplication(), "id", "edit_username"));
EditText mPassword = (EditText)longinDialogView.findViewById( MResource.getIdByName(getApplication(), "id", "edit_password"));
customBuilder.setTitle("").setContentView(view).setPositiveButton(“确定”, new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).setNegativeButton(“取消”, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
Dialog dialog = customBuilder.create();
dialog.show();
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。