我正在编写我的第一个应用程序,我是编程新手......这个程序正在进行中,但我想看看到目前为止看起来如何,如果我只能编译它!我认为括号有问题,每当我修正一个错误,另一个出现在其他地方......是否有一种简单的方法来发现缺少括号?谢谢!
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();
}
});
}
答案 0 :(得分:2)
正确缩进非常重要。如果您的缩进是正确的,任何缺失的括号都会很容易被注意到。这里是缩进和固定括号/ parens:
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();
}
});
}
}
}
}
答案 1 :(得分:2)
如果您没有IDE,那么进入的一个好习惯是在您键入开始时立即添加右括号,括号或括号。一旦你有两个,你可以填写其余的。你永远不会以这种方式添加一个孤儿。
我会在所有评论中说出一致的样式和代码布局对此有帮助。
答案 2 :(得分:1)
使用IDE(eclipse,IntelliJ等)。你的体重太重了吗? emacs或vim编辑器怎么样?它们都具有支架匹配功能。编程编辑在那里是十几个。选择一个,学习它,继续你定期安排的节目。
答案 3 :(得分: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
并且如前所述,正确的代码缩进有助于实现