以下是我所做的一个小应用程序的一些主要代码,但似乎有一个运行时问题,有人可以看看,并告诉我到底是什么问题:
// there are two button only one for exit and the other to calculate**
public class TimerCodeActivity extends Activity {
protected Object timebox;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.timerlayout);
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
finish();
System.exit(0);}
});
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener(){
int digits = Integer.parseInt(((String) timebox).substring(0,2));
int digits1 = Integer.parseInt(((String) timebox).substring(2,4));
int digits2 = Integer.parseInt(((String) timebox).substring(4,6));
int newnumber = ((digits + 777) + (digits1 * 16) + (digits2 * 3)) ;
@Override
public void onClick(View v) {
Toast.makeText(TimerCodeActivity.this, String.valueOf(newnumber), Toast.LENGTH_LONG).show();
}
});
}
}
错误日志cat如下:
D/AndroidRuntime(539): Shutting down VM
W/dalvikvm(539): threadid=1: thread exiting with uncaught exception (group=0x409951f8)
E/AndroidRuntime(539): FATAL EXCEPTION: main
E/AndroidRuntime(539): java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.Timer.Code/com.Timer.Code.TimerCodeActivity}:
java.lang.NullPointerException
E/AndroidRuntime(539): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
E/AndroidRuntime(539): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
E/AndroidRuntime(539): at
android.app.ActivityThread.access$600(ActivityThread.java:122)
E/AndroidRuntime(539): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
E/AndroidRuntime(539): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(539): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(539): at
android.app.ActivityThread.main(ActivityThread.java:4340)
E/AndroidRuntime(539): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(539): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(539): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
E/AndroidRuntime(539): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
E/AndroidRuntime(539): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(539): Caused by: java.lang.NullPointerException
E/AndroidRuntime(539): at com.Timer.Code.TimerCodeActivity$2.<init>
(TimerCodeActivity.java:32)
/AndroidRuntime(539): at
com.Timer.Code.TimerCodeActivity.onCreate(TimerCodeActivity.java:29)
E/AndroidRuntime(539): at android.app.Activity.performCreate(Activity.java:4465)
E/AndroidRuntime(539): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
E/AndroidRuntime(539): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
E/AndroidRuntime(539): ... 11 more
请告诉我问题...谢谢
答案 0 :(得分:1)
我猜它是NullPointerException
?
您正在使用时间框的值初始化digits
。但我没有看到时间框被初始化。我也想知道为什么时间框是Object
,因为你将它作为String
来处理。
注意:当您拨打setOnClickListener
而不是OnClick
方法时,数字也会被初始化。
编辑(评论后):
这应该可以解决问题。
public class TimerCodeActivity extends Activity {
protected TextView timebox;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.timerlayout);
timebox = (TextView) findViewById(R.id.name_of_textview);
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
finish();
System.exit(0);}
});
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
String timeboxRepresentation = timebox.getText();
int firstDigit = Integer.parseInt(timeboxRepresentation.substring(0,2));
int secondDigit = Integer.parseInt(timeboxRepresentation.substring(2,4));
int thirdDigit = Integer.parseInt(timeboxRepresentation.substring(4,6));
int sum = ((firstDigit + 777) + (secondDigit * 16) + (thirdDigit * 3)) ;
Toast.makeText(TimerCodeActivity.this, String.valueOf(sum), Toast.LENGTH_LONG).show();
}
});
}
}