问题描述
|
因此,我有一个菜单充气机,当我选择其中的一个充气机时,我的程序便关闭了,我相信这是因为该类还为我必须添加的一系列按钮实现了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);
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;
}
现在效果很好!感谢您的帮助。
解决方法
您的onClick侦听器应该不是问题。
强制关闭可以来自您的startActivity()调用。
您确定已在清单中声明“设置”活动吗?
您可以发布整个清单吗?