在android中使用java在何处编写点击事件

在android中使用java在何处编写点击事件,java,android,Java,Android,您好,我最近更新了may ADT,现在我正在使用eclipse开发新的应用程序。所以我想知道在哪里写setOnclickListner事件。我想在单击按钮时打开另一个活动。我试过各种组合,但每次都会出错。我的eclipse版本:8.1.2.201302132326请告诉我在哪里可以编写代码 添加setContentView(R.layout.activity_main)后;它给出了一个错误,应用程序将不幸失败 我还删除了一些代码并运行它,但它不起作用 例如 及 此代码已从编码中删除,但不起作用

您好,我最近更新了may ADT,现在我正在使用eclipse开发新的应用程序。所以我想知道在哪里写setOnclickListner事件。我想在单击按钮时打开另一个活动。我试过各种组合,但每次都会出错。我的eclipse版本:8.1.2.201302132326请告诉我在哪里可以编写代码

添加setContentView(R.layout.activity_main)后;它给出了一个错误,应用程序将不幸失败

我还删除了一些代码并运行它,但它不起作用

例如

此代码已从编码中删除,但不起作用

当我创建新的空白活动时,会自动创建fragment activity.xml,我希望在fragment activity.xml文件中进行GUI更改。如果我在activity.xml中做了更改,那么它会给出错误信息

请帮帮我

先谢谢你

这是自动生成的may代码

    public class MainActivity extends Activity
    {



        @Override
        protected void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);



            if (savedInstanceState == null) 
            {
                getFragmentManager().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;
                }

            }

        }

你应该把按钮碎片放在容器里

因此,您需要访问您应该这样指定的按钮,因为您的placehold片段膨胀了
fragment\u container.xml的布局

Button b1=(Button)rootView.findViewById(R.id.button1);
b1.setOnclickListener(this); 
在本例中,您的类实现了
onclicklistener
,而
onclick
将自动覆盖您的类

public class MainActivity extends Activity
    {



        @Override
        protected void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);



            if (savedInstanceState == null) 
            {
                getFragmentManager().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);
                            final Activity contect=getActivity();
                Button b1=(Button)rootView.findViewById(R.id.button1);
                 b1.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent in=new Intent(contect,YourActivity.class);
                startActivity(in);
            }
        }) ;

                    return rootView;
                }

            }

        }
将按钮放在fragment_main中,移动到其他活动

否则,删除片段并扩展活动,同时从代码和项目中删除appcompat_v7库


从下面删除库属性-->Android-->删除它

尝试扩展FragmentActivity而不是activity您可以删除片段代码并在activity.xml中进行更改,并在onCreate()中使用setOnclickListner。
Button b1=(Button)rootView.findViewById(R.id.button1);
b1.setOnclickListener(this); 
public class MainActivity extends Activity
    {



        @Override
        protected void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);



            if (savedInstanceState == null) 
            {
                getFragmentManager().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);
                            final Activity contect=getActivity();
                Button b1=(Button)rootView.findViewById(R.id.button1);
                 b1.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent in=new Intent(contect,YourActivity.class);
                startActivity(in);
            }
        }) ;

                    return rootView;
                }

            }

        }