这里是 Android/Java 的新手,但在 C/C++ 和 Object Pascal 方面很有基础。我有 5 个 ImageButtons,只启用了一个。然后在某个时间点按下单独的启用按钮时,其他四个也应该通过调用带有布尔值( enableBtn2..3.. 等)传递的 setEnabled() 来启用。但是,每当我按下单独启用的按钮来调用成员 newRung() 进而调用另一个成员 refreshMnuButtons() 时,就会引发异常,我怀疑该异常是由对 setEnabled() 父类成员的调用引起的。有人可以请教吗?指出漏洞?我疯狂地寻找解释,但似乎找不到合适的答案。请看我附上的代码:
public class MyActivity extends Activity {
private ImageView cell1,cell2,cell3,cell4,cell5,cell6,rungClose;
private ImageButton mnuBtn1,mnuBtn2,mnuBtn3,mnuBtn4,mnuBtn5;
private boolean enableBtn1 = true,
enableBtn2 = false,
enableBtn3 = false,
enableBtn4 = false,
enableBtn5 = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pic_ladder_main);
// setup toolbar
TableLayout tbl = (TableLayout)findViewById(R.id.tblToolbar);
ImageButton mnuBtn1 = new ImageButton(this);
mnuBtn1.setImageResource(R.drawable.rungopentool);
mnuBtn1.setEnabled(enableBtn1);
//setup mnuBtn1 listener
mnuBtn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
newRung();
}
});
ImageButton mnuBtn2 = new ImageButton(this);
mnuBtn2.setImageResource(R.drawable.noc);
mnuBtn2.setEnabled(enableBtn2);
//setup mnuBtn2 listener
mnuBtn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addNOC();
}
});
final ImageButton mnuBtn3 = new ImageButton(this);
mnuBtn3.setImageResource(R.drawable.ncc);
mnuBtn3.setEnabled(enableBtn3);
//setup mnuBtn3 listener
mnuBtn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addNCC();
}
});
ImageButton mnuBtn4 = new ImageButton(this);
mnuBtn4.setImageResource(R.drawable.process);
mnuBtn4.setEnabled(enableBtn4);
//setup mnuBtn4 listener
mnuBtn4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addProcess();
}
});
ImageButton mnuBtn5 = new ImageButton(this);
mnuBtn5.setImageResource(R.drawable.coil);
mnuBtn5.setEnabled(enableBtn5);
//setup mnuBtn5 listener
mnuBtn5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
addCoil();
}
});
TableRow mnurow = new TableRow(this);
mnurow.setBackgroundColor(0x3F000000);
// display menu
mnurow.addView(mnuBtn1);
mnurow.addView(mnuBtn2);
mnurow.addView(mnuBtn3);
mnurow.addView(mnuBtn4);
mnurow.addView(mnuBtn5);
tbl.addView(mnurow);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_pic_ladder_main, menu);
return true;
}
private void createRungImages() {
// TODO
}
private void newRung()
{
Toast.makeText(this, "New Rung" ,Toast.LENGTH_SHORT).show();
enableBtn2 = true;
enableBtn3 = true;
enableBtn4 = true;
enableBtn5 = true;
enableBtn1 = false;
refreshMnuButtons();
}
private void addNOC()
{
Toast.makeText(this, "NOC" ,Toast.LENGTH_SHORT).show();
}
private void addNCC()
{
Toast.makeText(this, "NCC" ,Toast.LENGTH_SHORT).show();
}
private void addProcess()
{
Toast.makeText(this, "Add Process" ,Toast.LENGTH_SHORT).show();
}
private void addCoil()
{
Toast.makeText(this, "Coil" ,Toast.LENGTH_SHORT).show();
}
public void refreshMnuButtons()
{
try {
mnuBtn1.setEnabled(enableBtn1);
mnuBtn2.setEnabled(enableBtn2);
mnuBtn3.setEnabled(enableBtn3);
mnuBtn4.setEnabled(enableBtn4);
mnuBtn5.setEnabled(enableBtn5);
}
catch(Exception e) {Toast.makeText(this, "Exception" ,Toast.LENGTH_SHORT).show(); }
}
}
这是我的 xml:请注意,我正在尝试通过代码生成大部分图形元素。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="false"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="44dp" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TableLayout
android:id="@+id/ladderSheet"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableLayout>
</ScrollView>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<HorizontalScrollView
android:id="@+id/horizontalScrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableLayout
android:id="@+id/tblToolbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbarStyle="outsideInset"
android:scrollbars="horizontal" >
</TableLayout>
</HorizontalScrollView>
</RelativeLayout>
</RelativeLayout>
提前致谢!