2012-04-23 60 views
-3

我正在构建一个Android应用程序,但日食是驾驶我疯狂的小括号错误,而不是告诉我在哪里检查它应该是。任何人都可以告诉我括号内的括号吗?我将非常感谢帮助,我想在5月之前完成这个小项目......但5月20日是绝对最新的。请指出如何完成括号

public class MainActivity extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 


final Button button = (Button) findViewById(R.id.toggleButton); 
button.setOnClickListener(new View.OnClickListener() { 
    public void onClick;{; 
      newPhoneImage = 
      getResources().getDrawable(R.drawable.i_dens); 
    } 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 

      }; 
     ) 
     }; 
    }; 

};

+0

我建议你缩短你的代码更一致。它会为你节省很多头痛。另外,'public void onClick; {;'? – climbage 2012-04-23 05:37:50

+0

为什么[javascript]标签? – 2012-04-23 05:39:01

+0

缩进一致或不缩进。还有一些你的代码是挂在引用的代码块 – Hawken 2012-04-23 05:41:28

回答

1
public void onClick;{; 

究竟是那些;字符意味着在做什么?你的第一步步骤是摆脱那些,然后再试一次。

0

公共类MainActivity extends活动{

/**首次创建活动时调用。 */

@Override

公共无效的onCreate(捆绑savedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.main); 

final Button button =(Button)findViewById(R.id.toggleButton);

button.setOnClickListener(新OnClickListener(){

@Override 
public void onClick(View arg0) { 
    // TODO Auto-generated method stub 
    newPhoneImage = 
     getResources().getDrawable(R.drawable.i_dens); 
} 

});

}

}

+0

破坏格式... – sarnold 2012-04-23 05:45:16

0

需要知道去哪里收盘括号去;放置是不是任意。如果很容易添加它们,我们可以放弃它们并完成它。但因为他们重要,必须手动添加它们。考虑下面的一段代码:

if (current_time() - time_since_last_contact() > 20) 
alert_moscow_to_launch(); 
open_silo_hatches(); 
sleep(20); 
response = check_for_message_from_moscow(); 
if (response == LAUNCH) 
launch_missiles(); 
else if (response == PEACE) 
close_silo_hatches(); 
else /* FALSE RESPONSE */ 
launch_missiles(); 

这是一个没有任何括号的没有意义的混乱。让我们添加一些:

if (current_time() - time_since_last_contact() > 20) { 
    alert_moscow_to_launch(); 
} 
open_silo_hatches(); 
sleep(20); 
response = check_for_message_from_moscow(); 
if (response == LAUNCH) { 
    launch_missiles(); 
} else if (response == PEACE) { 
    close_silo_hatches(); 
} else /* FALSE RESPONSE */ { 
    launch_missiles(); 
} 

这让一些感,但现在是不怎么样 - 它打开粮仓,无论它是如何长时间以来一直存在与领导接触。

if (current_time() - time_since_last_contact() > 20) { 
    alert_moscow_to_launch(); 
    open_silo_hatches(); 
    sleep(20); 
    response = check_for_message_from_moscow(); 
    if (response == LAUNCH) { 
     launch_missiles(); 
    } else if (response == PEACE) { 
     close_silo_hatches(); 
    } else { /* no response or false response */ 
     launch_missiles(); 
    } 
} 

这个版本明确只执行the final steps如果与领导的沟通是完全切断。

程序员必须知道方括号的位置 - 如果您遇到问题,可能会在您的算法文件中构建一个flow chart。这是一个有点老派,但了解方括号去的好开始。它不能很好地转化为回调的世界,但是如果您将大括号之间的块视为嵌套层次结构,它可以帮助您查看括号需要去的位置。

0

我想我固定在这里:

public class MainActivity extends Activity { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    final Button button = (Button) findViewById(R.id.toggleButton); 
    button.setOnClickListener(new View.OnClickListener(){ 
     public void onClick(){ 
      newPhoneImage=getResources().getDrawable(R.drawable.i_dens); 
     } 
     public void onClick(View arg0){} 
    }); 
} 

的问题是在:

public void onClick;{; 

这应该是一个方法,以便:

public void onClick(){ 

我刚刚重建的休息,其中一部分是过度使用

}; 

尽我所见,它绝不应该在程序中。