Android setOnClickListener 不起作用 [英] Android setOnClickListener not working

查看:80
本文介绍了Android setOnClickListener 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在android Activity.java 应用程序中添加按钮操作后不幸关闭.请帮助解决.我在这里缺少什么代码.????

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;
    }
}

}

推荐答案

看来您是在 fragment_main.xml 而不是 activity_main.xml 中构建了视图.

It seems you built your views inside fragment_main.xml and not activity_main.xml.

当您第一次创建一个新的 android 项目时,您会自动创建并打开这些文件:

When you first create a new android project, you have these files which are automatically created and opened:

然后,当您开始时,在 fragment_main.xml 文件中添加视图 (例如:TextView).而您尝试在 Activity 中使用此视图执行基本事件,如下所示:

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..

解决方案:将 onCreateView 方法中的所有内容移动到 Fragment 类中.调用视图并在相关片段而不是父活动中执行某些操作.

<小时>

例如,对于您的情况:


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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆