Android的setOnClickListener不工作 [英] Android setOnClickListener not working
问题描述
添加按钮动作在安卓Activity.java应用程序后关闭很可惜。 请帮忙解决。我在这里缺少什么code。????
公共类MainActivity扩展ActionBarActivity {
INT计数器;
按钮添加,子;
TextView的显示;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
计数器= 0;
添加=(按钮)findViewById(R.id.bAdd);
分=(按钮)findViewById(R.id.bSub);
显示器=(TextView中)findViewById(R.id.textDisp);
add.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(视图v){
//执行上的点击动作
反++;
}
});
如果(savedInstanceState == NULL){
getSupportFragmentManager()的BeginTransaction()
。新增(R.id.container,新PlaceholderFragment())
。承诺();
}
}
@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
//充气菜单;这增加了项目操作栏,如果它是present。
。getMenuInflater()膨胀(R.menu.main,菜单);
返回true;
}
@覆盖
公共布尔onOptionsItemSelected(菜单项项){
//处理动作栏项目点击这里。将操作栏
//自动在主/向上按钮操作的点击,只要
//你在AndroidManifest.xml中指定一个父活动。
INT的id = item.getItemId();
如果(ID == R.id.action_settings){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
/ **
*包含一个简单的视图的占位符片段。
* /
公共静态类PlaceholderFragment扩展片段{
公共PlaceholderFragment(){
}
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
捆绑savedInstanceState){
查看rootView = inflater.inflate(R.layout.fragment_main,集装箱,假);
返回rootView;
}
}
}
看来你建立你的看法里面的 fragment_main.xml 而非 activity_main.xml 。
在你的第一的创建一个新的Android项目,你有这些文件,这些文件将被自动创建并打开了:
然后,当你开始,你添加视图的(例如:一个TextView)的内部 fragment_main.xml
文件。而你试图做一个基本的事件这样的观点你的活动里面
,是这样的:
公共类MainActivity延伸活动{
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); //使用布局activity_main.xml
//尝试设置视图中的一个简单的文本(的TextView)previously添加
TextView的文字=(TextView的)findViewById(R.id.textView1);
text.setText(简单文字); //你这里得到一个错误!
/ *
*您做一个片段的交易增加PlaceholderFragment片段
*在屏幕上 - 这下面snippnet自动创建。
* /
如果(savedInstanceState == NULL){
getSupportFragmentManager()的BeginTransaction()
。新增(R.id.container,新PlaceholderFragment())提交()。
}
}
您不能运行你的应用程序或有时你只有一个白色的屏幕,因为你尝试调用/显示器的意见是进错了布局。
解决方法:移动所有的东西onCreateView方法进入片段类中。打电话的意见和做一下相关的片段,而不是父活动。
例如,对于您的情况:
公共静态类PlaceholderFragment扩展片段{
INT计数器;
按钮添加,子;
TextView的显示;
公共PlaceholderFragment(){
}
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
捆绑savedInstanceState){
查看rootView = inflater.inflate(R.layout.fragment_main,集装箱,假);
计数器= 0;
//不要忘记附上您的视角以膨胀的看法为rootView.findViewById()
添加=(按钮)rootView.findViewById(R.id.bAdd);
分=(按钮)rootView.findViewById(R.id.bSub);
显示器=(TextView中)rootView.findViewById(R.id.textDisp);
add.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(视图v){
//执行上的点击动作
反++;
}
});
返回rootView;
}
}
After adding button action in android Activity.java application is closing unfortunately. please help in resolving. what code i am missing here.????
public class MainActivity extends ActionBarActivity {
int counter;
Button add, sub;
TextView display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 0;
add = (Button) findViewById(R.id.bAdd);
sub = (Button) findViewById(R.id.bSub);
display = (TextView) findViewById(R.id.textDisp);
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
counter++;
}
});
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
It seems you built your views inside fragment_main.xml and not activity_main.xml.
When you first create a new android project, you have these files which are automatically created and opened:
Then, when you begin, you add views (e.g: a TextView) inside fragment_main.xml
file. Whereas you tried to do a basically event with this view inside your Activity
, something like this:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // Using layout activity_main.xml
// You try to set a simple text on the view (TextView) previously added
TextView text = (TextView) findViewById(R.id.textView1);
text.setText("Simple Text"); // And you get an error here!
/*
* You do an fragment transaction to add PlaceholderFragment Fragment
* on screen - this below snippnet is automatically created.
*/
if(savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
You cannot run your app or sometimes you have only a white screen, because the views that you tried to call/display are into the wrong layout..
Solution: Move all your stuff inside onCreateView method into the Fragment class. Call views and do something in the related fragment and not the parent activity.
For example, for your case:
public static class PlaceholderFragment extends Fragment {
int counter;
Button add, sub;
TextView display;
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
counter = 0;
// Don't forget to attach your view to the inflated view as "rootView.findViewById()"
add = (Button) rootView.findViewById(R.id.bAdd);
sub = (Button) rootView.findViewById(R.id.bSub);
display = (TextView) rootView.findViewById(R.id.textDisp);
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
counter++;
}
});
return rootView;
}
}
这篇关于Android的setOnClickListener不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!