[英]Attempt to invoke virtual method (EditText)
我希望我的应用程序在第一个 AlertDialog 中从 user 中获取用户名并将其发布到第二个 AlertDialog 上,但是当我添加以下代码行时,我的应用程序崩溃了:txt.setText(edt.getText().toString());。 它应该将 textview 更改为在第一个 AlertDialog 上给出的 edittext 值。 以下是错误。
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.example.myapplication.MainActivity.Alert01(MainActivity.java:73)
at com.example.myapplication.MainActivity$1.onClick(MainActivity.java:62)
at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
EditText edt;
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
edt = (EditText) findViewById(R.id.edit_username);
txt = (TextView) findViewById(R.id.text02);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
ViewPager viewPager = findViewById(R.id.view_pager);
viewPager.setAdapter(sectionsPagerAdapter);
TabLayout tabs = findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
Alert();
}
public void Alert () {
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this, R.style.MyDialogTheme);
View v = LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog0, null);
builder.setMessage("Welcome ! ");
builder.setView(v);
builder.setPositiveButton("Submit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Alert01();
}
});
AlertDialog alert = builder.create();
alert.show();
}
public void Alert01 (){
txt.setText(edt.getText().toString());
AlertDialog.Builder builder01 = new AlertDialog.Builder(MainActivity.this,R.style.MyDialogTheme);
View v01 = LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog1,null);
builder01.setView(v01);
builder01.setTitle("Congratulations ! ");
AlertDialog alert = builder01.create();
alert.show();
}
}
您需要先对布局进行膨胀,然后在活动中调用 findViewById。
EditText edt
是AlertDialog1布局的一部分TextView txt
是AlertDialog2布局的一部分,所以你可以访问edt
与AlertDialog1的参考和txt
通过AlertDialog2的参考。
更新您的代码,如下所示:
EditText edt;
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
// edt = (EditText) findViewById(R.id.edit_username);<--Remove this line from here
// txt = (TextView) findViewById(R.id.text02);<--Remoe this line from here and paste both findViewById below to setContentView(R.layout.activity_main) method.
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
ViewPager viewPager = findViewById(R.id.view_pager);
viewPager.setAdapter(sectionsPagerAdapter);
TabLayout tabs = findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
Alert();
}
public void Alert () {
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this, R.style.MyDialogTheme);
View v = LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog0, null);
builder.setMessage("Welcome ! ");
builder.setView(v);
edt = (EditText)v.findViewById(R.id.edit_username);
builder.setPositiveButton("Submit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Alert01();
}
});
AlertDialog alert = builder.create();
alert.show();
}
public void Alert01 (){
// txt.setText(edt.getText().toString());<--Remove this line from here and set it below ...
AlertDialog.Builder builder01 = new AlertDialog.Builder(MainActivity.this,R.style.MyDialogTheme);
View v01 = LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog1,null);
builder01.setView(v01);
txt = (TextView) v01.findViewById(R.id.text02);
txt.setText(edt.getText().toString());
builder01.setTitle("Congratulations ! ");
AlertDialog alert = builder01.create();
alert.show();
}
}
膨胀布局后在 Alert() 方法中移动此行
edt = (EditText) v.findViewById(R.id.edit_username);
同时修改 Alert01() 方法
AlertDialog.Builder builder01 = new AlertDialog.Builder(MainActivity.this,R.style.MyDialogTheme);
View v01 = LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog1,null);
builder01.setView(v01);
txt = (TextView) v01.findViewById(R.id.text02);
txt.setText(edt.getText().toString());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.