我正在尝试从两个 EditText 字段中获取文本,然后将其转换为整数值并执行更多基于此的代码。这是我的代码与我的 onCreate 方法的距离
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button go = (Button)findViewById(R.id.button1);
go.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
EditText timer = (EditText)findByViewId(R.id.editText2);
EditText beeps = (EditText)findByViewId(R.id.editText1);
try{
int length = Integer.valueOf(timer.getText().toString());
int beepRepeat = Integer.valueOf(beeps.getText().toString());
Log.d("onClick","starting alarm");
initializeAlarm(length, beepRepeat);//(length, beepRepeat);
}
catch (Exception numberFormatException){
Log.d("Catch","Caught number format exception");
//Log.d("Catch",timer.getText().toString());
//Log.d("Catch",beeps.getText().toString());
initializeAlarm(2,3);
};
}
});
}
但是,我在 Log.d("Catch",timer.getText().toString()); 处抛出了一个错误。为什么我会在那里出错!我所做的就是将文本打印为字符串。这是错误 java.lang.nullPointerException 和线程以未捕获的异常退出 (group=0x40015560)
有谁知道为什么我的 getText() 会抛出 nullPointerException?
编辑:这是我的 activity_main.xml
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_below="@+id/textView2"
android:ems="10"
android:inputType="number" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="46dp"
android:ems="10"
android:inputType="number|numberDecimal" >
</EditText>
编辑:这是来自 numberFormatException 错误的信息
01-21 00:08:53.789: W/System.err(325): java.lang.NullPointerException
01-21 00:08:53.800: W/System.err(325): at com.example.americancheesecakesupreme5000.MainActivity$1.onClick(MainActivity.java:31)
01-21 00:08:53.800: W/System.err(325): at android.view.View.performClick(View.java:2485)
01-21 00:08:53.811: W/System.err(325): at android.view.View$PerformClick.run(View.java:9080)
01-21 00:08:53.811: W/System.err(325): at android.os.Handler.handleCallback(Handler.java:587)
01-21 00:08:53.820: W/System.err(325): at android.os.Handler.dispatchMessage(Handler.java:92)
01-21 00:08:53.820: W/System.err(325): at android.os.Looper.loop(Looper.java:123)
01-21 00:08:53.830: W/System.err(325): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-21 00:08:53.830: W/System.err(325): at java.lang.reflect.Method.invokeNative(Native Method)
01-21 00:08:53.830: W/System.err(325): at java.lang.reflect.Method.invoke(Method.java:507)
01-21 00:08:53.840: W/System.err(325): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-21 00:08:53.840: W/System.err(325): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-21 00:08:53.840: W/System.err(325): at dalvik.system.NativeStart.main(Native Method)
编辑:我的问题是我由 EditText 通过调用 Find ByViewId而不是 Find ViewByID声明的,无论出于何种原因,它显然编译并运行良好......