简体   繁体   中英

Force to close error in android

I am new to android and i am now only started.For me when i tried to implement on click listener for a button the application closes.Can anybody help me.

Following is the code:

public class Sampleprojectsubbu1Activity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);        
        final  Button b1=(Button)findViewById(android.R.id.button1);
        b1.setOnClickListener(new View.OnClickListener()
        {       
        @Override
        public void onClick(View v) 
        {           

            // TODO Auto-generated method stub

        }
    });
    }
}

您使用的ID是“ android.R.id.button1”,它是android的一部分,应使用类似R.id.your_button_id的名称

May be in this line you're having the error -

final  Button b1=(Button)findViewById(android.R.id.button1);

in above line android.R.id.button1 will provide by default button which will coming with android jar.

Instead of there, you've to use the button id. What've you used in your main.xml file. Like if you're having one button in your main.xml with id of buttonconfirm means, you should use like below -

final  Button b1=(Button)findViewById(R.id.buttonconfirm);

Hope this helps you. And, also please post your main.xml with your question.

Please change this line .

final  Button b1=(Button)findViewById(android.R.id.button1);

to

final  Button b1=(Button)findViewById(R.id.button1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM