menuinflator 和 onclicklistener 的问题
所以我有一个菜单充气器,当我在其中选择某些内容时,我的程序会强制关闭,我相信这是因为该类还为我必须添加的一系列按钮实现了 onclicklistener。以下是一些相关代码:
package com.riley.howmany;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ScrollView;
import android.widget.LinearLayout;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
public class howMany extends Activity implements View.OnClickListener{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//I have to use a dynamic layout because it changes based on user options.
//As of right now it is just in a for loop because the settings menu won't open
//because of this issue. I hope this makes sense.
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
ll.setPadding(1,1,1,1);
TextView tv = new TextView(this);
tv.setText("Dynamic layouts ftw!");
ll.addView(tv);
//Each button press actually performs the same code for that individual button
for (int c=0; c<=10; c++) {
Button b = new Button (this);
b.setText("Button:"+" "+"0");
b.setTextSize(10.0f);
b.setOnClickListener(this);
ll.addView(b);
}
this.setContentView(sv);
}
public void onClick(View view) {
//handle each button click
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menus, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent = new Intent(this, Setting.class);
startActivity(intent);
return true;
}
}
非常感谢您提供的任何建议!
编辑/更新!
好的,我找到了修复方法。就是这样:
@Override
public boolean onCreateOptionsMenu (Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menus, menu);
return true;
}
@Override
public boolean onOptionsItemSelected (MenuItem item) {
startActivity(new Intent(this, Setting.class));
return true;
}
现在效果很好!谢谢你的帮助。
So I have a menu inflator and when I select something in it, my program force closes and I believe it is because the class also implements onclicklistener for an array of buttons I have to add. Here is some of the relevant code:
package com.riley.howmany;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ScrollView;
import android.widget.LinearLayout;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
public class howMany extends Activity implements View.OnClickListener{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//I have to use a dynamic layout because it changes based on user options.
//As of right now it is just in a for loop because the settings menu won't open
//because of this issue. I hope this makes sense.
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
ll.setPadding(1,1,1,1);
TextView tv = new TextView(this);
tv.setText("Dynamic layouts ftw!");
ll.addView(tv);
//Each button press actually performs the same code for that individual button
for (int c=0; c<=10; c++) {
Button b = new Button (this);
b.setText("Button:"+" "+"0");
b.setTextSize(10.0f);
b.setOnClickListener(this);
ll.addView(b);
}
this.setContentView(sv);
}
public void onClick(View view) {
//handle each button click
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menus, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent = new Intent(this, Setting.class);
startActivity(intent);
return true;
}
}
Thank you very much for any advice you can provide!
EDIT/UPDATE!
Ok I found the fix. Here it is:
@Override
public boolean onCreateOptionsMenu (Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menus, menu);
return true;
}
@Override
public boolean onOptionsItemSelected (MenuItem item) {
startActivity(new Intent(this, Setting.class));
return true;
}
Now it works great! Thank you for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 onClick 侦听器不应该是问题。
强制关闭可以来自您的 startActivity() 调用。
您确定已在清单中声明了设置活动吗?
你能发布你的整个清单吗?
Your onClick listener shouldn't be the problem.
The force close can come from your startActivity() call.
Are you sure you have declared the Setting activity in your manifest?
Could you post your whole manifest?