Android通过编程方式从类中单击按钮

Android通过编程方式从类中单击按钮,android,onclicklistener,programmatically-created,Android,Onclicklistener,Programmatically Created,我在MainActivity.java中有一个onClickswitch方法,按钮是在Navigation.java中以编程方式创建的。正确调用初始菜单nav.mainMenu(),并按预期创建按钮。我的问题是,当我点击按钮时,什么都没有发生 我在logcat中没有得到错误堆栈跟踪。我已经尝试过将日志放入onclick方法中,但没有达到这个目的。模拟器似乎可以工作,不会冻结或崩溃 我假设当我将代码发送到菜单时,活动在该类中?所以当我尝试在main活动中使用onClickListener时,它没有

我在MainActivity.java中有一个
onClick
switch方法,按钮是在Navigation.java中以编程方式创建的。正确调用初始菜单
nav.mainMenu()
,并按预期创建按钮。我的问题是,当我点击按钮时,什么都没有发生

我在logcat中没有得到错误堆栈跟踪。我已经尝试过将日志放入onclick方法中,但没有达到这个目的。模拟器似乎可以工作,不会冻结或崩溃

我假设当我将代码发送到菜单时,活动在该类中?所以当我尝试在
main活动中使用
onClickListener
时,它没有意识到吗

我也不确定我对MainActivity.java中的
v.getId()
有什么期望

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;
    }
  }
}
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();
  }
}
Navigation.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;
    }
  }
}
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()
    添加到导航.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)

不能从字段中指定上下文。您可能“无法分配权限”,因为您实际上得到了nullpointerexception并编辑了错误的位置。也就是说,不需要
getApplicationContext()

另外,我是否可以建议您返回内容布局,而不是将上下文强制转换为活动

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);
}
我也不确定我应该从MainActivity.java R.id.btnGame中的v.getId()中得到什么


ID与变量名不匹配。如果不明确地给按钮一个ID,它将是
-1

什么错误?你能给他们看一下吗?我没有发现任何错误,在构建之后,logcat是空的。我将编辑上面的内容以包含生成输出。您过滤掉错误了吗?没有,那是在
verbose
上。好吧,我相当确定那里的上下文应该为空,但是,您的按钮没有做任何事情,因为您似乎缺少一个setOnClickListener。我曾尝试将类定义移动到onCreate方法中,但我认为这不是问题所在,因为最初的菜单调用仍在进行。我移动了它,看不出有什么不同。我已删除
getApplicationContext()
。所以现在我把它保留为
btnGame=newbutton(mContext)?我也没有得到任何回应,太棒了。我不知道如何在按钮中添加
clickListener
!您的侦听器可以在任何需要的地方实现。您还可以将setOnClickListener方法添加到导航类中,从MainActivity I get
-1
中的
onClick
方法记录视图?我将注销侦听器,看看是否能找出我做错了什么。
v.getId()
是负数,因为您从未将ID设置到按钮上。。。您不使用XML的任何原因?显然,您已经有了一些生成id值的XML资源