У меня есть метод переключения onClick
в MainActivity.java, а кнопки создаются программно в Navigation.java. Начальное меню nav.mainMenu()
вызывается правильно, и кнопка создается, как и ожидалось. Моя проблема в том, что когда я нажимаю кнопку, ничего не происходит.
Я не получаю трассировку стека ошибок в logcat. Я пытался помещать журналы в метод onclick, но это не так далеко. Эмулятор вроде работает, не зависает и не зависает.
Я предполагаю, что когда я отправляю код в меню, активность находится в этом классе? поэтому, когда я пытаюсь использовать onClickListener
в MainActivity
, он не знает об этом?
Я также не уверен, чего мне ожидать от v.getId()
в MainActivity.java R.id.btnGame
?
MainActivity.java
public class MainActivity extends Activity implements View.OnClickListener {
public Navigation nav = new Navigation(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.empty);
nav.mainMenu();
}
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.btnGame:
nav.game();
break;
}
}
}
Навигация.java
public class Navigation {
Button btnGame;
Context mContext;
Navigation(Context mContext) {
this.mContext = mContext;
}
public void mainMenu() {
LinearLayout ll = new LinearLayout(mContext);
ll.removeAllViews();
LinearLayout.LayoutParams llP = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
ll.setLayoutParams(llP);
ll.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams btnParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
btnGame = new Button(mContext.getApplicationContext());
btnGame.setLayoutParams(btnParams);
btnGame.setText("Play Game");
ll.addView(btnGame);
Activity activity = (Activity) mContext;
activity.setContentView(ll);
}
public void game() {
Toast.makeText(mContext, "I dont see this", Toast.LENGTH_SHORT).show();
}
}
Я попытался добавить
.setOnClickListener()
к кнопкам в Navigation.java, но я не могу правильно понять контекст, я не знаю, чего не хватает.Расширение Navigation.java до MainActivity или Activity дает мне цикл ошибок.
Logcat после того, как приложение было встроено в эмулятор:
09-30 17:43:59.655 7038-7038/? I/art: Not late-enabling -Xcheck:jni (already on)
09-30 17:43:59.655 7038-7038/? W/art: Unexpected CPU variant for X86 using defaults: x86
09-30 17:43:59.854 7038-7038/com.fomtirth.barcodebattle W/System: ClassLoader referenced unknown path: /data/app/com.fomtirth.barcodebattle-2/lib/x86
09-30 17:43:59.867 7038-7038/com.fomtirth.barcodebattle I/InstantRun: starting instant run server: is main process
09-30 17:44:00.230 7038-7066/com.fomtirth.barcodebattle I/OpenGLRenderer: Initialized EGL, version 1.4
09-30 17:44:00.230 7038-7066/com.fomtirth.barcodebattle D/OpenGLRenderer: Swap behavior 1
09-30 17:44:00.230 7038-7066/com.fomtirth.barcodebattle W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
09-30 17:44:00.230 7038-7066/com.fomtirth.barcodebattle D/OpenGLRenderer: Swap behavior 0
09-30 17:44:00.234 7038-7066/com.fomtirth.barcodebattle D/EGL_emulation: eglCreateContext: 0x9baa38c0: maj 2 min 0 rcv 2
09-30 17:44:00.241 7038-7066/com.fomtirth.barcodebattle D/EGL_emulation: eglMakeCurrent: 0x9baa38c0: ver 2 0 (tinfo 0x99a887d0)
09-30 17:44:00.293 7038-7066/com.fomtirth.barcodebattle D/EGL_emulation: eglMakeCurrent: 0x9baa38c0: ver 2 0 (tinfo 0x99a887d0)
09-30 17:44:00.400 7038-7066/com.fomtirth.barcodebattle D/EGL_emulation: eglMakeCurrent: 0x9baa38c0: ver 2 0 (tinfo 0x99a887d0)
09-30 17:44:00.460 7038-7066/com.fomtirth.barcodebattle D/EGL_emulation: eglMakeCurrent: 0x9baa38c0: ver 2 0 (tinfo 0x99a887d0)
verbose
. - person TomFirth   schedule 30.09.2017