When I try to use the button method and test it on the android emulator, it always crashes. If I don't call any methods its work first, but I need to edit it when I program. A simple example is test1, if I comment out test1.getHeight() it works fine, but when I use it, it crashes. Ultimately, I want to be able to edit the height/width.
public class MainActivity extends Activity {
final Button [] mainMenuBtn = new Button[10];
Button test1 = new Button(this);
@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();
}
int windowWidth = this.getResources().getDisplayMetrics().widthPixels;
int windowHeight = this.getResources().getDisplayMetrics().heightPixels;
test1 = (Button)findViewById(R.id.button0);
test1.getHeight();
}
@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;
}
}
}
I changed it and it still didnt work. This is my xml
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridLayout0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnCount="4"
>
<ImageView
android:id="@+id/imageView1"
android:layout_column="0"
android:layout_row="0"
android:layout_columnSpan="1"
android:src="@drawable/ic_launcher" />
<ImageButton
android:id="@+id/imageButton1"
android:layout_column="1"
android:layout_row="0"
android:layout_columnSpan="3"
android:src="@drawable/ic_launcher" />
</GridLayout>
And this is the code, it crashes if I call any method, but it doesnt crash when I new it or add the ID. Its really straight forward, I don't know why it causes this, perhaps its because i using the newest SDK?
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.os.Build;
public class MainMenu extends Activity {
ImageView checkBox1;
ImageButton checkButton1 = new ImageButton(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
checkBox1 = new ImageView(this);
checkBox1 = (ImageView)findViewById(R.id.imageView1);
checkButton1 = (ImageButton)findViewById(R.id.imageButton1);
final ViewTreeObserver viewTreeObs = checkBox1.getViewTreeObserver();
viewTreeObs.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
checkBox1.setBackgroundColor(Color.RED);
viewTreeObs.removeGlobalOnLayoutListener(this);
}
});
//
}
@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, 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_menu,
container, false);
return rootView;
}
}
}