Java 应用程序上的android.text.Editable android.widget.EditText.getText()单击按钮
我对这个代码有问题Java 应用程序上的android.text.Editable android.widget.EditText.getText()单击按钮,java,android,android-dialog,Java,Android,Android Dialog,我对这个代码有问题 public class AgendaActivity extends AppCompatActivity { private DbManager db=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.agendalayout); db=new
public class AgendaActivity extends AppCompatActivity {
private DbManager db=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.agendalayout);
db=new DbManager(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.addxml, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
// User chose the "Settings" item, show the app settings UI...
return true;
case R.id.action_add:
final Dialog d = new Dialog(this);
d.setTitle("Prova");
d.setCancelable(false);
d.setContentView(R.layout.popupdialog);
Window window = d.getWindow();
window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
d.show();
//eventi Button
Button b = (Button) d.findViewById(R.id.ok_action);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
EditText p1 = (EditText) findViewById(R.id.Ora_inizio);
EditText p2 = (EditText) findViewById(R.id.Ora_fine);
EditText p3 = (EditText) findViewById(R.id.pause);
String p1Value = p1.getText().toString();
String p2Value = p2.getText().toString();
String p3Value = p3.getText().toString();
try {
int p1Final = Integer.parseInt(p1Value);
} catch (Exception e) {
Toast exceptionAgenda = Toast.makeText(AgendaActivity.this,
"Uno dei due campi o entrambi sono vuoti!",
Toast.LENGTH_LONG);
exceptionAgenda.show();
}
//db.save(primoValore,secondoValore,terzoValore);
}
});
default:
}
return true;
};
}
}
});
Button b2=(Button) d.findViewById(R.id.canceldialog);
b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Toast.makeText(d.getContext(), "cliccatocancel", Toast.LENGTH_LONG).show();
d.cancel();
}
});
问题是当我单击“确定”按钮时,会出现以下错误:尝试在空对象引用上调用虚拟方法“android.text.Editable android.widget.EditText.getText()”。我认为问题出在这行stringp1value=p1.getText().toString()代码>我不知道如何解决。有人能帮我吗
添加输出错误:
11-06 13:03:24.747 16356-16356/com.apps.giak.lavorotime E/AndroidRuntime: FATAL EXCEPTION: main
11-06 13:03:24.747 16356-16356/com.apps.giak.lavorotime E/AndroidRuntime: Process: com.apps.giak.lavorotime, PID: 16356
11-06 13:03:24.747 16356-16356/com.apps.giak.lavorotime E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
11-06 13:03:24.747 16356-16356/com.apps.giak.lavorotime E/AndroidRuntime: at com.apps.giak.lavorotime.AgendaActivity$1.onClick(AgendaActivity.java:62)
11-06 13:03:24.747 16356-16356/com.apps.giak.lavorotime E/AndroidRuntime: at android.view.View.performClick(View.java:5155)
11-06 13:03:24.747 16356-16356/com.apps.giak.lavorotime E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:20747)
11-06 13:03:24.747 16356-16356/com.apps.giak.lavorotime E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
11-06 13:03:24.747 16356-16356/com.apps.giak.lavorotime E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
11-06 13:03:24.747 16356-16356/com.apps.giak.lavorotime E/AndroidRuntime: at android.os.Looper.loop(Looper.java:145)
11-06 13:03:24.747 16356-16356/com.apps.giak.lavorotime E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5832)
11-06 13:03:24.747 16356-16356/com.apps.giak.lavorotime E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-06 13:03:24.747 16356-16356/com.apps.giak.lavorotime E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
11-06 13:03:24.747 16356-16356/com.apps.giak.lavorotime E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteIn
XML
`
`您甚至不用看源代码,就可以通过错误消息和您所说的行来判断问题所在。p1为空;您尝试在其上运行getText()
。Null没有方法getText()
,因此您会得到一个Null指针异常
findViewById(R.id.Ora___;io)
很可能返回null,这意味着Android在当前布局中很难通过该id找到视图。仔细检查此ID是否用于当前布局中的视图。请使用
EditText p1=(EditText)d.findViewById(R.id.Ora__;io)代码>
而不是
EditText p1=(EditText)findViewById(R.id.Ora__;io)代码>
因为id为R.id.Ora___;io
的视图位于扩展到对话框的布局中,即R.layout.popupdialog
因此,您必须使用Dialog.findViewById()
方法
但是现在您正试图从活动布局(R.layout.agendalayout)访问此Edittext
而不是对话框布局。您的R.id.Ora__inazio元素在哪里?在此发布您的xml文件可能的副本我已经添加了一个android监视器的输出并添加了XMLoh感谢现在单击“确定”按钮不要崩溃,但日志中没有显示任何内容action@giacomolorenzo您尚未在按钮单击中记录任何内容。单击按钮时的预期结果是什么?是的,我忘记添加另一个代码,但现在它的工作和感谢帮助:)
`<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"></LinearLayout>
<EditText
android:id="@+id/Ora_inizio"
android:inputType="numberDecimal"
android:digits="0123456789./"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:hint="Ore" />
<EditText
android:id="@+id/Ora_fine"
android:inputType="date"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="16dp"
android:fontFamily="sans-serif"
android:hint="data"/>
<EditText
android:id="@+id/pause"
android:inputType="numberDecimal"
android:digits="0123456789./"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="16dp"
android:fontFamily="sans-serif"
android:hint=""/>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/ok_action"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="Ok"/>
<Button
android:id="@+id/canceldialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Cancel"/>
</TableRow>