dialog中的值传到activity时出现空指针

YXTS122 2018-02-27 02:09:57
dialog中edittext的值传到activity中的button上时出现空指针,怎么解决
public class FunGamepad extends Activity implements OnClickListener {

private Button btn1, btnSet;


String Str_btn_1;
EditText setbtn_et_1;



@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO 自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.gamepad);

btnSet = (Button) findViewById(R.id.gameepad_btnSet);
btn1 = (Button) findViewById(R.id.gameepad_Btn1);
setbtn_et_1 = (EditText) findViewById(R.id.gamepad_setbtn_et_1);



btnSet.setOnClickListener(this);
btn1.setOnClickListener(this);

}




@Override
// 按钮监听
public void onClick(View v) {
// TODO 自动生成的方法存根
switch (v.getId()) {

case R.id.gameepad_btnSet: Gamepad_BtnSet();
break;

}
}

public void Gamepad_BtnSet() {


// 创建选择"按键设置"后显示的对话框
AlertDialog.Builder builder1 = new Builder(
FunGamepad.this);
LayoutInflater layoutInflater = LayoutInflater
.from(FunGamepad.this);
View gamepad_setbtn = layoutInflater.inflate(
R.layout.gamepad_setbtn, null);

builder1.setTitle("设置按钮")
.setView(gamepad_setbtn)
.setPositiveButton(
"确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int i) {
// 按下确定以后进行的操作
try {
// 获得设置按钮文本框中的文字
Str_btn_1 = setbtn_et_1.getText().toString();

// 按钮上的文字换成设置后的文字
btn1.setText(Str_btn_1);
}
catch (Exception e)
{
Toast.makeText(getApplication(),e.toString() ,Toast.LENGTH_LONG).show();
}

}
})
.setNegativeButton(
"取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(
DialogInterface dialog,int i) {
// 按下取消以后关闭
dialog.dismiss();
}
}).create().show();
}



}



...全文
870 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
YXTS122 2018-09-01
  • 打赏
  • 举报
回复
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.top.MainActivity"
android:gravity="center_horizontal"
android:orientation="vertical" >

<LinearLayout
android:layout_marginTop="30dp"
android:paddingLeft="45dp"
android:paddingRight="45dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textSize="20sp"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content"
android:text="用户名"/>
<TextView
android:textSize="20sp"
android:id="@+id/user_userXinxi"
android:layout_width="0dp"
android:layout_weight="4"
android:layout_height="wrap_content"
android:text="userName"/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textSize="20sp"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content"
android:text="头像"/>
<LinearLayout
android:textSize="20sp"
android:layout_width="0dp"
android:layout_weight="4.00"
android:gravity="left"
android:layout_height="76dp">

<ImageView
android:id="@+id/img_userXinxi"
android:src="@drawable/ic_launcher"
android:layout_width="90dp"
android:layout_height="76dp"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textSize="20sp"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content"
android:text="学校"/>
<EditText
android:id="@+id/school_userXinxi"
android:lines="1"
android:textSize="20sp"
android:layout_width="0dp"
android:layout_weight="4"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textSize="20sp"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content"
android:text="院系"/>
<EditText
android:id="@+id/yuanxi_userXinxi"
android:lines="1"
android:textSize="20sp"
android:layout_width="0dp"
android:layout_weight="4"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textSize="20sp"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="wrap_content"
android:text="手机号"/>
<EditText
android:id="@+id/phone_userXinxi"
android:textSize="20sp"
android:layout_width="0dp"
android:lines="1"
android:layout_weight="4"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textSize="20sp"
android:layout_width="0dp"
android:layout_weight="4"
android:layout_height="wrap_content"
android:text=" "/>
<EditText
android:id="@+id/yanZhengMa_userXinxi"
android:textSize="15sp"
android:hint="手机验证码"
android:lines="1"
android:layout_width="0dp"
android:layout_weight="4"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btnYZM_userXinxi"
android:text="发送验证码"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<TextView
android:id="@+id/tijiao_userXinxi"
android:text="提交"
android:textSize="30dp"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />


</LinearLayout>

yangshuaiMAX 2018-03-01
  • 打赏
  • 举报
回复
空指针是因为你的setbtn_et_1为空吧,你要去你的Dialog里面的去findViewById 所以你修改成setbtn_et_1 = (EditText)gamepad_setbtn.findViewById(R.id.gamepad_setbtn_et_1); 位置修改到Dialog下面.
usecf 2018-02-27
  • 打赏
  • 举报
回复
你对话框中LayoutInflater layoutInflater = LayoutInflater .from(FunGamepad.this); 布局变了 弹框出的edittext是另外一个了,不是你R.layout.gamepad布局中的了 应该用R.layout.gamepad_setbtn中的edittext去传值 比如: R.layout.gamepad_setbtn.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <EditText android:layout_width="match_parent" android:layout_height="100dp" android:id="@+id/gamepad_setbtn_et_2" android:text="et5555"></EditText> </LinearLayout> FunGamepad.java EditText setbtn_et_2; public void Gamepad_BtnSet() { // 创建选择"按键设置"后显示的对话框 AlertDialog.Builder builder1 = new Builder(FunGamepad.this); LayoutInflater layoutInflater = LayoutInflater.from(FunGamepad.this); View gamepad_setbtn = layoutInflater.inflate(R.layout.gamepad_setbtn, null); setbtn_et_2 = (EditText)gamepad_setbtn.findViewById(R.id.gamepad_setbtn_et_2); builder1.setTitle("设置按钮") .setView(gamepad_setbtn) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int i) { // 按下确定以后进行的操作 try { // 获得设置按钮文本框中的文字 Str_btn_1 = setbtn_et_2.getText().toString(); // 按钮上的文字换成设置后的文字 btn1.setText(Str_btn_1); } catch (Exception e) { Toast.makeText(getApplication(), e.toString(), Toast.LENGTH_LONG).show(); } } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int i) { // 按下取消以后关闭 dialog.dismiss(); } }).create().show(); }
  • 打赏
  • 举报
回复
你把 setbtn_et_1和btn1设成 static试试

80,361

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧