尝试开发类似拨号器的活动。这是我到目前为止下面的代码......
public class Dial extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dial);
Button pb1 = (Button) findViewById(R.id.pb1);
Button pb2 = (Button) findViewById(R.id.pb2);
Button pb3 = (Button) findViewById(R.id.pb3);
Button pb4 = (Button) findViewById(R.id.pb4);
Button pb5 = (Button) findViewById(R.id.pb5);
Button pb6 = (Button) findViewById(R.id.pb6);
Button pb7 = (Button) findViewById(R.id.pb7);
Button pb8 = (Button) findViewById(R.id.pb8);
Button pb9 = (Button) findViewById(R.id.pb9);
Button pb0 = (Button) findViewById(R.id.pb0);
Button pbstar = (Button) findViewById(R.id.pbstar);
Button pbhash = (Button) findViewById(R.id.pbhash);
Button pbcall = (Button) findViewById(R.id.pbcall);
pb1.setOnClickListener(this);
pb2.setOnClickListener(this);
pb3.setOnClickListener(this);
pb4.setOnClickListener(this);
pb5.setOnClickListener(this);
pb6.setOnClickListener(this);
pb7.setOnClickListener(this);
pb8.setOnClickListener(this);
pb9.setOnClickListener(this);
pb0.setOnClickListener(this);
pbstar.setOnClickListener(this);
pbhash.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
TextView text=(TextView)findViewById(R.id.text);
int id = (v.getId());
if (id == R.id.pb1){
text.append("1");
}
if (id == R.id.pb2){
text.append("2");
}
if (id == R.id.pb3){
text.append("3");
}
if (id == R.id.pb4){
text.append("4");
}
if (id == R.id.pb5){
text.append("5");
}
if (id == R.id.pb6){
text.append("6");
}
if (id == R.id.pb7){
text.append("7");
}
if (id == R.id.pb8){
text.append("8");
}
if (id == R.id.pb9){
text.append("9");
}
if (id == R.id.pb0){
text.append("0");
}
}
}
使用此代码,应用程序强制关闭。但是,如果我删除这部分:
Button pb1 = (Button) findViewById(R.id.pb1);
Button pb2 = (Button) findViewById(R.id.pb2);
Button pb3 = (Button) findViewById(R.id.pb3);
Button pb4 = (Button) findViewById(R.id.pb4);
Button pb5 = (Button) findViewById(R.id.pb5);
Button pb6 = (Button) findViewById(R.id.pb6);
Button pb7 = (Button) findViewById(R.id.pb7);
Button pb8 = (Button) findViewById(R.id.pb8);
Button pb9 = (Button) findViewById(R.id.pb9);
Button pb0 = (Button) findViewById(R.id.pb0);
Button pbstar = (Button) findViewById(R.id.pbstar);
Button pbhash = (Button) findViewById(R.id.pbhash);
Button pbcall = (Button) findViewById(R.id.pbcall);
pb1.setOnClickListener(this);
pb2.setOnClickListener(this);
pb3.setOnClickListener(this);
pb4.setOnClickListener(this);
pb5.setOnClickListener(this);
pb6.setOnClickListener(this);
pb7.setOnClickListener(this);
pb8.setOnClickListener(this);
pb9.setOnClickListener(this);
pb0.setOnClickListener(this);
pbstar.setOnClickListener(this);
pbhash.setOnClickListener(this);
该应用程序启动正常但什么也不做(因为没有 onClickListener。)这些声明行中是否存在错误?我已经尝试更改它们但仍然没有...我想要的基本上是当您按下按钮时,它会显示(在文本视图中)分配给它的数字...我能做些什么来解决这个问题?如果这是一个初学者问题,我很抱歉,但我现在开始了,我真的很想解决这个问题。
日志猫:
11-06 14:42:04.358: I/dalvikvm-heap(1658): Grow heap (frag case) to 52.477MB for 10074256-byte allocation
11-06 14:42:04.518: I/dalvikvm-heap(1658): Grow heap (frag case) to 62.144MB for 10126096-byte allocation
11-06 14:42:04.668: I/dalvikvm-heap(1658): Grow heap (frag case) to 71.617MB for 9970576-byte allocation
11-06 14:42:10.144: W/dalvikvm(1658): threadid=1: thread exiting with uncaught exception (group=0x417e5898)
11-06 14:42:10.154: E/AndroidRuntime(1658): FATAL EXCEPTION: main
11-06 14:42:10.154: E/AndroidRuntime(1658): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.devon.carmate/com.devon.carmate.Dial}: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
11-06 14:42:10.154: E/AndroidRuntime(1658): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
11-06 14:42:10.154: E/AndroidRuntime(1658): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
11-06 14:42:10.154: E/AndroidRuntime(1658): at android.app.ActivityThread.access$600(ActivityThread.java)
11-06 14:42:10.154: E/AndroidRuntime(1658): at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
11-06 14:42:10.154: E/AndroidRuntime(1658): at android.os.Handler.dispatchMessage(Handler.java)
11-06 14:42:10.154: E/AndroidRuntime(1658): at android.os.Looper.loop(Looper.java)
11-06 14:42:10.154: E/AndroidRuntime(1658): at android.app.ActivityThread.main(ActivityThread.java)
11-06 14:42:10.154: E/AndroidRuntime(1658): at java.lang.reflect.Method.invokeNative(Native Method)
11-06 14:42:10.154: E/AndroidRuntime(1658): at java.lang.reflect.Method.invoke(Method.java:525)
11-06 14:42:10.154: E/AndroidRuntime(1658): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
11-06 14:42:10.154: E/AndroidRuntime(1658): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
11-06 14:42:10.154: E/AndroidRuntime(1658): at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:115)
11-06 14:42:10.154: E/AndroidRuntime(1658): at dalvik.system.NativeStart.main(Native Method)
11-06 14:42:10.154: E/AndroidRuntime(1658): Caused by: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
11-06 14:42:10.154: E/AndroidRuntime(1658): at com.devon.carmate.Dial.onCreate(Dial.java:23)
11-06 14:42:10.154: E/AndroidRuntime(1658): at android.app.Activity.performCreate(Activity.java)
11-06 14:42:10.154: E/AndroidRuntime(1658): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
11-06 14:42:10.154: E/AndroidRuntime(1658): ... 13 more