我一直试图让我的按钮工作很长一段时间,有趣的是他们我已经完全按照其他按钮完全相同的方式实现了它们的工作正常,按钮不均匀抛出错误或异常,我已经厌倦了......我已经尝试了几乎我能想到的一切,但它仍然无法正常工作。
按钮不起作用的活动的代码。
@Override
protected void onCreate(Bundle savedInstanceState) {
Button easy, medium, hard;
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.exam_level_options);
easy = (Button) findViewById(R.id.easyExamButn);
medium = (Button) findViewById(R.id.mediumExamButn);
hard = (Button) findViewById(R.id.hardExamButn);
easy.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
intent = new Intent (ExamOptionPage.this, EasyExam.class);
startActivity(intent);
}
});
medium.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
intent = new Intent (ExamOptionPage.this, MediumExam.class);
startActivity(intent);
}
});
hard.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
intent = new Intent (ExamOptionPage.this, HardExam.class);
startActivity(intent);
}
});
}
/*@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.easyExamButn:
intent = new Intent(this, EasyExam.class);
this.startActivity(intent);
overridePendingTransition(R.anim.fade, 0);
break;
case R.id.mediumExamButn:
intent = new Intent(this, MediumExam.class);
this.startActivity(intent);
overridePendingTransition(R.anim.fade, 0);
break;
case R.id.hardExamButn:
intent = new Intent(this, HardExam.class);
this.startActivity(intent);
overridePendingTransition(R.anim.fade, 0);
break;
default:
break;
}
}*/
我已经阻止注释掉了switch语句,但是尝试了另一种方法仍然没有运气,但是switch语句适用于其他活动。做同样的事情。
日志:
01-08 23:42:39.436: INFO/ActivityManager(58): Starting activity: Intent { act=com.gc02.android.activities.OPTIONACTIVITY cmp=com.gc02.android/.activities.OptionActivity }
01-08 23:42:39.464: INFO/WindowManager(58): Setting rotation to 0, animFlags=0
01-08 23:42:39.514: INFO/ActivityManager(58): Config changed: { scale=1.0 imsi=310/260 loc=en_US touch=3 keys=2/1/2 nav=3/1 orien=1 layout=34}
01-08 23:42:40.035: INFO/ActivityManager(58): Displayed activity com.gc02.android/.activities.OptionActivity: 576 ms (total 576 ms)
01-08 23:42:41.234: INFO/ActivityManager(58): Starting activity: Intent { cmp=com.gc02.android/.activities.ExamActivity }
01-08 23:42:41.614: INFO/ActivityManager(58): Displayed activity com.gc02.android/.activities.ExamActivity: 358 ms (total 358 ms)
01-08 23:42:42.144: DEBUG/dalvikvm(58): GC freed 1726 objects / 84336 bytes in 104ms
01-08 23:42:43.474: WARN/KeyCharacterMap(1638): No keyboard for id 0
01-08 23:42:43.484: WARN/KeyCharacterMap(1638): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
01-08 23:42:46.694: DEBUG/dalvikvm(101): GC freed 551 objects / 26640 bytes in 75ms
01-08 23:42:47.584: DEBUG/dalvikvm(96): GC freed 411 objects / 22256 bytes in 116ms
01-08 23:42:48.054: DEBUG/dalvikvm(96): GC freed 719 objects / 53216 bytes in 166ms
01-08 23:42:51.114: DEBUG/dalvikvm(96): GC freed 693 objects / 42344 bytes in 70ms
01-08 23:42:59.134: INFO/NotificationService(58): enqueueToast pkg=com.gc02.android callback=android.app.ITransientNotification$Stub$Proxy@44c476a8 duration=0
01-08 23:43:01.688: DEBUG/dalvikvm(99): GC freed 12471 objects / 573368 bytes in 1422ms
01-08 23:43:03.124: WARN/InputManagerService(58): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@44c870a0
似乎按钮没有被调用,当我点击它们时,我该怎么办?
谢谢大家!
答案 0 :(得分:0)
我没有在任何地方看到你的“意图”对象。事实上,这应该给出编译错误。
如果您错过了这里的行,那么请确保您已将所有活动都包含在AndroidManifest.xml中。
答案 1 :(得分:0)
请在对象行的pass id之后添加以下行,以便在java文件中调用onClick方法: -
easy.setOnClickListener(this);
medium.setOnClickListener(this);
hard.setOnClickListener(this);
@Override
public void onClick(View v) {
if(v==easy){
intent = new Intent (ExamOptionPage.this, EasyExam.class);
startActivity(intent);
} else if(v==medium){
intent = new Intent (ExamOptionPage.this, MediumExam.class);
startActivity(intent);
} else if(v==hard){
intent = new Intent (ExamOptionPage.this, HardExam.class);
startActivity(intent);
}
}