Unable to create layout from MainActivity.java

+1 pratt 15 · January 10, 2015
I m following the bucky android tuts and as per tut 14, my emulator is not showing the layout and button which i have created from MainActivity.java. Do i have to change something in activity_main.xml?


package com.example.pratt.myapplication;

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


public class MainActivity extends ActionBarActivity {

    private static final String TAG="My Message";

@Override
    protected void onCreate(Bundle savedInstanceState) {
        /*
        */
        super.onCreate(savedInstanceState);
        RelativeLayout myLayout=new RelativeLayout(this);
        Button blue_button=new Button(this);
        myLayout.setBackgroundColor(Color.RED);
        myLayout.addView(blue_button);
         blue_button.setText("Blue Button");
        Log.i(TAG,"onCreate");
        setContentView(myLayout);
    }


    @Override
    protected void onStop() {
        super.onStop();
        Log.i(TAG,"onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG,"onDestroy");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i(TAG,"onResume");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i(TAG,"onStart");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i(TAG,"onPause");
    }

   


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


Post a Reply

Replies

Oldest  Newest  Rating
0 Hector Boone · January 10, 2015
I've had problems with emulators not showing before. I have three set up to test with and also try using different API levels.
  • 1

Java / Android Development

107,236 followers
About

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

Links
Moderators
Bucky Roberts Administrator