0

我正在编写我的第一个应用程序,我是编程新手......这个程序正在进行中,但我想看看它到目前为止的样子,如果我能编译它!我认为括号有问题,每次我修复一个错误时,另一个错误就会出现在其他地方......有没有一种简单的方法来发现丢失的括号?谢谢!

package com.Jenko.votingapp;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.content.Intent;

public class MainActivity extends Activity {

    @Override public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button createNew = (Button)findViewById(R.id.newPoll);
        createNew.setOnClickListener(new View.OnClickListener() {
            public void onClick (View view) {
                startActivityForResult(new Intent("com.Jenko.votingapp.CREATENEWPOLL"), 1);
                finish();
            }

            public void onActivityResult(int requestCode, int resultCode, Intent data) {
              if (requestCode ==1) {
                  if (resultCode == RESULT_OK) {
                      //something will eventually happen here...
                  }

                  Button seePrevious = (Button)findViewById(R.id.prevPoll);
                  seePrevious.setOnClickListener(new View.OnClickListener() {
                      public void onClick (View view) {
                          startActivityForResult(new Intent("com.Jenko.votingapp.SEEPREVIOUSPOLL"), 2);
                          finish();
                      }
                  });
              }
4

4 回答 4

2

正确缩进非常重要。如果缩进正确,任何缺少的括号都会很容易被注意到。这里是缩进和固定的括号/括号:

package com.Jenko.votingapp;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.content.Intent;

public class MainActivity extends Activity {

    @Override
    public void onCreate( Bundle savedInstanceState ) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );

        Button createNew = (Button)findViewById( R.id.newPoll );
        createNew.setOnClickListener( new View.OnClickListener() {

            public void onClick( View view ) {
                startActivityForResult( new Intent( "com.Jenko.votingapp.CREATENEWPOLL" ), 1 );
                finish();
            }
        });
    }

    public void onActivityResult( int requestCode, int resultCode, Intent data ) {
        if( requestCode == 1 ) {
            if( resultCode == RESULT_OK ) {// something will eventually happen here...}
                Button seePrevious = (Button)findViewById( R.id.prevPoll );
                seePrevious.setOnClickListener( new View.OnClickListener() {

                    public void onClick( View view ) {
                        startActivityForResult( new Intent( "com.Jenko.votingapp.SEEPREVIOUSPOLL" ), 2 );
                        finish();
                    }
                });
            }
        }
    }
}
于 2012-09-06T19:31:30.680 回答
2

如果您没有 IDE,养成的一个好习惯是在您键入开始的那一刻立即添加右括号、大括号或圆括号。两者都有后,您可以填写其余部分。你永远不会那样添加孤儿。

我将支持所有那些说一致的代码样式和布局在这里有帮助的评论。

于 2012-09-06T19:34:00.930 回答
1

使用 IDE(eclipse、IntelliJ 等)。对你来说太重了吗?emacs 或 vim 编辑器怎么样?它们都具有括号匹配功能。编程编辑器是一毛钱在那里。选择一个,学习它,然后继续您定期安排的编程。

于 2012-09-06T19:27:04.403 回答
0

大多数编码标准将左括号直接放在语句或函数之后,并为右括号提供自己的行,就像这样

    function() {
        do stuff here
    }

或者就在语句或函数之后,没有其他类似的东西

    function()
    {
        do stuff here
    }

这使得发现打开和关闭括号更容易。您上面的代码重构以满足这些标准看起来像这样

    public class MainActivity extends Activity { <-- missing closing bracket

        @Override
        public void onCreate(Bundle savedInstanceState) { <-- missing closing bracket
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            Button createNew = (Button)findViewById(R.id.newPoll);
            createNew.setOnClickListener(new View.OnClickListener() { <-- missing closing bracket
                public void onClick (View view) {
                    startActivityForResult(new Intent("com.Jenko.votingapp.CREATENEWPOLL"), 1);
                    finish();
                } //end onClick

                public void onActivityResult(int requestCode, int resultCode, Intent data) { <-- missing closing bracket
                    if (requestCode ==1) {
                        if (resultCode == RESULT_OK) {
                            //something will eventually happen here...
                        }//end if


                        Button seePrevious = (Button)findViewById(R.id.prevPoll);
                        seePrevious.setOnClickListener(new View.OnClickListener() {
                            public void onClick (View view) {
                                startActivityForResult(new Intent("com.Jenko.votingapp.SEEPREVIOUSPOLL"), 2);
                                finish();
                            } //end onClick

                        }); //end View.OnClickListener
                    } //end if

您缺少以下括号:

MainActivity
public void onCreate(Bundle savedInstanceState)
public void onActivityResult
createNew.setOnClickListener

正如已经提到的,正确的代码缩进有很大帮助

于 2012-09-06T19:46:54.337 回答