删除计算器中的按钮

时间:2014-10-03 10:58:31

标签: android calculator

我正在创建一个计算器,我创建了一个删除按钮但是当我运行应用程序时,它会崩溃并停止。 这是我的java代码:

请帮帮我。

       private static String removeLastChar(String str) {
       return str.substring(0,str.length()-1);

和这个

        bdelete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            editText.setText(removeLastChar(editText.getText().toString()));
        }
       });

这是我的日志

java.lang.RuntimeException: Unable to start activity   ComponentInfo{player.android.com.layout/player.android.com.layout.MyActivity}:  java.lang.NullPointerException
         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
         at android.app.ActivityThread.access$600(ActivityThread.java:141)
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
         at android.os.Handler.dispatchMessage(Handler.java:99)
         at android.os.Looper.loop(Looper.java:137)
         at android.app.ActivityThread.main(ActivityThread.java:5103)
         at java.lang.reflect.Method.invokeNative(Native Method)
         at java.lang.reflect.Method.invoke(Method.java:525)
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
         at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
         at player.android.com.layout.MyActivity.onCreate(MyActivity.java:68)
         at android.app.Activity.performCreate(Activity.java:5133)
         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
         at android.app.ActivityThread.access$600(ActivityThread.java:141)
         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
         at android.os.Handler.dispatchMessage(Handler.java:99)
         at android.os.Looper.loop(Looper.java:137)
         at android.app.ActivityThread.main(ActivityThread.java:5103)
         at java.lang.reflect.Method.invokeNative(Native Method)
         at java.lang.reflect.Method.invoke(Method.java:525)
         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
         at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

使用此代码,您将删除所有字符,因为您说从0移除到长度为1的字符。

str.substring(0,str.length()-1);

例如:

String ll =" Lorem Ipsum只是"

如果你说String ii = ll.substring(0,2);

您将获得:ii == "Lor"

尝试这样的事情:

   public String remove(){
        String character = "2313213";
        String character1 = "";
        for(int i=0;i<character.length()-1;i++){
            character1 += character.charAt(i);
        }

        return character1;
    }

答案 1 :(得分:0)

componentWillMount(){

TodoStore.on("change", () => {
    this.setState({
        todos: TodoStore.getall()
    })
})
}