I need to resolve this problem ASAP

0 abdul almalki · March 21, 2015
package com.example.sensitiveguy.myapplication;

import android.graphics.Color;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        //layouts
        RelativeLayout BuckyLayout = new RelativeLayout(this);
        BuckyLayout.setBackgroundColor(Color.GREEN);
        RelativeLayout.LayoutParams buttonDetails=  new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT
        );
        buttonDetails.addRule(RelativeLayout.CENTER_HORIZONTAL);
        buttonDetails.addRule(RelativeLayout.CENTER_HORIZONTAL);
        RelativeLayout.LayoutParams usernameDetails=  new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT
        );
        usernameDetails.addRule(RelativeLayout.ABOVE,redButton.getId());
        usernameDetails.addRule(RelativeLayout.CENTER_HORIZONTAL);

        //username
        EditText username= new EditText(this);
        BuckyLayout.addView(username);
        //Button
        Button redButton= new Button(this);
        redButton.setBackgroundColor(Color.RED);
        BuckyLayout.addView(redButton,buttonDetails);
        setContentView(BuckyLayout);
        redButton.setId(1);
        username.setId(2);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}



when i try to add redButton.getId() such like  in line  31  show me erro!! 
Idont know how to  insert Image  but i  upload ithe captur screen here  
is the picture link   http://www9.0zz0.com/2015/03/21/14/483009764.png
please i need  assisstance ASAP   try to copy  the

Post a Reply

Replies

Oldest  Newest  Rating
+1 Hemin Desai · March 22, 2015
Hey bro, I think I figured out your error.
You mentioned the rules for the button before initiating and declaring it.
Try to initiate the button and after that write rules for that button.

Execute the below code:

//username
EditText username= new EditText(this);
BuckyLayout.addView(username);
//Button
Button redButton= new Button(this);
redButton.setBackgroundColor(Color.RED);
BuckyLayout.addView(redButton,buttonDetails);
setContentView(BuckyLayout);
redButton.setId(1);
username.setId(2);

Write the above piece of code after the below lines of code:

//layouts
RelativeLayout BuckyLayout = new RelativeLayout(this);
BuckyLayout.setBackgroundColor(Color.GREEN);


Thanks.
  • 1

Java / Android Development

107,322 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator