Android 以编程方式创建按钮 onClick 来自 class
Android programmatically created button onClick from class
我在 MainActivity.java 中有一个 onClick
切换方法,按钮是在 Navigation.java[= 中以编程方式创建的51=]。正确调用初始菜单 nav.mainMenu()
并按预期创建按钮。我的问题是,当我点击按钮时,没有任何反应。
我在 logcat 中没有得到任何错误堆栈跟踪。我试过将日志放在 onclick 方法中,但它并没有走那么远。模拟器似乎可以工作,不会死机或崩溃。
我假设当我将代码发送到菜单时,activity 就在 class 中?所以当我尝试在 MainActivity
中使用 onClickListener
时,它并不知道它?
我也不确定 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;
}
}
}
Navigation.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)
您不能从字段中分配上下文。您可能 "can't assign a right",因为您实际上遇到了空指针异常并编辑了错误的位置。也就是说,不需要 getApplicationContext()
。
此外,我是否可以建议您 return 内容布局而不是将上下文转换为 Activity?
public Navigation nav;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
nav = new Navigation(this);
setContentView(nav.mainMenu());
}
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.btnGame:
nav.game();
break;
}
}
乍一看,我发现您从未在按钮上设置点击侦听器,所以您可能应该这样做。
例如
Context mContext;
View.OnClickListener mClickListener;
Navigation(Context context) {
this.mContext = context;
if (context instanceof View.OnClickListener) {
this.mClickListener = (View.OnClickListener) context ;
}
}
现在使用这个
if (mClickListener!=null) {
button.setOnClickListener(mClickListener);
}
I'm also not sure what I should be expecting from v.getId() in MainActivity.java R.id.btnGame?
ID 与变量名称不匹配。如果不明确给按钮一个 ID,它将是 -1
我在 MainActivity.java 中有一个 onClick
切换方法,按钮是在 Navigation.java[= 中以编程方式创建的51=]。正确调用初始菜单 nav.mainMenu()
并按预期创建按钮。我的问题是,当我点击按钮时,没有任何反应。
我在 logcat 中没有得到任何错误堆栈跟踪。我试过将日志放在 onclick 方法中,但它并没有走那么远。模拟器似乎可以工作,不会死机或崩溃。
我假设当我将代码发送到菜单时,activity 就在 class 中?所以当我尝试在 MainActivity
中使用 onClickListener
时,它并不知道它?
我也不确定 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;
}
}
}
Navigation.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)
您不能从字段中分配上下文。您可能 "can't assign a right",因为您实际上遇到了空指针异常并编辑了错误的位置。也就是说,不需要 getApplicationContext()
。
此外,我是否可以建议您 return 内容布局而不是将上下文转换为 Activity?
public Navigation nav;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
nav = new Navigation(this);
setContentView(nav.mainMenu());
}
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.btnGame:
nav.game();
break;
}
}
乍一看,我发现您从未在按钮上设置点击侦听器,所以您可能应该这样做。
例如
Context mContext;
View.OnClickListener mClickListener;
Navigation(Context context) {
this.mContext = context;
if (context instanceof View.OnClickListener) {
this.mClickListener = (View.OnClickListener) context ;
}
}
现在使用这个
if (mClickListener!=null) {
button.setOnClickListener(mClickListener);
}
I'm also not sure what I should be expecting from v.getId() in MainActivity.java R.id.btnGame?
ID 与变量名称不匹配。如果不明确给按钮一个 ID,它将是 -1